// *************************************************************
// Title: validate.js
// Purpose: General form validation functions
// Comments:
// Modifications: date & author
//    4.10.2008: jmusick - initial validation functions
//    9.25.2008: jmusick - adding dynamic select javascript
//    10.4.2008: jmusick - adding JS to accomodate tertiary select
// Usage Locations:
//    www.genetalent.com microsite is using this. 
//    Many of the salesforce.com 'form submit' pages use this.
//    Barring other considerations, choose to refer to this 
//    version so as to reduce code duplication.
// *************************************************************

function fnValidateCustom(oForm, arfn)
{
    var error = "";
   
    if(typeof arfn != 'undefined' && arfn.length>0)
    {
        for(var i = 0; i < arfn.length; i++)
        {
            var thisErr = arfn[i](oForm);
            if(thisErr.length > 0)
                error = error + "- " + thisErr;
        }
    }

    if(oForm)
    {
        var nMax = oForm.elements.length; 
		for(var i = 0; i < nMax; i++)
        {
            var sName = 'fnValidate' + i;
            if(document[sName])
            {
                var func = document[sName];
                if(func != null)
                {
                    error = error + func(oForm);
                }
            }            
        }

        if(error.length > 0)
        {
            alert("This form cannot be submitted because of the following errors:\n\n" + error);
			return false;
        }
        else
            return true;
    }
}

function fnValidate(oForm)
{
  return fnValidateCustom(oForm, []);
}

function fnIsEmpty(elem){
	//alert('fnIsEmpty');
	if(elem.value.length == 0){
		return true;
	}
	return false;
}

function fnIsAllAlpha(elem){
	//alert('fnIsAllAlpha');
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function fnCheckEmail (sInputEmail) {
	//alert('fnCheckEmail');
	var strng=sInputEmail;
	var error="";
	if (strng == "") {
	   error = "Email: You didn't enter an email address.\n";
	}
	else{
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(strng))) { 
		   error = "Email: Please enter a valid email address.\n";
		}
		else {
		//test email for illegal characters
		   var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
			 if (strng.match(illegalChars)) {
			  error = "Email: The email address contains illegal characters.\n";
		   }
		}
	}
	return(error);    
}
function fnCheckDate (iMonthValue, iYearValue) {
	// alert('fnCheckDate');
	// Only checking for empty string now, build out as appropriate.
	var error="";
	var sMonthError = "Date: Please choose a month value.";
	var sYearError = "Date: Please choose a year value.\n";
	if (iMonthValue == "")
		error += sMonthError;
	if (iYearValue == "")
	{
		if(error.length>0)
			error += "\n";
		error += sYearError;
	}
	return(error);
}

// ***********************************************************************
//  Function: populateDependentSelect
//  Author: jmusick
//  Last Modified: 10.04.2008
//  Purpose: This function populates a dependent select element
//      based on values that are set in an array on the page and 
//      passed in.
//    Currently it also hides the element when not in use, by
//    using CSS classes, dependentselect_shown anddependentselect_hidden
// ***********************************************************************

function populateDependentSelect(oSelectToBePopulated, aOptionValues){
	oSelectToBePopulated.options.length=0;	
	// if no dependent array passed in (or undefined), make sure the field is invisible...
	if(!aOptionValues){
		//alert('no dependent array passed in');
		if(cssjs('check',oSelectToBePopulated,'dependentselect_shown')){
			cssjs('swap',oSelectToBePopulated,'dependentselect_shown','dependentselect_hidden')
		}
		return;
	}
	// so we know that there is a dependent array - make sure the field is showing...
	if(cssjs('check',oSelectToBePopulated,'dependentselect_hidden')){
		//alert('dependent array passed in, but field is currently hidden, show it!');
		cssjs('swap',oSelectToBePopulated,'dependentselect_hidden','dependentselect_shown')
	}

	oSelectToBePopulated.options.length=aOptionValues.length;
	for(var i=0;i<aOptionValues.length;i++)
	{
		oSelectToBePopulated.options[i].text=aOptionValues[i];
		oSelectToBePopulated.options[i].value=aOptionValues[i];
		
	}
}

// ***********************************************************************
//  Function: fFindSelectValues
//  Author: jmusick
//  Last Modified: 10.04.2008
//  Purpose: This is a helper function for populateDependentSelect. It is
//    used to create the values passed into the other function - because
//    for tertiary arrays it is necessary to know both primary and secondary
//    values.
//    ** It also hides the tertiary element in cases that populateDependentSelect
//       might not catch.
// ***********************************************************************
function fFindSelectValues(iIndex,aValues,iFocusIndex){
	//alert('Function: fFindSelectValues');
	if(typeof iFocusIndex == 'undefined' || iFocusIndex.length==0){
		// this code block entered when the primary select is chosen. Since at this point there
		//   are now tertiarty values, let's check for the visibility and length - and reset both.
		// this code block ALSO entered when the secondary select is in the "please choose" position,
		// in which case we also are resetting the tertiary select.
		if(typeof oTertiarySelectObject != 'undefined'){
			oTertiarySelectObject.options.length=0;
			if(cssjs('check',oTertiarySelectObject,'dependentselect_shown')){
				cssjs('swap',oTertiarySelectObject,'dependentselect_shown','dependentselect_hidden')
			}					
		}
	
		return(aValues[iIndex]);
	}
	else{
		// this code block indicates that we are dealing with a secondary select. It might or might 
		//   not have a dependent (tertiary) select option
		if(aValues[iIndex].aSubFocusArray[iFocusIndex]){
			return(aValues[iIndex].aSubFocusArray[iFocusIndex]);
		}
		else{
			if(typeof oTertiarySelectObject != 'undefined'){
				//there isn't any tertiary select - so let's make sure the visibility/length are correct.
				if(cssjs('check',oTertiarySelectObject,'dependentselect_shown')){
					cssjs('swap',oTertiarySelectObject,'dependentselect_shown','dependentselect_hidden')
				}				
			}
		}
	}
}

/*
 * Function: cssjs
 * written by Christian Heilmann (http://icant.co.uk)
 * eases the dynamic application of CSS classes via DOM
 * parameters: action a, object o and class names c1 and c2 (c2 optional)
 * actions: swap exchanges c1 and c2 in object o
 *			add adds class c1 to object o
 *			remove removes class c1 from object o
 *			check tests if class c1 is applied to object o
 * example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
 */
function cssjs(a,o,c1,c2)
{
	switch (a){
		case 'swap':
			o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
		case 'add':
			if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(o.className)
		break;
	}
}