//
// $Id: inc_myfunctions.js 3064 2010-03-02 15:48:17Z agarg $
// $HeadURL: http://svn.merrymaids.com/svn1/web/shared/v1/trunk/csi/inc_myfunctions.js $

// Function to toggle pages of DIVs using a SELECT-OPTION drop down box,
// Use style="position:absolute; visibility:hidden;" for all html div boxes except one
// Usage: mySwitchDiv([select-option as object],[root name of divs as string])
function mySwitchDiv(vSelectName, vDivGroupName) {
	for (var i = 1; i <= vSelectName.length; i++) {
		vCurrentDivObj = document.getElementById(vDivGroupName+i);
		if (vSelectName.options[vSelectName.selectedIndex].value == i) {
			vCurrentDivObj.style.visibility = "visible";
		}
		else {
			vCurrentDivObj.style.visibility = "hidden";
		}
	}
}

// Function to select ALL checkboxes on a form
// Usage: myToggleAllCBFrm([form as object],[check value as boolean])
function myToggleAllCBFrm(vFormObj, vCheckValue) {
	for (var i = 0; i < vFormObj.elements.length; i++) {
		vElementObj = vFormObj.elements[i];
		if (vElementObj.type.toUpperCase() == 'CHECKBOX') {
			vElementObj.checked = vCheckValue;
		}
	}
}

function myToggleCBArray(vCBArrayObj, vCheckValue) {

	//alert(vCBArrayObj.length);
	for (var i = 0; i < vCBArrayObj.length; i++) {
		//alert(vCBArrayObj[i].checked);
		vCBArrayObj[i].checked = vCheckValue;
	}
}

// Function to select all items in a select-option listbox
// Usage: myToggleAllSelOptSingle([select-option as object],[select value as boolean])
function myToggleAllSelOptSingle(vSelOptObj, vSelectValue) {
	vSelOptObj.multiple = true;
	for (var i=0; i<vSelOptObj.length; i++) {
		vSelOptObj.options[i].selected = vSelectValue;
	}
}

// Function to select ALL items in ALL Select-multiple boxes on a form
// Usage: myToggleAllSelOptFrm([form as object],[select value as boolean])
function myToggleAllSelOptFrm(vFormObj, vSelectValue) {
	for (var i = 0; i < vFormObj.elements.length; i++) {
		vElementObj = vFormObj.elements[i];
		if (vElementObj.type.toUpperCase() == 'SELECT-MULTIPLE') {
			vElementObj.disabled = false;
			for (var j=0; j<vElementObj.length; j++) {
				vElementObj.options[j].selected = vSelectValue;
			}
		}
	}
}

// Function to validate a radio group
// Usage: myValRadio([form as object], [friendly_field_name as string], [requiredvalue as bool])
function myValRadio(vRadioObj, vFriendlyFieldName, vRequiredBool) {

	vSelectedFlag = false;

	for (var i = 0; i < vRadioObj.length; i++) {
		if (vRadioObj[i].checked == true) {
			vSelectedFlag = true;
		}
	}

	if ( (vRequiredBool == 1) && (vSelectedFlag == true) ) {
		return true;
	}

	else if ( (vRequiredBool == 1) && (vSelectedFlag == false) ) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
	}

	else {
		return true;
	}
}

// Function to disable all elements on a form
// Usage: myFormDisableAll([form as object])
function myFormDisableAll(vForm) {

	oEle = vForm.elements;

	for (i = 0; i < oEle.length; i++) {
		if ( (oEle[i].type == "submit") || (oEle[i].type == "button") || (oEle[i].type == "text") || (oEle[i].type == "file") || (oEle[i].type == "select-one") || (oEle[i].type == "select-many") || (oEle[i].type == "checkbox") || (oEle[i].type == "radio") || (oEle[i].type == "textarea") ) {
			oEle[i].disabled = true;
		}
	}
}

// Function to enable all elements on a form
// Usage: myFormEnableAll([form as object])
function myFormEnableAll(vForm) {

	oEle = vForm.elements;

	for (i = 0; i < oEle.length; i++) {
		if ( (oEle[i].type == "submit") || (oEle[i].type == "button") || (oEle[i].type == "text") || (oEle[i].type == "file") || (oEle[i].type == "select-one") || (oEle[i].type == "select-many") || (oEle[i].type == "checkbox") || (oEle[i].type == "radio") || (oEle[i].type == "textarea") ) {
			oEle[i].disabled = false;
		}
	}
}

