﻿     var errorMsg = "";
    // This function will select the correct state sub-list in the dropdown based on the country selection. 
    function changeIndustry(industry){
     
	    var regularOptions = new Array(
		    "Accounting",
		    "Agriculture",
		    "Biotechnology",
		    "Chemicals",
		    "Construction",
		    "Consulting",
		    "Education",
		    "Electronics",
		    "Energy",
		    "Engineering",
		    "Entertainment",
		    "Environmental",
		    "Financial Services",
		    "Food and Beverage",
		    "Government",
		    "Healthcare",
		    "Hospitality",
		    "Insurance",
		    "Legal" ,
		    "Machinery",
		    "Manufacturing",
		    "Not For Profit",
		    "Pharmaceuticals",
		    "Publishing and Printing",
		    "Recreation",
		    "Retail",
		    "Shipping",
		    "Technology",
		    "Telecommunications",
		    "Transportation",
		    "Utilities",
		    "Other"
		    ); 

	    var softwareOptions = new Array(
		    "SW - App Dev/Testing/Lifecycle Tools",
		    "SW - Business Intelligence",
		    "SW - CAD",
		    "SW - Collaboration/Project Management",
		    "SW - Content/Document Management",
		    "SW - CRM",
		    "SW - Databases",
		    "SW - Data Integration/Legacy Extension",
		    "SW - Data Warehouses/Query Tools/OLAP",
		    "SW - Desktop Applications",
		    "SW - EAI",
		    "SW - e-Business Applications",
		    "SW - eBusiness Tools",
		    "SW - Engineering",
		    "SW - ERP",
		    "SW - Financial Applications",
		    "SW - Healthcare and Pharmaceutical",
		    "SW - Human Resources",
		    "SW - Infrastructure/PerfAsset/Sys Mgmt",
		    "SW - Insurance",
		    "SW - Marketing Automation",
		    "SW - Midware/Connect/AppServ/WebServ",
		    "SW - Mobile/Wireless",
		    "SW - Network Management",
		    "SW - Operating Systems",
		    "SW - Portal and Portal Tools",
		    "SW - Security",
		    "SW - Storage Management",
		    "SW - Supply Chain/Manufacturing",
		    "SW - Telecommunications"
	    );
    	
	    if( industry.options.length>1){	
	 	    industry.options[industry.options.length-1] = null; 
	    }	
    	
	    else{
		    for(i=1; i<regularOptions.length; i++){	
			    industry.options[i] = new Option(regularOptions[i]); 
		    }	
	    }	
    	 	
    }

    // This function will select the correct state sub-list in the dropdown based on the country selection. 
    function moveToState(index, state, language){

	    var countryIdx = getCountryIndices(); 
	    var stateIdx =   getStateListIndices(); 
     
	    if(index==countryIdx[0])  					    // Arg 
		    state.selectedIndex = stateIdx[0]; 
	    else if(index==countryIdx[1])					// Aus
		    state.selectedIndex = stateIdx[1]; 		
 	    else if(index==countryIdx[2])					// Bra
		    state.selectedIndex = stateIdx[2]; 		
	    else if(index==countryIdx[3])					// Can
		    state.selectedIndex = stateIdx[3]; 		
	    else if(index==countryIdx[4])					// Ecu
		    state.selectedIndex = stateIdx[4]; 				
	    else if(index==countryIdx[5])					// Jap
		    state.selectedIndex = stateIdx[5]; 	
	    else if(index==countryIdx[6])					// Mex
		    state.selectedIndex = stateIdx[6]; 			
	    else if(index==countryIdx[7])					// Per
		    state.selectedIndex = stateIdx[7]; 
	    else if(index==countryIdx[8])					// UK
		    state.selectedIndex = stateIdx[8]; 	
	    else if(index==countryIdx[9])					// USA
		    state.selectedIndex = stateIdx[9]; 
	    else
		    state.selectedIndex = 0;	
    }

    // This function will return an array of the indexes of the following countries in order: 
    // Arg // Aus // Bra // Can // Ecu // Jap // Mex // Per // UK // USA
    // This means that we will only have to maintain the indices in one place!	
    function getCountryIndices(){
	    return new Array( 9, 12, 28, 37, 56, 91, 116, 142, 186, 1 ); 	
    }

    function getStateListIndices(){
   	    return new Array( 53, 78,  87,  115,  129,  151,  199,  232, 258, 1 ); 	
    }

    // This function will return the correct state count, given the state name or index
    function getStateCount(idx){ 
	    var stateCounts = new Array(24,8,27,13,21,47,32,25,78,51);	
	    return stateCounts[idx];
    }

    // This function will make sure that a state in the correct range for a particular
    // country is selected. If the country in question does not have states, it will 
    // set the field value to blank to ensure that a state value is not submitted and 
    // return true. 
    function checkStates(index, state, language){	
    	
	    var countryIdx = getCountryIndices(language); 
	    var stateIdx   = getStateListIndices(language); 
    							
	    if( index==null || index<0 )						// No state selected at all
		    return false; 
    	
	    // Check all countries with states
	    for(i=0; i<10; i++){
		    if(i==8)
			    continue;
		    if(index==countryIdx[i] &&  				 
			    (state.selectedIndex < stateIdx[i] + 1 || 
			    state.selectedIndex > stateIdx[i] + getStateCount(i) ))
		    return false; 
	    }
    	
	    // For countries without states, reset form to blank state value before submitting
	    if( index!=0 && 
			    index!=countryIdx[0] && 
			    index!=countryIdx[1] && 
			    index!=countryIdx[2] && 
			    index!=countryIdx[3] &&
			    index!=countryIdx[4] && 
			    index!=countryIdx[5] && 
			    index!=countryIdx[6] && 
			    index!=countryIdx[7] && 
			    index!=countryIdx[8] && 
			    index!=countryIdx[9]){
		    state.selectedIndex=0; 
		    return true; 
	    }	
    	
	    // For correct forms	
	    return true;	
    }

     // Validates required contact information upon submission of form
      function Validate() {    
            
 	    // First name
 	    if (document.getElementsByName("FirstName").item(0).value==''){
    	    alert("Please enter your first name.");
    	    document.getElementsByName("FirstName").item(0).focus();
  		    return false;
   	    }	
  	    // Last name
  	    if (document.getElementsByName("LastName").item(0).value==''){
  	  	    alert("Please enter your last name.");
    	    document.getElementsByName("LastName").item(0).focus();
   		    return false;
  	    }         
       
     // Job Title
  	    if (document.getElementsByName("Jobtitle").item(0).value==''){
  	  	    alert("Please enter your job title.");
    	    document.getElementsByName("Jobtitle").item(0).focus();
   		    return false;
  	    }         
     
       
       // Industry	
      if (document.getElementsByName("PrimeIndustry").item(0).selectedIndex==0){
        alert("Please enter your industry.");
	    document.getElementsByName("PrimeIndustry").item(0).focus();
        return false;
       }

      // Company
      if (document.getElementsByName("CompanyName1").item(0).value==''){
        alert("Please enter a company name.");
        document.getElementsByName("CompanyName1").item(0).focus();
       return false;
      }
      // Address 1
      if (document.getElementsByName("Street1").item(0).value==''){
        alert("Please enter a street address.");
        document.getElementsByName("Street1").item(0).focus();
        return false;
      }
      // City
      if (document.getElementsByName("City").item(0).value==''){
        alert("Please enter a city.");
        document.getElementsByName("City").item(0).focus();
        return false;
      }
      // Country
      if (document.getElementsByName("Country").item(0).selectedIndex==0){
       alert("Please enter a country.");
       document.getElementsByName("Country").item(0).focus();
      return false;
       }
      
       // Select state
       if(checkStates(document.getElementsByName("Country").item(0).selectedIndex, document.getElementsByName("State").item(0), 'EN')==false)    	  {
      alert("Please specify a valid state or province.");
      moveToState(document.getElementsByName("Country").item(0).selectedIndex, document.getElementsByName("State").item(0), 'EN');
      document.getElementsByName("State").item(0).focus(); 
      return false;
      }
      
      // Require state and postal code for US  
      if(document.getElementsByName("Country").item(0).selectedIndex==1){
  	    if(document.getElementsByName("State").item(0).selectedIndex==0){
		       alert("State or Province is required for US addresses.");
  		       document.getElementsByName("State").item(0).focus(); 
		       return false;
	    }
	    if(document.getElementsByName("PostCode").item(0).value==""){
		    alert("Postal Code is required for US addresses.");
  		    document.getElementsByName("PostCode").item(0).focus(); 
		    return false;
	    }
      } 

//        // Require state for Canada 
  	    if(document.getElementsByName("Country").item(0).selectedIndex==37){
  	     if(document.getElementsByName("State").item(0).selectedIndex==0){
		       alert("State or Province is required.");
  		       document.getElementsByName("State").item(0).focus(); 
		       return false;
	    }
      } 

//  	    if(document.getElementsByName("State").item(0).selectedIndex==37){
//		       alert("State or Province is required for Canadian addresses.");
//  		       document.getElementsByName("State").item(0).focus(); 
//		       return false;
//	    }
//      } 

       // Phone
      if (document.getElementsByName("persDirectPhone").item(0).value==''){
        alert("Please enter a phone number.");
        document.getElementsByName("persDirectPhone").item(0).focus();
        return false;
      }
//      

  // Valid email address				
  if ((document.getElementsByName("EMail").item(0).value =='') ||
     (document.getElementsByName("EMail").item(0).value.indexOf("@")==-1) ||
      (document.getElementsByName("EMail").item(0).value.indexOf(".")==-1) ||
     (document.getElementsByName("EMail").item(0).value.indexOf(" ")!==-1) ||
     (document.getElementsByName("EMail").item(0).value.length < 6)){
    alert("Please enter a valid email address.");
  	document.getElementsByName("EMail").item(0).focus();
    return false;
  }     

    if (!document.getElementsByName("company_type").item(0).checked && !document.getElementsByName("company_type").item(1).checked && !document.getElementsByName("company_type").item(2).checked) 
    { 
	    alert("Please enter the type of company you work for."); 
	    document.getElementsByName("company_type").item(0).focus(); 
	    return false; 
    } 
    
    if(document.getElementsByName("question_comment").item(0).value.length > 2000 ) 
    { 
	    if(confirm("Your question or comment cannot contain more than 2000 characters.\n--Click OK to truncate your question.\n--Click Cancel to return and edit your question.")) 
	    { 
		    document.getElementsByName("question_comment").item(0).value = document.getElementsByName("question_comment").item(0).value.substring(0,1999); 
		    return true; 
	    } 
	    else 
	    { 
		    document.getElementsByName("question_comment").item(0).value.focus(); 
		    return false; 
	    } 
    }
    
    
    var re = /&nbsp;|&nbsp/g;        
    document.getElementsByName("question_comment").item(0).value = document.getElementsByName("question_comment").item(0).value.replace(re, "");   


   // registration.question_comment.value = registration.question_comment.value.replace("&nbsp;", "").replace("&nbsp", "");
     if(trim(document.getElementsByName("question_comment").item(0).value).length < 25 ) 
    { 
	    alert("Please tell us a bit about your application so that we can assign it to the appropriate professional services team.");
		document.getElementsByName("question_comment").item(0).focus(); 
		return false; 

    }
    
//    // Populate the correct ATC 
//    if(document.getElementsByName("company_type").item(0).checked==true)
//        document.getElementsByName("ActivityTemplateCode").item(0).value += "-OEM"; 
//    else if (document.getElementsByName("company_type").item(1).checked ==true) 
//        document.getElementsByName("ActivityTemplateCode").item(0).value += "-Direct"; 
//    else document.getElementsByName("ActivityTemplateCode").item(0).value += "-Reseller"; 
//     
    }
    
    function handleValidation(thisObj)
    {
        errorMsg = "";
        
        var test = validateEmail(thisObj.value);
        var obj = document.getElementById("validationID");
        obj.style.visibility = "visible";
        obj.innerHTML = "Validating email address entered......";
    }
    
    function handleCheckEvent(thisObj)
    {
        document.getElementById("company_type").checked = thisObj.checked;
    }
