// *******************************************
// * DEL SCRIPTS
// *******************************************

// *******************************************
// * CHECK WEBFORMS FOR TERM SELECTION
// *******************************************

function validate_Form_Term() 
{
    var returnValue = true;
	var myOption = -1;
	for (i = 0; i < document.form.term.length; i++)
	{
		if (document.form.term[i].checked)   
		{ 
		    myOption = i; 
		}	
	}
	if (document.form.term.checked) 
	{
		myOption = i;
	}
	if (myOption == -1) 
	{
		alert("Please select an Intended Term.");
      	returnValue = false; 
	}
	return returnValue;
}

// *******************************************************
// * TAB CURSOR TO NEXT FIELD WHEN MAXLENGTH REACHED
// *******************************************************

function moveOnMax(field,nextFieldID)
{
  if (field.value.length >= field.maxLength)
  {
    document.getElementById(nextFieldID).focus();
  }
}

// *******************************************************
// * ENABLE/DISABLE STATE SELECTION FOR COUNTRY
// *******************************************************

function enableStateSelection() 
{
    if (document.cat_apply.country.value != "USA")
    {
        document.cat_apply.state.selectedIndex = 0;
        document.cat_apply.state.disabled = true;
    }
    else 
    {
        document.cat_apply.state.disabled = false;
    }
}

// *******************************************************
// * CLEAR/DISABLE/ENABLE INTERNATIONAL/DOMESTIC PHONE
// *******************************************************

function protectPhoneFields() {

if (document.cat_apply.phonetype[0].checked)   {
document.cat_apply.intphone.value = "";
document.cat_apply.intphone.disabled = true;
document.cat_apply.areacode01.disabled = false; 
document.cat_apply.areacode02.disabled = false;
document.cat_apply.prefix01.disabled = false;
document.cat_apply.prefix02.disabled = false;
document.cat_apply.linenumber01.disabled = false;
document.cat_apply.linenumber02.disabled = false;
document.cat_apply.extension01.disabled = false;
document.cat_apply.extension02.disabled = false;
}

if (document.cat_apply.phonetype[1].checked)   {
document.cat_apply.areacode01.value = "";
document.cat_apply.areacode02.value = "";
document.cat_apply.prefix01.value = "";
document.cat_apply.prefix02.value = "";
document.cat_apply.linenumber01.value = "";
document.cat_apply.linenumber02.value = "";
document.cat_apply.extension01.value = "";
document.cat_apply.extension02.value = "";
document.cat_apply.areacode01.disabled = true;
document.cat_apply.areacode02.disabled = true;
document.cat_apply.prefix01.disabled = true;
document.cat_apply.prefix02.disabled = true;
document.cat_apply.linenumber01.disabled = true;
document.cat_apply.linenumber02.disabled = true;
document.cat_apply.extension01.disabled = true;
document.cat_apply.extension02.disabled = true;
document.cat_apply.intphone.disabled = false;
}
}

// *******************************************************
// * VALIDATE THE MASTER FORM DATA
// *******************************************************