// Function to submit a form and disable all user controls, prevents double-clicking submit buttons, etc
// Usage: myFormSubmitDisable([form as object])
function myFormSubmitDisable(vForm, vMessage) {

	if (vMessage.length > 0) {
		alert(vMessage);
	}

	window.status = "Please wait...";

	myFormEnableAll(vForm); // Enable first, so all fields get submitted to the server
	vForm.submit();
	myFormDisableAll(vForm);
}


// Function to validate a string by length
// Usage: myValString([textbox as object], [friendly_field_name as string], [minlength as integer], [maxlength as integer], [requiredvalue as bool])
function myValString(vTextObj, vFriendlyFieldName, vMinLength, vMaxLength, vRequiredBool) {

	if ( ((vRequiredBool == 1) && (vTextObj.value.length > 0)) || ((vRequiredBool == 0) && (vTextObj.value.length > 0)) ) {

		if ((vTextObj.value.length < vMinLength) || (vTextObj.value.length > vMaxLength)) {
			if (vRequiredBool) {
				alert("Field '" + vFriendlyFieldName + "' must be at least " + vMinLength + " but not more than " + vMaxLength + " characters.");
			}
			else {
				alert("Field '" + vFriendlyFieldName + "' must not be more than " + vMaxLength + " characters.");
			}
			vTextObj.focus();
			return false;
		}
		else {
			return true;
		}
	}
	else if ((vRequiredBool == 1) && (vTextObj.value.length == 0)) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
		vTextObj.focus();
		return false;
	}
	else {
		return true;
	}
}

// Function to validate a number by min and max values
// Usage: myValNumber([textbox as object], [friendly_field_name as string], [minvalue as integer], [maxvalue as integer], [requiredvalue as bool])
function myValNumber(vNumberObj, vFriendlyFieldName, vMinValue, vMaxValue, vRequiredBool) {

	if ( ((vRequiredBool == 1) && (vNumberObj.value.length > 0)) || ((vRequiredBool == 0) && (vNumberObj.value.length > 0)) ) {

		var my_regex = /^(\d+)$/;

		if (my_regex.test(vNumberObj.value) == 1) {
			if ((vNumberObj.value >= vMinValue) && (vNumberObj.value <= vMaxValue)) {
				return true;
			}
			else {
				alert("Field '" + vFriendlyFieldName + "' must be at least " + vMinValue + " but not more than " + vMaxValue + ".");
				vNumberObj.focus();
				return false;
			}
		}
		else {
			alert("Field '" + vFriendlyFieldName + "' must be a numeric value between " + vMinValue + " and " + vMaxValue + ".");
			vNumberObj.focus();
			return false;
		}

	}
	else if ((vRequiredBool == 1) && (vNumberObj.value.length == 0)) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
		vNumberObj.focus();
		return false;
	}
	else {
		return true;
	}
}

// Function to validate phone numbers
// Usage: myValPhone([textbox as object], [friendly_field_name as string], [requiredvalue as bool])
function myValPhone(vTextObj, vFriendlyFieldName, vRequiredBool) {

	if ( ((vRequiredBool == 1) && (vTextObj.value.length > 0)) || ((vRequiredBool == 0) && (vTextObj.value.length > 0)) ) {

		var my_regex = /^(\d{3}\d{3}\d{4})$/;

		if (my_regex.test(vTextObj.value) != 1) {
			//alert("Field '" + vFriendlyFieldName + "' must be in the format XXX-XXX-XXXX.");
			alert("Field '" + vFriendlyFieldName + "' is invalid.");
			//vTextObj.focus();
			//alert("phone false");
			return false;
		}
		else {
			//alert("phone true")
			return true;
		}
	}
	else if ((vRequiredBool == 1) && (vTextObj.value.length == 0)) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
		//vTextObj.focus();
		//alert("phone1 false");
		return false;
	}
	else {
		//alert("default true");
		return true;
	}
}

// Function to validate date strings as MM-DD-YYYY or M-D-YYYY
// Usage: myValDate([textbox as object], [friendly_field_name as string], [strictformat as bool], [requiredvalue as bool])
function myValDate(vTextObj, vFriendlyFieldName, vStrictFormat, vRequiredBool) {

	if ( ((vRequiredBool == 1) && (vTextObj.value.length > 0)) || ((vRequiredBool == 0) && (vTextObj.value.length > 0)) ) {

		if (vStrictFormat == 1) {
			var my_regex = /^(\d{2}[-]\d{2}[-]\d{4})$/;
			var vMessageFormat = "MM-DD-YYYY";
		}
		else {
			var my_regex = /^(\d{1,2}[-]\d{1,2}[-]\d{4})$/;
			var vMessageFormat = "M-D-YYYY";
		}

		if (my_regex.test(vTextObj.value) != 1) {
			alert("Field '" + vFriendlyFieldName + "' must be in the format " + vMessageFormat + ".");
			vTextObj.focus();
			return false;
		}
		else {
			return true;
		}
	}
	else if ((vRequiredBool == 1) && (vTextObj.value.length == 0)) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
		vTextObj.focus();
		return false;
	}
	else {
		return true;
	}
}


