﻿
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 hear about us and populate comments field accordingly
function populateComments(hearfrom)
{
    if (hearfrom.value != '' && hearfrom.value != 'Other (please specify)')
        document.getElementById("registration").question_comment.value = hearfrom.value;
    else
        document.getElementById("registration").question_comment.value = document.getElementById("registration").Other.value;
}

// 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() {
      registrationForm = document.getElementById("registration");

   // Valid email address	
   
        // Location
        rdoChicago = registrationForm.Location[0];
        rdoDC = registrationForm.Location[1];
        rdoHouston = registrationForm.Location[2];
        rdoLondon = registrationForm.Location[3];
        rdoAmsterdam  = registrationForm.Location[4];
        
        if ( !rdoChicago.checked && !rdoDC.checked && !rdoHouston.checked && !rdoLondon.checked && !rdoAmsterdam.checked) {
            alert("Please select one of the locations.");
    	    registrationForm.Location[0].focus();
    	    return false;
        }
        

 	    // First name
 	    if (registrationForm.FirstName.value==''){
    	    alert("Please enter your first name.");
    	    registrationForm.FirstName.focus();
  		    return false;
   	    }	
  	    // Last name
  	    if (registrationForm.LastName.value==''){
  	  	    alert("Please enter your last name.");
    	    registrationForm.LastName.focus();
   		    return false;
  	    }         
       
     // Job Title
  	    if (registrationForm.Jobtitle.value==''){
  	  	    alert("Please enter your job title.");
    	    registrationForm.Jobtitle.focus();
   		    return false;
  	    }         
     
       
       // Industry	
      if (registrationForm.PrimeIndustry.selectedIndex==0){
        alert("Please enter your industry.");
	    registrationForm.PrimeIndustry.focus();
        return false;
       }

      // Company
      if (registrationForm.CompanyName1.value==''){
        alert("Please enter a company name.");
       registrationForm.CompanyName1.focus();
       return false;
      }
      // Address 1
      if (registrationForm.Street1.value==''){
      alert("Please enter a street address.");
       registrationForm.Street1.focus();
       return false;
      }
      // City
      if (registrationForm.City.value==''){
      alert("Please enter a city.");
       registrationForm.City.focus();
       return false;
      }
      // Country
      if (registrationForm.Country.selectedIndex==0){
       alert("Please enter a country.");
       registrationForm.Country.focus();
      return false;
       }
      
       // Select state
       if(checkStates(registrationForm.Country.selectedIndex, registrationForm.State, 'EN')==false)    	  {
      alert("Please specify a valid state or province.");
      moveToState(registrationForm.Country.selectedIndex, registrationForm.State, 'EN');
      registrationForm.State.focus(); 
      return false;
      }
      
      // Require state and postal code for US  
      if(registrationForm.Country.selectedIndex==1){
  	    if(registrationForm.State.selectedIndex==0){
		       alert("State or Province is required for US addresses.");
  		       registrationForm.State.focus(); 
		       return false;
	    }
	    if(registrationForm.PostCode.value==""){
		       alert("Postal Code is required for US addresses.");
  		       registrationForm.PostCode.focus(); 
		       return false;
	    }
      } 
        // Require state for Canada 
       if(registrationForm.Country.selectedIndex==37){
  	    if(registrationForm.State.selectedIndex==0){
		       alert("State or Province is required for Canadian addresses.");
  		       registrationForm.State.focus(); 
		       return false;
	    }
      } 

       // Phone
      if (registrationForm.persDirectPhone.value==''){
        alert("Please enter a phone number.");
       registrationForm.persDirectPhone.focus();
       return false;
      }
      
      // Valid email address				
      if (
            (registrationForm.EMail.value=='') ||
            (registrationForm.EMail.value.indexOf("@")==-1) ||
            (registrationForm.EMail.value.indexOf(".")==-1) ||
            (registrationForm.EMail.value.indexOf(" ")!==-1) ||
            (registrationForm.EMail.value.length < 6)
         ) {
        alert("Please enter a valid email address.");
  	    registrationForm.EMail.focus();
        return false;
      }     
      
      //validate the hear about us, if other is specified, specify other
//      if ( registrationForm.howhear.value == '') 
//      {
//        alert("Please specify a source.");
//  	    registrationForm.howhear.focus();
//        return false;
//      }

//    for (var i=0; i<registrationForm.product.length; i++) 
//    { 
//	    var bFilled = false; 
//	    if (registrationForm.product[i].checked) 
//	    { 
//		    bFilled = true; 
//		    break; 
//	    } 
//    } 
    
    //For activityTemplateCode
    registrationForm.ActivityTemplateCode.value = registrationForm.TemplateCode.value;
    
    for (var i=0; i<registrationForm.Location.length; i++) 
    { 
	    if (registrationForm.Location[i].checked) 
	    { 
		     registrationForm.ActivityTemplateCode.value += "-" + registrationForm.Location[i].value; 
		     break;
	    } 
    }


  
}




function handleValidation(thisObj)
{
    registrationForm = document.getElementById("registration");
  if ((registrationForm.EMail.value=='') ||
         (registrationForm.EMail.value.indexOf("@")==-1) ||
          (registrationForm.EMail.value.indexOf(".")==-1) ||
         (registrationForm.EMail.value.indexOf(" ")!==-1) ||
         (registrationForm.EMail.value.length < 6)) {
        alert("Please enter a valid email address.");
  	    registrationForm.EMail.focus();
        return false;
      }     
}
        