function validate_Form_Data() {
  
    var returnValue = true;
    var errorMessage = 'The required information is incomplete or contains errors:\n\n';
	var validDomesticChars = "0123456789";
	var validInternationalChars = "+()-*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var validZipCodeChars = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var invalidEntry = 5551212;
	var myPhoneType = -1;
	var getDomesticPrimaryPhone = ((document.cat_apply.areacode01.value) + (document.cat_apply.prefix01.value) + (document.cat_apply.linenumber01.value));
	var getInternationalPhone = document.cat_apply.intphone.value;
   	var getDomesticAlternatePhone = ((document.cat_apply.areacode02.value) + (document.cat_apply.prefix02.value) + (document.cat_apply.linenumber02.value));
    var promptErrorMessage = false; //set the initial value for the invalid character loop check
	var aErrors = ""; //return Primary Phone Number Invalid Characters
	var bErrors = ""; //return Alternate Phone Number Invalid Characters
	var cErrors = ""; //return International Phone Number Invalid Characters
   
    //First Name
	if (document.cat_apply.first_name.value == "" ) { errorMessage += ' * Please enter your First Name\n'; returnValue = false; }
	//Last Name
	if (document.cat_apply.last_name.value == "" ) { errorMessage += ' * Please enter your Last Name\n'; returnValue = false; } 
	//Address
	if (document.cat_apply.street.value.length < 5) { errorMessage += ' * Please enter a longer Street Address\n'; returnValue = false; } 
	//City
	if (document.cat_apply.city.value == "" ) { errorMessage += ' * Please enter a City\n'; returnValue = false; }
    //Country
    if (document.cat_apply.country.value == "" ) { errorMessage += ' * Please choose a Country\n'; returnValue = false; }
    if (document.cat_apply.country.value == "USA")
    {
       if(document.cat_apply.state.value == "" ) {
            errorMessage += ' * Please choose a State\n';
            returnValue = false;
        }
    }
    // Zip Code
	if(document.cat_apply.zip.value == "" ) { errorMessage += ' * Please enter a Zip\n'; returnValue = false; }
	// START Loop to check for space in zip code
	for (i=0; i<document.cat_apply.zip.value.length; i++) {
	    if ((validZipCodeChars.indexOf(document.cat_apply.zip.value.charAt(i)) == -1)) {
	        errorMessage += ' * Please enter a valid Zip\n'; returnValue = false;
	    }
	}
	//Consent
	if(document.cat_apply.consent.checked == false ) { errorMessage += ' * Please check I Agree \n'; returnValue = false; }
	// START Loop to check if Domestic/International Radio Button is selected
	for (i=0; i<document.cat_apply.phonetype.length; i++) {
		if (document.cat_apply.phonetype[i].checked)   {
		myPhoneType = i;
			// Check to see if Domestic Phone is checked
			if (document.cat_apply.phonetype[0].checked)    {
					if (getDomesticPrimaryPhone.length < 10) {
					errorMessage += ' * Please enter a valid Primary Phone Number length\n';
      				returnValue = false; 
					} //end domestic length check
					if ((document.cat_apply.prefix01.value) + (document.cat_apply.linenumber01.value) == invalidEntry) {
					errorMessage += ' * Invalid Primary Phone Number : 555-1212 is not permissible \n';
      				returnValue = false; 
					} //end xxx-555-1212 check
					else { 
					for (i = 0; i < getDomesticPrimaryPhone.length; i++) {
					    if((validDomesticChars.indexOf(getDomesticPrimaryPhone.charAt(i)) == -1))  
						 {  
								var a = getDomesticPrimaryPhone.charAt(i);
								aErrors += a;
								returnValue = false;
								promptErrorMessage = true;
								}//end returnValue = false
							}// end for loop digit check
							if (promptErrorMessage == true) {
								errorMessage += ' * Invalid characters in Primary Phone Number : ' + aErrors + '\n';
								}// end promptMessage
				} // end domestic phone else
				if(getDomesticAlternatePhone.length != "") {
		if(getDomesticAlternatePhone.length < 10){
					errorMessage += ' * Please enter a valid Alternate Phone Number length\n';
      				returnValue = false;
		}// end getDomesticAlternatePhone length check 
		if ((document.cat_apply.prefix02.value) + (document.cat_apply.linenumber02.value) == invalidEntry) {
					errorMessage += ' * Invalid Alternate Phone Number : 555-1212 is not permissible \n';
      				returnValue = false; 
					} //end xxx-555-1212 check
		else {
	for (i = 0; i < getDomesticAlternatePhone.length; i++) {   
						if((validDomesticChars.indexOf(getDomesticAlternatePhone.charAt(i)) == -1)) {  
								var b = getDomesticAlternatePhone.charAt(i);
								bErrors += b;
								returnValue = false;
								promptErrorMessage = true;
								}//end returnValue = false
							}// end for loop digit check
							if (promptErrorMessage == true) {
								errorMessage += ' * Invalid characters in Alternate Phone Number : ' + bErrors + '\n';
								}// end promptMessage
							}// end secondary phone else
	}// end if to check if Alternate Phone number was inititated 
			} // end phonetype[0].checked
			if (document.cat_apply.phonetype[1].checked) {
				if (document.cat_apply.intphone.value.length < 10) {
				errorMessage += ' * Please enter a valid International Phone Number \n';
      			returnValue = false; 
				} // end international length check
				else { 
						 for (i = 0; i < getInternationalPhone.length; i++) {   
								if(validInternationalChars.indexOf(getInternationalPhone.charAt(i)) == -1) {  
								var c = getInternationalPhone.charAt(i);
								bErrors += c;
								returnValue = false;
								promptErrorMessage = true;
								}//end returnValue = false
							}// end for loop digit check
							if (promptErrorMessage == true) {
								errorMessage += ' * Invalid characters in International Phone Number : ' + cErrors + '\n';
								}// end promptMessage
				} // end international phone else
			} // end phonetype[1].checked
		} // end myPhoneType
	}// end For Loop
	if (myPhoneType == -1) {
		errorMessage += ' * Please choose a Primary Phone Origin : Domestic or International \n';
      	returnValue = false; 
		}// end myPhoneType
	var myOption = -1;
	for (i=0; i<document.cat_apply.ContactTime.length; i++) {
	if (document.cat_apply.ContactTime[i].checked)   {
	myOption = i;
	}	
  }
	// END Loop to check if Domestic/International Radio Button is selected
	//Preferred Contact Time
	if (myOption == -1) { errorMessage += ' * Please choose a Preferred Contact Time\n'; returnValue = false; }
	//Email
	if (document.cat_apply.email.value.search("@") == -1 || document.cat_apply.email.value.search("[.*]") == -1) { errorMessage += ' * Please enter a valid E-mail Address\n'; returnValue = false; }
	//Preferred Method of Contact
	if (document.cat_apply.Preferred_Method.value == "" ) {errorMessage += ' * Please choose a Preferred Method of Contact \n'; returnValue = false; }
	//Affiliation Acknowledgement 
	if (document.cat_apply.affiliationAcknowledgement.value == "" ) { errorMessage += ' * Please choose an Affiliation Acknowledgement\n'; returnValue = false; }
	//Affiliation Name/Code
	if (document.cat_apply.txtAffiliationName.value == "" ) { errorMessage += ' * Please choose an Affilitation Code\n'; returnValue = false; }
	//How Did You Hear About Us
	if (document.cat_apply.hear.value == "" ) { errorMessage += ' * Please choose How You Heard About Us\n'; returnValue = false; }
	if (!returnValue) { alert(errorMessage);     
    }
  return returnValue;
  }