// Function to validate email addresses
// Usage: myValEmail([textbox as object], [friendly_field_name as string], [strictformat as bool], [requiredvalue as bool])
function myValEmail(vTextObj, vFriendlyFieldName, vStrictFormat, vRequiredBool) {

	if ( ((vRequiredBool == 1) && (vTextObj.value.length > 0)) || ((vRequiredBool == 0) && (vTextObj.value.length > 0)) ) {

		if (vStrictFormat == 1) {
			var my_regex = /^[a-z0-9_\.\-\+]{2,}[@]{1}[a-z0-9_\.-]+\.[a-z]{2,4}$/;
		}
		else {
			var my_regex = /^[a-zA-Z0-9_\.\-\+]{2,}[@]{1}[a-zA-Z0-9_\.-]+\.[a-z]{2,4}$/;
		}

		if (my_regex.test(vTextObj.value) != 1) {
			alert("Field '" + vFriendlyFieldName + "' is invalid.");
			vTextObj.focus();
			return false;
		}
		else {
			return true;
		}
	}
	else if ((vRequiredBool == 1) && (vTextObj.value.length == 0)) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
		vTextObj.focus();
		return false;
	}
	else {
		return true;
	}
}

// Function to check equality of email addresses
// Usage: myEqlEmail([textbox as object],[textbox as object])
function myEqlEmail(vTextObj1,vTextObj2) {
	//alert("equality checking");

	if (vTextObj1.value == vTextObj2.value){
		//alert("Email id's match");
		return true;
	}
	else {
		alert("Email id\'s do not match");
		return false;
	}
}

// Function to validate currency
// Usage: myValCurrency([textbox as object], [friendly_field_name as string], [requiredvalue as bool])
function myValCurrency(vTextObj, vFriendlyFieldName, vRequiredBool) {

	if ( ((vRequiredBool == 1) && (vTextObj.value.length > 0)) || ((vRequiredBool == 0) && (vTextObj.value.length > 0)) ) {

		var my_regex = /^[0-9]{1,}[.]{1}[0-9]{2}$/;

		if (my_regex.test(vTextObj.value) != 1) {
			alert("Field '" + vFriendlyFieldName + "' is invalid.  Currency must be in the format X.XX");
			vTextObj.focus();
			return false;
		}
		else {
			return true;
		}
	}
	else if ((vRequiredBool == 1) && (vTextObj.value.length == 0)) {
		alert("Field '" + vFriendlyFieldName + "' is required.  Currency must be in the format X.XX");
		vTextObj.focus();
		return false;
	}
	else {
		return true;
	}
}

// Function to verify that a certain number of items are listed in a select-option list.  Takes a MinValue and MaxValue.
// Usage: myValSelOptItemCount([select-option as object], [friendly field name as string], [minvalue as int], [maxvalue as int], [required as bool])
function myValSelOptItemCount(vSelOptObj, vFriendlyFieldName, vMinValue, vMaxValue, vRequiredBool) {

	if ( ((vRequiredBool == 1) && (vSelOptObj.length > 0)) || ((vRequiredBool == 0) && (vSelOptObj.length > 0)) ) {

		if ( (vSelOptObj.length >= vMinValue) && (vSelOptObj.length <= vMaxValue) ) {
			return true;
		}
		else {
			if (vRequiredBool == 1) {
				alert("Field '" + vFriendlyFieldName + "' must contain at least " + vMinValue + " items but not more than " + vMaxValue + " items.");
			}
			else {
				alert("Field '" + vFriendlyFieldName + "' must contain no more than " + vMaxValue + " items.");
			}
			return false;
		}
	}
	else if ((vRequiredBool == 1) && (vSelOptObj.length == 0)) {
		if (vRequiredBool == 1) {
			alert("Field '" + vFriendlyFieldName + "' must contain at least " + vMinValue + " items but not more than " + vMaxValue + " items.");
		}
		else {
			alert("Field '" + vFriendlyFieldName + "' must contain no more than " + vMaxValue + " items.");
		}
		return false;
	}
	else {
		// alert("true");
		return true;
	}
}

