var iValidCheck;
// ----------------------------------------------------------------------------
function IsValidEmail(EmailInput)
{	
	// Function checks to ensure email address is valid according to our validation spec.
	var field = EmailInput; 
	var str = field.value; 
	if (window.RegExp) 
	{ 	var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"; 
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; 
		var reg1 = new RegExp(reg1str); var reg2 = new RegExp(reg2str); 
		if (!reg1.test(str) && reg2.test(str)) 
		{ return true; } 
		field.focus(); 
		field.select(); 
		return false; 
	} 
	else 
	{ 
	if(str.indexOf("@") >= 0) 
		{ return true; } 
	else 
		{ 
		field.focus(); 
		field.select(); 
		return false; 
		} 
	} 
}

// -----------------------------------------------------------------------------
function IsValidZipCode(ZipCodeInput)
{	// functioon checks to be sure zip code has numeric values and is 5 digits
	if  (numericInputPassed(ZipCodeInput.value) && (ZipCodeInput.value.length==5))
		return true;
	else
		{
		ZipCodeInput.select();
	   	return false; 
	   	}
			
}

// -----------------------------------------------------------------------------
function IsValidCanadianZip(ZipCodeInput){ // CANADIAN CODES ONLY
    strlen = ZipCodeInput.value.length;  
    if (strlen != 6) {return false;} 
    ZipCodeInput=ZipCodeInput.value.toUpperCase();        // in case of lowercase characters
    // Check for legal characters in string - note index starts at zero
    if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(ZipCodeInput.charAt(0)) < 0) {return false;}
    if ('0123456789'.indexOf(ZipCodeInput.charAt(1)) < 0) {return false;}
    if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(ZipCodeInput.charAt(2)) < 0) {return false;}
    if ('0123456789'.indexOf(ZipCodeInput.charAt(3)) < 0) {return false;}
    if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(ZipCodeInput.charAt(4)) < 0) {return false;}
    if ('0123456789'.indexOf(ZipCodeInput.charAt(5)) < 0) {return false;}
    // if we get to here, all is well.
    return true; 
}

// -----------------------------------------------------------------------------
//This is for inputs that can take both US ZIP and Canadain postal code
function IsValidZipPostal(ZipCodeInput)
{
	//ZipCodeInput.value = Trim(ZipCodeInput.value)
	switch(ZipCodeInput.value.length)
	{
		case 5:
			return IsValidZipCode(ZipCodeInput);
			break;
		case 6:
			return IsValidCanadianZip(ZipCodeInput);
			break;
		default:
			return false;
			break;
	}
}

// -----------------------------------------------------------------------------
function CBoxGrpSet(inForm, cbPref) {
	// given a group of checkbox objects that are named with a similar string,
	// this function loops through all of the elements in the document form
	// looking for controls with this similar string in their names.
	// When if finds one, it checks to see if it's checked; if so, it increments
	// a count.
	// Finally it returns the count it found.
	// Note: if you have a non-checkbox control that has the similar string in
	// it, when this function tries to look at its checked property and it doesn't
	// have one, it'll throw an error.  Very ugly.  Be careful.
	var num = inForm.elements.length;
	var gCount = 0;
	var cCount = 0;
	var disabledCount = 0;
	if (cbPref.length == 0) {
		// if caller passes in a blank prefix, there's nothing to check so
		// get out!
		return 0;
	}
	for (var i = 0; i < num ; i++) {
		if (inForm.elements[i].name.indexOf(cbPref) != -1) {
			// this is a check box.
			gCount = gCount + 1;
			if (inForm.elements[i].disabled) {
				disabledCount = disabledCount + 1
			}
			else {
				if (inForm.elements[i].checked) {
					cCount = cCount + 1;
				}
			}
		}
	}
	if (disabledCount == gCount) {
		// they're all disabled, so basically, the pass.
		return gCount;
	}
	else {
		// at least some were not disabled so at least one should be set.
		return cCount;
	}
}

function radioPassed(radioObject) {
	// It is possible that some of the radios are disabled,
	// and some are not. (HA WebPts Bug# 443)
	
	// If some radios are enabled and NONE are checked,
	// we return the index number of the first unchecked
	// radio that is enabled. Return -1 if a radio was checked,
	// or all radios were disabled.
	
	var iCount = radioObject.length;
	
	// first see if ALL the radios are disabled, if so return true.
	var iDisabledCount = 0;
	var iFirstEnabledIndex = -1;	
	for (var i=0;i < iCount; i++)
	{
		if (radioObject[i].disabled)
		{
			iDisabledCount++;
		}
		else
		{
			if (-1 == iFirstEnabledIndex)
				iFirstEnabledIndex = i;	
		}
	}
	if ( iDisabledCount == iCount )
		return -1;

	// If we get this far, then at least some of the radios are enabled.
	// Now check see if any of them are CHECKED.
	for (var i=0;i < iCount; i++)
	{
		if (radioObject[i].checked)
			return -1;
	}

	// if we get this far, there are radios enabled, but none are checked.
	return iFirstEnabledIndex;
}

function dropDownPassed(dropObject) {
	// loop through the drop object passed in and see if any of them
	// are selected.  If so, return true.

	if (dropObject.disabled) {
	    // automatically return true since it's disabled and user CAN'T answer it.
	    return true;
	}

	for (var i=0;i < dropObject.length; i++) {

		if (dropObject.options[i].selected && dropObject.options[i].value != "") {

			return true;
		}
	}
	return false;
}

function checkBoxPassed(inform, chPref) {
	return (CBoxGrpSet(inform, chPref) > 0);
}

function inputIsNumeric(inStr) {
	for (var i = 0; i != inStr.length; i++) {
		aChar = inStr.substring(i, i+1);
		if (aChar < "0" || aChar > "9") {
			return false
		}
	}
	// If we get to here, all is well
	return true;
}

function numericInputPassed(numInput) {
	if (numInput.length == 0) {
		// no length
		return false;
	}
	if (!inputIsNumeric(numInput)) {
		// not numeric
		return false;
	}
	// if we get to here, all is well
	return true;
}

function numericPosInputPassed(numInput) {
	if (numInput.length == 0) {
		// no length
		return false;
	}
	if (!inputIsNumeric(numInput)) {
		// not numeric
		return false;
	} else {
		// numeric, but must be greater than zero
		if( numInput <= 0 ) {
			return false;
		}
	}

	// if we get to here, all is well
	return true;
}

function textInputPassed(txtInput) {
	if (txtInput.length == 0) {
		// no length
		return false;
	}
	// if we get to here, all is well
	return true;
}