// *******************************************************
// * OPEN AFFILIATION CODE SEARCH TOOL WINDOW
// *******************************************************

function openwindow() { 
    window.open("/DeLAffiliations/LookUpMoreInfo.aspx","mywindow","menubar=0,resizable=1,width=750,height=800"); 
    }
    
// *******************************************************
// * OPEN "WHAT IS THIS?" AFFILIATION CODE POP-UP
// *******************************************************
		
function openAffiliation() { 
    window.open("/DeLAffiliations/AffiliationCode.html","", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=425, height=375") 
    }

// *************************************************************
// * CLEAR AFFILIATION CODE VALUES IF SELECTEDINDEX CHANGED 
// *************************************************************
		
function autoSelect(attribute) {
		if (document.cat_apply.affiliationAcknowledgement[0].selected == true) {
		    document.cat_apply.txtAffiliationName.value = "";
		    document.cat_apply.txtBannerAttributeCode.value = "";
	    }
		if(document.cat_apply.affiliationAcknowledgement[1].selected == true) {
		    document.cat_apply.txtAffiliationName.value = "No Affiliation";
		    document.cat_apply.txtBannerAttributeCode.value = attribute;
	    }
		if(document.cat_apply.affiliationAcknowledgement[2].selected == true) {
		    document.cat_apply.txtAffiliationName.value = "";
		    document.cat_apply.txtBannerAttributeCode.value = "";
		    openwindow();
	    }
	}

// *******************************************************
// * MAY BE OBSOLETE
// *******************************************************
	
function submitform()
     {
       document.Dform.submit();
     }