// Function to validate a textarea by character
// Usage: myValWordCount([textbox as object], [friendly_field_name as string], [minlength as integer], [maxlength as integer], [requiredvalue as bool])
function myValWordCount(vTextObj, vFriendlyFieldName, vMinLength, vMaxLength, vSplitChar, vRequiredBool){

	if ( ((vRequiredBool == 1) && (vTextObj.value.length >= 0)) || ((vRequiredBool == 0) && (vTextObj.value.length >= 0)) ) {
	
	//alert(vTextObj.value.length);
	vTextObj = vTextObj.value.split(vSplitChar);
	//alert(vTextObj.length);
	
		if ((vTextObj.length < vMinLength) || (vTextObj.length > vMaxLength)) {
			if (vRequiredBool) {
				alert("Field '" + vFriendlyFieldName + "' must be at least " + vMinLength + " but not more than " + vMaxLength + " words.");
			}
			else {
				alert("Field '" + vFriendlyFieldName + "' must not be more than " + vMaxLength + " words.");
				//alert(vTextObj.length);
				return false;
			}
		}
		else {
			return true;
		}
	}
	else if ((vRequiredBool == 1) && (vTextObj.length <= 0)) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
		return false;
	}
		else {
			return true;
			 }
}

// Function to validate a select-option based on a given index
function myValSelOptByIndex(vSelOptObj, vFriendlyFieldName, vIndex) {

	if (vSelOptObj.selectedIndex == vIndex) {
		alert("Field '" + vFriendlyFieldName + "' is required.");
		return false;
	}
	else {
		return true;
	}
}


// Function to pop up a new browser window with the specified URL passed as a value from a select-option list
// Usage: mySelOptURLNewWindow([select-option as object], [reset list as boolean])
function mySelOptURLNewWindow(vSelOptObj, vResetList) {

	vSelIndex = vSelOptObj.selectedIndex;
	vSelValue = vSelOptObj.options[vSelIndex].value;

	if (vSelIndex != 0) {
		window.open(vSelValue);
		if (vResetList) {
			vSelOptObj.selectedIndex = 0;  // sets the drop down back to "--Please Select--"
		}
	}
}


// Function to pop up a simple JS window with no url or toolbar
// Also limited to one window by using window handle
function myPopUpSimple(vURL, vWidth, vHeight) {

	var whandle = "foo";

	// if the window is NOT closed, then set the focus
	if (whandle.closed == false) {
		whandle.focus();
	}
	// otherwise, open the window
	else {
		var winLeft = (screen.width - vWidth) / 2;
		var winTop = (screen.height - vHeight) / 2;
		whandle = window.open(vURL, 'mywin', 'dependent=yes, width='+vWidth+', height='+vHeight+', top='+winTop+', left='+winLeft+', menubar=no, resizable=no ,scrollbars=no, status=yes');
		whandle.focus();
	}
}


function LPad(String,Length,PadChar) {
 // **********************************************************
 // Placed in the public domain by Affordable Production Tools
 // April 1, 1998
 // Web site: http://www.aptools.com/
 //
 // December 2, 1998 -- Modified to allow specification of
 // pad character.
 //
 // This function accepts a number or string, and a number
 // specifying the desired length. If the length is greater
 // than the length of the value passed, the value is padded
 // with spaces (default) or the specified pad character
 // to the length specified.
 //
 // The function is useful in right justifying numbers or
 // strings in HTML form fields.
 // **********************************************************
	String += "";       // Force argument to string.
	Length += "";       // Force argument to string.
	PadChar += "";      // Force argument to string.
	if((PadChar == "") || (!(PadChar.length == 1)))
		PadChar = " ";

	var Count = 0;
	var PadLength = 0;
	Length = parseInt(0 + Length,10);
	if(Length <= String.length) // No padding necessary.
		return(String);

	PadLength = Length - String.length;

	for(Count = 0; Count < PadLength; Count++)
		String = PadChar + String;

	return(String);
}

function RPad(String,Length,PadChar) {
// RPad version of above by John Browne

	String += "";       // Force argument to string.
	Length += "";       // Force argument to string.
	PadChar += "";      // Force argument to string.
	if((PadChar == "") || (!(PadChar.length == 1))) {
		PadChar = " ";
	}

	var Count = 0;
	var PadLength = 0;
	Length = parseInt(0 + Length,10);
	if(Length <= String.length) { // No padding necessary.
		return(String);
	}

	PadLength = Length - String.length;

	for(Count = 0; Count < PadLength; Count++) {
		String = String + PadChar;
	}

	return(String)
}


