function doResizeFrame(szFrameID, intMinSize) {
	var oFrame = parent.frames[szFrameID];
	if (typeof oFrame == 'undefined') 
		oFrame = document.frames[szFrameID];

	if (typeof oFrame != 'undefined') {
		intHeight =  document.body && document.body.scrollHeight? oFrame.document.body.scrollHeight : oFrame.document.height? oFrame.document.height : null;
		if (intHeight) {
			if(intMinSize && intHeight < intMinSize)
				intHeight = intMinSize;
			oFrame.resizeTo(parent.document.getElementById(szFrameID).offsetWidth, intHeight); 
		} else {
			setTimeout("doResizeFrame('" + szFrameID + "', " + intMinSize + ")", 1000);
		}
	} 
}

function doClientSearch(){
	szClientInfo = window.showModalDialog("../search/client_search_frame.php","Client","dialogHeight: 400px; dialogWidth: 750px; edge: Raised; center: Yes; help: Yes; resizable: no; status: yes;");
	if(szClientInfo)
		return szClientInfo;
	else
		return '';
}

function doMouseOverSelect(oRow){
	if(oOver)
		oOver.className = 'SelectRow';
	oOver = oRow;
	oRow.className = 'SelectRowMouseOver';
}

function doMouseOutSelect(oRow){
	if(oOver)
		oOver.className = 'SelectRow';
	oOver = null;
}

function getSelected(oList){
	szAddIDs = '';
	szNameList = '';
	for(i=0; i < oList.options.length;i++){
		if(oList.options[i].selected){
			if(szAddIDs.length > 0){
				szAddIDs += ',';
				szNameList += ', ';
			}
			szAddIDs += oList.options[i].value;
			szNameList += oList.options[i].text;
		}
	}
	return szAddIDs;
}

function LTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1 || s.charCodeAt(0) == 160) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1 || s.charCodeAt(j) == 160)
			j++;
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;       
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}


function Trim(str){
		return RTrim(LTrim(str));
}


function szFormatMoney(szIn){

	szMeniscus = '.00';
	szChar = ',';	
	blnIsNegative = false;
	szOutput = '';

	// parses a number and inserts the separation character in the appropriate places.
	szIn = new String(szIn)	
	if(szIn == 'undefined' || szIn == 'null')
		szIn = '0.00';
	if(szIn.substr(0,1) == '-'){
		szIn = szIn.substr(1);
		blnIsNegative = true;
	}
	if(szIn.indexOf('.') > 0){
		szMeniscus = szIn.substr(szIn.indexOf('.'), 3);
		szIn = szIn.substr(0, szIn.indexOf('.'));
	}
	while(szIn.length > 3){
		szOutput =  szChar + szIn.substr(szIn.length - 3) + szOutput;
		szIn = szIn.substr(0, szIn.length - 3);
	}
	szOutput = szIn + szOutput;

	
	if(blnIsNegative)
		return '($' + szOutput + szMeniscus + ')';
	else
		return '$' + szOutput + szMeniscus;
}


function doUpdateCustomHTML(intActionID, intContentID, intReferenceID){
	szFeatures = 'width=800,height=620,left=100,top=60,screenX=100,screenY=60,status=no,menubar=no,resizable=no,toolbar=no,scrollbars=no';
	myWindow = window.open('custom_page.asp?szCommand=SHOWUPDATE&intContentID=' + intContentID + '&intReferenceID=' + intReferenceID, 'winHTMLUpdate', szFeatures);
	myWindow.focus();
}


function getDOMObject(szName){
	return eval('document.all.' +szName);
	if (oObject != null){
	   if (oObject.length > 0){
		 return oObject(0);
	   } else 
		  return oObject;
	} 
	return null
}

function doListMove(lstSource, lstDest){
	// ===============================
	// * Moves 0 or more items from the source list to a sorted position in the dest list. 
	// * Returns a comma seperated string of the IDs of the items that were moved.
	// * 
    // * lstSource: The object reference to the source list 
	// * lstDest: The object reference to the dest list
	// ===============================
	delArray = new Array;
	x=0;
	for(i=0; i < lstSource.options.length; i++){
		if(lstSource.options[i].selected){
			blnIsSelection = true;
			var optNew = new Option(lstSource.options[i].text, lstSource.options[i].value, false, false);
			delArray[x++] = lstSource.options[i].value;

			//Sort the list.
			j = lstDest.options.length - 1;
			while(j > -1 && optNew.text <= lstDest.options[j].text ){
				var optSwap = new Option(lstDest.options[j].text, lstDest.options[j].value, false, false);
				lstDest.options[j+1] = optSwap;
				j--;
			}
			lstDest.options[j+1] = optNew;
		}
	}

	for(i=0; i<delArray.length;i++){
		for(j=0; j < lstSource.options.length; j++){
			if(lstSource.options[j].value == delArray[i])
				lstSource.options[j] = null;
		}
	}
	return delArray.toString();
}


