// JavaScript Document

// web-to-leads functions: start

		function jsCompanyUpdate(lsSelectedValue) {

			if (frmInput.cover_cost.options[frmInput.cover_cost.selectedIndex].value == "No") {
				frmInput.company.value = "Purple Direct Student Prospects"; 
			}
			else {
				switch (lsSelectedValue) {
						case "Antigua/Barbuda":
						case "Argentina":
						case "Bahamas":
						case "Barbados":
						case "Belize":
						case "Bolivia":
						case "Chile":
						case "Colombia":
						case "Costa Rica":
						case "Cuba":
						case "Dominica":
						case "Dominican Republic":
						case "Ecuador":
						case "El Salvador":
						case "Grenada":
						case "Guatemala":
						case "Guyana":
						case "Haiti":
						case "Honduras":
						case "Jamaica":
						case "Japan":				
						case "Korea, North":				
						case "Korea, South":
						case "Mexico":
						case "Nicaragua":
						case "Panama":
						case "Paraguay":
						case "Peru":
						case "Saint Kitts/Nevis":
						case "Saint Lucia":
						case "Saint Vincent/Grenadines":
						case "Suriname":
						case "Trinidad/Tobago":
						case "Uruguay":
						case "Venezuela":				
							frmInput.company.value = "Blue Direct Student Prospects"; 
							break;
						case "Australia":
						case "Bangladesh":
						case "Bhutan":
						case "Burma": 
						case "China":
						case "Hong Kong":						
						case "India":
						case "Kiribati":
						case "Macao":
						case "Marshall Islands":
						case "Mongolia":
						case "Nauru":
						case "Nepal":
						case "New Zealand":				
						case "Pakistan":
						case "Papua New Guinea":
						case "Samoa":
						case "Solomon Islands":
						case "Sri Lanka":						
						case "Taiwan":
						case "Tuvalu":
						case "Vanuatu":				
							frmInput.company.value = "Red Direct Student Prospects"; 
							break;
    					case "Brunei":
    					case "Cambodia":
    					case "Denmark":
    					case "Fiji":
						case "Finland":
						case "Iceland":
    					case "Indonesia":
    					case "Laos":
    					case "Malaysia":
    					case "Micronesia":
    					case "Norway":
    					case "Palau":			
    					case "Philippines":
    					case "Singapore":								
						case "Sweden":
						case "Thailand":
						case "Vietnam":																		
							frmInput.company.value = "White Direct Student Prospects"; 
							break;													
						case "Afghanistan":
						case "Albania":
						case "Algeria":
						case "Andorra":
						case "Angola":
						case "Armenia":
						case "Austria":
						case "Azerbaijan":
						case "Bahrain":
						case "Belarus":
						case "Belgium":
						case "Benin":
						case "Bosnia/Herzegovina":
						case "Botswana":
						case "Brazil":
						case "Bulgaria":
						case "Burkina Faso":
						case "Burundi":
						case "Cameroon":
						case "Canada":
						case "Cape Verde":
						case "Central African Republic":
						case "Chad":
						case "Comoros":
						case "Congo (Brazzaville)":
						case "Congo (Kinshasa)":
						case "Cote D'Ivoire":
						case "Croatia":
						case "Cyprus":
						case "Czech Republic":
						case "Djibouti":
						case "Egypt":
						case "Equatorial Guniea":
						case "Eritrea":
						case "Estonia":
						case "Ethiopia":
						case "France":
						case "Gabon":
						case "Gambia":
						case "Georgia":
						case "Germany":
						case "Ghana":
						case "Greece":
						case "Guinea":
						case "Guinea-Bissaau":
						case "Hungary":
						case "Iran":
						case "Iraq":
						case "Ireland":
						case "Israel":
						case "Italy":
						case "Jordan":
						case "Kazakhstan":
						case "Kenya":
						case "Kuwait":
						case "Kyrgyzstan":
						case "Latvia":
						case "Lebanon":
						case "Lesotho":
						case "Liberia":
						case "Libya":
						case "Liechtenstein":
						case "Lithuania":
						case "Luxembourg":
						case "Macedonia":
						case "Madagascar":
						case "Malawi":
						case "Maldives":
						case "Mali":
						case "Malta":
						case "Mauritania":
						case "Mauritius":
						case "Moldova":
						case "Monaco":
						case "Morocco":
						case "Mozambique":
						case "Namibia":
						case "Netherlands":
						case "Niger":
						case "Nigeria":
						case "Oman":
						case "Poland":
						case "Portugal":
						case "Qatar":
						case "Romania":
						case "Russia":
						case "Rwanda":
						case "San Marino":
						case "Sao Tome & Principe":
						case "Saudi Arabia":
						case "Senegal":
						case "Seychelles":
						case "Sierra Leone":
						case "Slovakia":
						case "Slovenia":
						case "Somalia":
						case "South Africa":
						case "Spain":
						case "Sudan": 
						case "Swaziland":
						case "Switzerland":
						case "Syria":
						case "Tajikistan":
						case "Tanzania":
						case "Togo":
						case "Tonga":
						case "Tunisia":
						case "Turkey":
						case "Turkmenistan":
						case "Uganda":
						case "Ukraine":
						case "United Arab Emirates":
						case "United Kingdom":
						case "United States":
						case "Uzbekistan":
						case "Vatican City (Holy See)":
						case "Yemen":
						case "Yugoslavia":
						case "Zambia":
						case "Zimbabwe":
							frmInput.company.value = "Yellow Direct Student Prospects"; 
							break;	
						default:
							frmInput.company.value = "Unknown Student Prospects"; 
							break;
				} // switch
			} // if

			// display the company under certain circumstances
			if (frmInput.email.value.indexOf("acetta.com") != -1) 
				alert (frmInput.company.value);

		} //function

		//--------------------------------------------------------------------------------
		// Name:	doSubmit()
		// Desc:    
		//--------------------------------------------------------------------------------

		function doSubmit(){

			var strMsg = "";
			var objForm = document.frmInput;
			
			// check that required fields are populated
			if(objForm.first_name.value == ""){
				strMsg += "Given Name" + "\r"
			}
			
			if(objForm.last_name.value == ""){
				strMsg += "Family Name" + "\r";
			}
			
			if(objForm.email.value == ""){
				strMsg += "Email Address" + "\r";
			}	
			
			if(objForm.country.value == ""){
				strMsg += "Country" + "\r";
			}	
		
			if(objForm.citizenship.value == ""){
				strMsg += "Citizenship" + "\r";
			}	

			if(objForm.cover_cost.value == ""){
				strMsg += "Cover Cost" + "\r";
			}	
			
			// if missing, show message and exit
			if(strMsg != ""){
				alert("In order to continue, please supply: \r\r" + strMsg);
				return false;
			}	

			// validate format of fields
			if(isEmail(objForm.email) == false){
				strMsg += "E-mail (format: user@domain.xxx)" + "\r";
			}	

			if(isDate(objForm.birthdate, true) == false){
				strMsg += "Birth Date (format: mm/dd/yyyy)" + "\r";
			}	
			
			if(isDate(objForm.startdate, false) == false){
				strMsg += "Expected Start Date (format: mm/dd/yyyy)" + "\r";
			}	
			

			// if invalid, show message and exit
			if(strMsg != ""){
				alert("In order to continue, please correct: \r\r" + strMsg);
				return false;
			}	

			jsCompanyUpdate(frmInput.citizenship.options[frmInput.citizenship.selectedIndex].value);


			return true;
		} 

		//--------------------------------------------------------------------------------
		// Name:	isDate(objInput, blnIsBirthdate)
		// Desc:    
		//--------------------------------------------------------------------------------

		function isDate(objInput, blnIsBirthdate){

			try{
			
				var strDate = trim(objInput.value);
			
				if(strDate.length < 1){	
					return true;
				}
				
				var d;
				var s;
				var astrDate;
				var astrMonth = new Array(12);
				var astrSeparator = new Array("-"," ","/",".");
				
				var err = 0;
				
				var blnFound = false;	
				
				var intDay;
				var intIndex;
				var intMonth;
				var intYear;

				var str1 = "";
				var str2 = "";
				var strDateStyle = "US"; //United States date style 
				var strDay;
				var strMonth;
				var strYear;						
				
				astrMonth[0] = "Jan";
				astrMonth[1] = "Feb";
				astrMonth[2] = "Mar";
				astrMonth[3] = "Apr";
				astrMonth[4] = "May";
				astrMonth[5] = "Jun";
				astrMonth[6] = "Jul";
				astrMonth[7] = "Aug";
				astrMonth[8] = "Sep";
				astrMonth[9] = "Oct";
				astrMonth[10] = "Nov";
				astrMonth[11] = "Dec";
				
				if(strDate.length < 1){
					return true;
				}

				for(intIndex = 0; intIndex < astrSeparator.length; intIndex++){
					if(strDate.indexOf(astrSeparator[intIndex]) != -1){
						
						astrDate = strDate.split(astrSeparator[intIndex]);								
						
						if(astrDate.length == 2){
							d = new Date();										
							astrDate[2] = d.getFullYear();					
						}

						if(astrDate.length != 3){    
							err = 1;
							return false;
						}
						else{
							strDay = astrDate[0];
							strMonth = astrDate[1];
							strYear = astrDate[2];			
						}							

						blnFound = true;
					}
				}
			
				if(blnFound == false){
					if(strDate.length > 5){
						strDay = strDate.substr(0, 2);
						strMonth = strDate.substr(2, 2);
						strYear = strDate.substr(4);
					}
				}
			
				if(strYear.length == 2){
					if(blnIsBirthdate == true){
						
						intYear = parseInt(strYear, 10);
						
						if(isNaN(intYear)){
							err = 4;
							return false;
						}
					
						d = new Date();										
						var i = d.getFullYear();					
						i = i - 2000;					
						
						if(intYear <= i){
							strYear = '20' + strYear;
						}
						else{
							strYear = '19' + strYear;
						}	

					}	
					else{								
						strYear = '20' + strYear;
					}	
				}

				// US style
				if(strDateStyle == "US"){
					strTemp = strDay;
					strDay = strMonth;
					strMonth = strTemp;
				}
			
				intDay = parseInt(strDay, 10);

				if(isNaN(intDay)){
					err = 2;
					return false;
				}

				intMonth = parseInt(strMonth, 10);

				if(isNaN(intMonth)){
					for(var i = 0; i < 12; i++){
						if(strMonth.toUpperCase() == astrMonth[i].toUpperCase()){
							intMonth = i + 1;
							strMonth = astrMonth[i];
							i = 12;
						}
					}

					if(isNaN(intMonth)){
						err = 3;
						return false;
					}
				}

				intYear = parseInt(strYear, 10);
			
				if(isNaN(intYear)){
					err = 4;
					return false;
				}

				if(intMonth > 12 || intMonth < 1){
					err = 5;
					return false;
				}

				if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)){
					err = 6;
					return false;
				}

				if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1)){
					err = 7;
					return false;
				}

				if(intMonth == 2){
					if(intDay < 1){
						err = 8;
						return false;
					}

					if(isLeapYear(intYear) == true){
						if(intDay > 29){
							err = 9;
							return false;
						}
					}
					else{
						if(intDay > 28){
							err = 10;
							return false;
						}
					}
				}

				if(intYear > 2999 || intYear < 1900){
					err = 11;
					return false;
				}
					
				if(intMonth < 10){
					str1 = "0";
				}
				
				if(intDay < 10){
					str2 = "0";
				}
						
				if(strDateStyle == "US"){		
					objInput.value = str1 + intMonth + "/" + str2 + intDay + "/" + intYear;
				}
				else{
					objInput.value = intDay + "/" + intMonth + "/" + intYear;
				}									

				if(blnIsBirthdate){
					d = new Date();										
					s = new Date(objInput.value);
					if((d-s) <= 0){
						return false;
					}
				}
												
			return true;	
			
			}
			catch(err){
				return false;
			}	
		}
		  
		//--------------------------------------------------------------------------------
		// Name:	isEmail(objInput)
		// Desc:    
		//--------------------------------------------------------------------------------

		function isEmail(objInput){
			var strEmail = objInput.value;
			var astrDomain;
			var astrIPAddress;
			var astrMatch;
			var strPattern;
			var strDomain;
			var strUser;
			var int1;
			
			if(strEmail.length < 1){	
				return true;
			}
				
			// The following pattern is used to check if the entered e-mail addres
			// fits the user@domain format.  It also is used to separate the username
			// from the domain. 
			
			var strPattern1 = /^(.+)@(.+)$/
			
			// The following string represents the pattern for matching all special
			// characters.  We don't want to allow special characters in the address. 
			// These characters include ( ) < > @ , ; : \ " . [ ]    

			var strPattern2 = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
			
			// The following string represents the range of characters allowed in a 
			// username or domainname.  It really states which chars aren't allowed. 
			
			var strPattern3 = "\[^\\s" + strPattern2 + "\]";
			
			// The following pattern applies if the "user" is a quoted string (in
			// which case, there are no rules about which characters are allowed
			// and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
			// is a legal e-mail address. 
			
			var strPattern4 = "(\"[^\"]*\")";
			
			// The following pattern applies for domains that are IP addresses,
			// rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
			// e-mail address. NOTE: The square brackets are required. 
			
			var strPattern5= /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
			
			// The following string represents an strAtom (basically a series of
			// non-special characters.) 
			
			var strAtom = strPattern3 + '+';
			
			// The following string represents one word in the typical username.
			// For example, in john.doe@somewhere.com, john and doe are words.
			// Basically, a word is either an strAtom or quoted string. 
			
			var strWord = "(" + strAtom + "|" + strPattern4 + ")";
			
			// The following pattern describes the structure of the user
			
			var strPattern6 = new RegExp("^" + strWord + "(\\." + strWord + ")*$")
			
			// The following pattern describes the structure of a normal symbolic
			// domain, as opposed to strPattern5, shown above. */
			
			var strPattern7 = new RegExp("^" + strAtom + "(\\." + strAtom +")*$")
			var strPattern8;
			
			// Finally, let's start trying to figure out if the supplied address is
			// valid. Begin with the coarse pattern to simply break up user@domain into
			// different pieces that are easy to analyze. 

			astrMatch = strEmail.match(strPattern1)
			
			if(astrMatch == null){
				// Too many/few @'s or something; basically, this address doesn't
				// even fit the general mold of a valid e-mail address.
				// alert("Email address seems incorrect (check @ and .'s)")
				return false
			}

			strUser = astrMatch[1]
			strDomain = astrMatch[2]

			// See if "strUser" is valid 
			if(strUser.match(strPattern6) == null){
				// strUser is not valid
				// alert("The username doesn't seem to be valid.")
				return false
			}

			// if the e-mail address is at an IP address (as opposed to a symbolic
			// host name) make sure the IP address is valid. 
			astrIPAddress = strDomain.match(strPattern5)
			if(astrIPAddress != null){
				// this is an IP address
				for(var i = 1; i <= 4; i++){
					if(astrIPAddress[i] > 255){
						// alert("Destination IP address is invalid!")
						return false
					}
				}
				return true
			}

			// Domain is symbolic name
			astrDomain = strDomain.match(strPattern7)
			if(astrDomain == null){
				// alert("The domain name doesn't seem to be valid.")
				return false
			}

			// domain name seems valid, but now make sure that it ends in a
			// three-letter word (like com, edu, gov) or a two-letter word,
			// representing country (uk, nl), and that there's a hostname preceding 
			// the domain or country. 

			// Now we need to break up the domain to get a count of how many atoms
			// it consists of. 
			strPattern8 = new RegExp(strAtom,"g")
			astrDomain = strDomain.match(strPattern8)
			
			if(astrDomain[astrDomain.length - 1].length < 2 || 
				astrDomain[astrDomain.length - 1].length > 3){
			// the address must end in a two letter or three letter word.
			// alert("The address must end in a three-letter domain, or two letter country.")
			return false
			}

			// Make sure there's a host name preceding the domain.
			if(astrDomain.length < 2){
			// alert("This address is missing a hostname!")
			return false
			}
			objInput.value = strEmail;
			return true;
		}  

		//--------------------------------------------------------------------------------
		// Name:	isLeapYear(intYear)
		// Desc:    
		//--------------------------------------------------------------------------------

		function isLeapYear(intYear){
			
			if(intYear % 100 == 0){
				if(intYear % 400 == 0){ return true; }
			}
			else{
				if ((intYear % 4) == 0){ return true; }
			}

			return false;
		}

		//--------------------------------------------------------------------------------
		// Name:	trim(strValue)
		// Desc:    
		//--------------------------------------------------------------------------------

		function trim(strValue)
		{
		   
			while(''+ strValue.charAt(strValue.length - 1) == ' '){
				strValue = strValue.substring(0, strValue.length - 1);
			}
		   
			while(''+ strValue.charAt(0) ==' '){
				strValue = strValue.substring(1, strValue.length);
			}
			
			return strValue;	
		}