// ******************************
// BEGIN ARRAY AND LIST FUNCTIONS
// ******************************

// Convert a particular column of a JavaScript array to a hidden field for submission
// Usage: myArray2HField([vArray as obj], [vColumnNum as int], [vFormObj as object], [vHFieldName as string])
function myArray2HField(vArray, vColumnNum, vFormObj, vHFieldName) {

	vTempArray = new Array;

	//for (i = 0; i < p_array_right.length; i++) {
	for (i = 0; i < vArray.length; i++) {
		vTempArray[i] = vArray[i][vColumnNum];
	}

	vArrayString = vTempArray.toString();

	var input = document.createElement('input');
	input.type = "hidden";
	input.name = vHFieldName;
	input.value = vArrayString;
	// document.forms[0].appendChild(input);
	vFormObj.appendChild(input);

}


// Move items from JS array and JS list
function moveitems(vFromSelOptObj, vToSelOptObj, vFromArray, vToArray) {

	// If nothing is selected, do nothing
	if (vFromSelOptObj.selectedIndex == -1) {
		return;
	}

	// add to the dest array
	for( var i = 0; i < vFromSelOptObj.options.length; i++ ) {
		if ( vFromSelOptObj.options[i] != null && ( vFromSelOptObj.options[i].selected == true) ) {
			//alert(i + " " + vFromSelOptObj.options[i].value);
			//vToArray.push(vFromArray[i]);
			vToArray.push(vFromArray[vFromSelOptObj.options[i].value]);
		}
	}

	var j = 0;

	// remove from the source array - start from bottom and work your way up
	for( var i = vFromSelOptObj.options.length; i >= 0; i-- ) {
		if ( vFromSelOptObj.options[i] != null && ( vFromSelOptObj.options[i].selected == true) ) {
			//vFromArray.splice(vFromSelOptObj.options[i].value, 1);
			//vFromArray.splice(i, 1);
			vFromArray.splice(vFromSelOptObj.options[i].value, 1);
		}
		else {
			j++;
		}
	}

	//populateList(vFromSelOptObj, vFromArray);
	//populateList(vToSelOptObj, vToArray);

}



// Sorting functions for Javascript array columns
// 10 columns should be enough, string and numeric
function ByCol00s(a, b) {
	if (a[0] < b[0]) return -1;
	if (a[0] > b[0]) return 1;
	return 0;
}

function ByCol00n(a, b) {
	return (a[0] -b[0]);
}

function ByCol01s(a, b) {
	if (a[1] < b[1]) return -1;
	if (a[1] > b[1]) return 1;
	return 0;
}

function ByCol01n(a, b) {
	return (a[1] -b[1]);
}

function ByCol02s(a, b) {
	if (a[2] < b[2]) return -1;
	if (a[2] > b[2]) return 1;
	return 0;
}

function ByCol02n(a, b) {
	return (a[2] -b[2]);
}

function ByCol03s(a, b) {
	if (a[3] < b[3]) return -1;
	if (a[3] > b[3]) return 1;
	return 0;
}

function ByCol03n(a, b) {
	return (a[3] -b[3]);
}

function ByCol04s(a, b) {
	if (a[4] < b[4]) return -1;
	if (a[4] > b[4]) return 1;
	return 0;
}

function ByCol04n(a, b) {
	return (a[4] -b[4]);
}

function ByCol05s(a, b) {
	if (a[5] < b[5]) return -1;
	if (a[5] > b[5]) return 1;
	return 0;
}

function ByCol05n(a, b) {
	return (a[5] -b[5]);
}

function ByCol06s(a, b) {
	if (a[6] < b[6]) return -1;
	if (a[6] > b[6]) return 1;
	return 0;
}

function ByCol06n(a, b) {
	return (a[6] -b[6]);
}

function ByCol07s(a, b) {
	if (a[7] < b[7]) return -1;
	if (a[7] > b[7]) return 1;
	return 0;
}

function ByCol07n(a, b) {
	return (a[7] -b[7]);
}

function ByCol08s(a, b) {
	if (a[8] < b[8]) return -1;
	if (a[8] > b[8]) return 1;
	return 0;
}

function ByCol08n(a, b) {
	return (a[8] -b[8]);
}