function resizeImage(szImg, intSize){
	oImg = document.getElementById(szImg);
	if(intSize == null)	
		intSize = '';
	if(!oImg)
		return false;

	h = parseInt(oImg.height);
	w = parseInt(oImg.width);
	if(h == 0 || w == 0){
		setTimeout('resizeImage("' + szImg + '", ' + intSize + ')', 1000)
		return false;
	}

	if(h > w && h > intSize){
		intProportion = w/h;
		oImg.height = intSize;
		oImg.width = parseInt(intSize * intProportion);
	} else if(w > intSize) {
		intProportion = h/w;
		oImg.width = intSize;
		oImg.height = parseInt(intSize * intProportion);
	}
}

function szMonthName(InValue){
	// Changed to return short month...
	return szShortMonthName(InValue);

	switch(InValue){
	case 1: return 'January';
	case 2: return 'February';
	case 3: return 'March';
	case 4: return 'April';
	case 5: return 'May';
	case 6: return 'June';
	case 7: return 'July';
	case 8: return 'August';
	case 9: return 'September';
	case 10: return 'October';
	case 11: return 'November';
	case 12: return 'December';
	}
}

function szShortMonthName(InValue){
	switch(InValue){
	case 1: return 'Jan';
	case 2: return 'Feb';
	case 3: return 'Mar';
	case 4: return 'Apr';
	case 5: return 'May';
	case 6: return 'Jun';
	case 7: return 'Jul';
	case 8: return 'Aug';
	case 9: return 'Sep';
	case 10: return 'Oct';
	case 11: return 'Nov';
	case 12: return 'Dec';
	}
}

function szFormatShortDate(dtInDate) {
	if (dtInDate!=null && dtInDate != 'null' && dtInDate != ''){
		var dtNew = new Date(dtInDate);
		return szShortMonthName(dtNew.getMonth() + 1) + ' ' + dtNew.getDate() + ', ' + dtNew.getFullYear();
	}
	else
		return '';
}

function szFormatDate(dtInDate) {
 
	if (dtInDate!=null && dtInDate != 'null' && dtInDate != ''){
		var dtNew = new Date(dtInDate);
		return szMonthName(dtNew.getMonth() + 1) + ' ' + dtNew.getDate() + ', ' + dtNew.getFullYear();
	}
	else
		return '';
}

function szFormatDateTime(dtInDate) {
	if (dtInDate!=null && dtInDate != 'null' && dtInDate != ''){
		var dtNew = new Date(dtInDate);
		h = parseInt(dtNew.getHours())
		if(h > 0)
			return szFormatDate(dtInDate) + ' ' + dtNew.getHours() + ':' + dtNew.getMinutes();
		else
			return szFormatDate(dtInDate);
		}
	else
		return '';
}


/* This function validates that the input passed in is of the required type. 
 * Parameters:
 * szInput		-	the value to be validated	(Required)
 * intMinLength	-	the minimum length			(default 0) 
 *					if > 0
 *					the field is required
 * intMaxLength	-	the maximum length for		(default 0) 
 *					string type only
 * returns:		-	true if valid, false otherwise. 
 *					Set the fields on oValidation as appropriate
 */

oValidation = new Object;
oValidation.isTooShort = false;
oValidation.isTooLong = false;
oValidation.isWrongType = false;
oValidation.szTypeMessage = '';

function isValid(szInput, szType, szFieldName, blnShowMessages, intMinLength, intMaxLength){
	oValidation.isTooShort = false;
	oValidation.isTooLong = false;
	oValidation.isWrongType = false;
	oValidation.szTypeMessage = '';

	switch(szType){
		case 'DATE':
			break;
		case 'INTEGER':
			isValidInteger(szInput, intMinLength, intMaxLength);
			break;
		case 'FLOAT':
			isValidFloat(szInput, intMinLength, intMaxLength);
			break;
		case 'NAME':
			isValidName(szInput, intMinLength, intMaxLength)
			break;
		case 'EMAIL':
			isValidEmail(szInput, intMinLength, intMaxLength)
			break;
		case 'TELEPHONE':
			isValidTelephone(szInput, intMinLength)
			break;
		case 'SEARCH':
			isValidSearch(szInput)
			break;
		case 'TEXT':
			isValidText(szInput, intMinLength, intMaxLength)
			break;
		case 'XMLTEXT':
			isValidXMLText(szInput, intMinLength, intMaxLength)
			break;
		case 'URL':
			isValidURL(szInput, intMinLength, intMaxLength)
			break;
		case 'CSS':
			break;
		case 'HTML':
			break;
		case 'HTMLWITHSCRIPT':
			break;
		default:
			alert('You have passed in an incorrect type.');
			oValidation.isTooShort = true;
			oValidation.isTooLong = true;
			oValidation.isWrongType = true;
	}
	//return the default error messages if required.
	if(blnShowMessages && (oValidation.isTooShort || oValidation.isTooLong ||
			oValidation.isWrongType)){
		szMsg = '';
		if (szFieldName && szFieldName != '')
			szMsg = '<B>' + szFieldName + '</B>: ';
		if (szType == "INTEGER" || szType == "FLOAT") {
			if(oValidation.isTooShort || oValidation.isTooLong)
				if(intMinLength && intMaxLength)
					szMsg += 'Please enter a number between ' + intMinLength + ' and ' + intMaxLength + '.<BR>';
				else
					szMsg += 'You must enter a value for this field.<BR>';

		} else {
			if(oValidation.isTooShort){
				if(intMinLength == 0)
					szMsg += 'You must enter a value.<BR>';
				else if(intMinLength == 1)
					szMsg += 'You must enter a value for this field.<BR>'
				else
					szMsg += 'The value must be at least ' + intMinLength + ' characters in length.<BR>'
			}
			if(oValidation.isTooLong){
				szMsg += 'The value cannot be longer that ' + intMaxLength + ' characters in length.<BR>'
			}
		}
		if(oValidation.isWrongType){
			szMsg += oValidation.szTypeMessage + '<BR>'
		}
		oErrorMessages.innerHTML += szMsg;
		oErrorDisplay.style.display = 'inline';
	}
	return 	!(oValidation.isTooShort || oValidation.isTooLong || oValidation.isWrongType)
}

function getFormValue(szFormValue, szForm){
	if(szFormValue == '' || szFormValue == 'null'){
		alert('You have not specified a form value.');
		return false;
	}
	var szReturn = null;
	if(szForm == null)
		szForm = 'frmMain'
	try{
		szReturn = eval('document.' + szForm + '.' + szFormValue + '.value');
	} catch(frmErr){
		alert('The form value: document.' + szForm + '.' + szFormValue + '.value was not found.');
		return false;
	}
	return szReturn;
}

function isValidName(szName, intMinLength, intMaxLength){
	szName = Trim(szName);
	var re = /[^a-zA-Z\s\'-]/
	if(re.test(szName)){
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'This field can only contain letters, a single quote, and the hyphen.'
	}
	if(intMinLength && szName.length < intMinLength)
		oValidation.isTooShort = true;
	if(intMaxLength > 0 && szName.length > intMaxLength)
		oValidation.isTooLong = true;
}

function isValidURL(szURL, intMinLength, intMaxLength){
	szURL = Trim(szURL);
	if (szURL.indexOf("http") > -1 && szURL.indexOf("http://") == -1) {
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'A valid URL must begin with "http://".';
	} else {
		var re = /[^a-zA-Z\s\d\.:+\_\-\?\%\&\/\\]/
		if(re.test(szURL)){
			oValidation.isWrongType = true;
			oValidation.szTypeMessage = 'This field can only contain valid URL characters.'
		}
		if(intMinLength && szURL.length <= intMinLength)
			oValidation.isTooShort = true;
		if(intMaxLength > 0 && szURL.length > intMaxLength)
			oValidation.isTooLong = true;
	}
}

function isValidSearch(szSearch){
	szName = Trim(szSearch);
	var re = /[^a-zA-Z\"\s\d\*]/
	if(re.test(szSearch)){
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'The search string can only contain letters and numbers and the * character.'
	}
}

function isValidEmail(szEmail, intMinLength, intMaxLength){
	szEmail = Trim(szEmail);
	if((String(intMinLength) == 'undefined' || intMinLength == 0) && szEmail.length == 0)
		return true;
	else {
		if (szEmail.indexOf(",") > -1)
			arrEmail = szEmail.split(",");
		else
			arrEmail = szEmail.split(";");
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
		for (key in arrEmail) {
			szEmail = Trim(arrEmail[key]);
			if(!re.test(szEmail)){
				oValidation.isWrongType = true;
				oValidation.szTypeMessage = 'This field must contain a valid email address.';
				break;
			}
		}
	}
}


function isValidTelephone(szNumber, intMinLength){
	szNumber= Trim(szNumber);
	var re = /[^\s\d()-]/
	if(szNumber.length > 0 && re.test(szNumber)){
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'Telphone numbers can only contain numbers and (, ), -.'
	}

	if(intMinLength && szNumber.length < intMinLength)
		oValidation.isTooShort = true;
}

function isValidFloat(szFloat, intMinLength, intMaxLength){
	var re = /^[-+]?\d+(\.\d+)?$/
	if(re.test(szFloat)){
		;
	} else {
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'This field must contain a valid number.'
	}
	if (!oValidation.isWrongType) {
		if(intMinLength && szFloat < intMinLength)
			oValidation.isTooShort = true;
		if(intMaxLength > 0 && szFloat > intMaxLength)
			oValidation.isTooLong = true;
	}
}

function isValidInteger(szInteger, intMinLength, intMaxLength){
	var re = /[^\d]/
	if(re.test(szInteger) || isNaN(parseInt(szInteger))){
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'This field must contain a valid integer.'
	}
	if(intMinLength && parseInt(szInteger) < intMinLength)
		oValidation.isTooShort = true;
	if(intMaxLength > 0 && parseInt(szInteger) > intMaxLength)
		oValidation.isTooLong = true;
}

function isValidText(szText, intMinLength, intMaxLength){
	szText = Trim(szText);
	var re = /[^\w\s\'\",.?:;\\\(\)\-\+\=\\[\]&\*\%\$\#\@\<\>\!\@\/]/
	if(re.test(szText)){
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'This field can only contain letters, numbers and some special characters.'
	}
	if(intMinLength && szText.length < intMinLength)
		oValidation.isTooShort = true;
	if(intMaxLength > 0 && szText.length > intMaxLength)
		oValidation.isTooLong = true;
}

function isValidXMLText(szXMLText, intMinLength, intMaxLength){
	szXMLText = Trim(szXMLText);
	var re = /[^\w\s\'\",.?:;\!]/
	if(re.test(szXMLText)){
		oValidation.isWrongType = true;
		oValidation.szTypeMessage = 'This field can only contain letters, numbers and some special characters.'
	}
	if(intMinLength && szXMLText.length <= intMinLength)
		oValidation.isTooShort = true;
	if(intMaxLength > 0 && szXMLText.length > intMaxLength)
		oValidation.isTooLong = true;
}

function clearErrors(){
	oErrDiv = document.getElementById('oErrorMessages');
	if(oErrDiv) {
		oErrDiv.innerHTML = '';
		oErrorDisplay.style.display = 'none';
	} else
		alert('The error div does not exist.');
}

function checkForErrors(){
	oErrDiv = document.getElementById('oErrorMessages');
	if(oErrDiv)
		return oErrDiv.innerHTML.length > 0;
	else
		alert('The error div does not exist.');
}

function addError(szError, szFieldName){
	if(!szError || szError == '')
		alert('You have not passed an error message to this function.')
	else {
		oErrDiv = document.getElementById('oErrorMessages');
		if(oErrDiv){
			if (szFieldName && szFieldName != '')
				oErrDiv.innerHTML += '<B>' + szFieldName + '</B>: ';
			oErrDiv.innerHTML += szError + '<BR>';
			oErrorDisplay.style.display = 'inline';
		} else
			alert('The error div does not exist.');
		
	}
}

function showParamWindow(szURL){
	szFeatures = 'width=600,height=542,left=140,top=150,status=no,menubar=no,resizable=no,toolbar=no';
	myWindow = window.open(szURL, 'winParamDetail', szFeatures);
	myWindow.focus();
}


