﻿
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() {
      registrationForm = document.getElementById("registration");

   // Valid email address				
//      if (errorMsg != ''){
//        registrationForm.EMail.focus();
//        return false;
//      }     
  	    // Activity template code 
//  	    var actTempCode = registrationForm.ActivityTemplateCode.value.split("-");

//	    for (var i=0; i<registrationForm.company_type.length; i++) {
//    	    if (registrationForm.company_type[i].checked) {
//     		    actTempCode[0] = actTempCode[0] + "-" + registrationForm.company_type[i].value;    
//	 		    break;
//   		    }
//	    }
//	    registrationForm.ActivityTemplateCode.value = actTempCode[0];
     
 	    // 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;
  	    }         
     
       
      //valid Job Responsibility
      if (document.getElementsByName("JobType").item(0).selectedIndex==0){
        alert("Please enter Job Responsibility.");
	    document.getElementsByName("JobType").item(0).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;
      }     


    for (var i=0; i<registrationForm.product.length; i++) 
    { 
	    var bFilled = false; 
	    if (registrationForm.product[i].checked) 
	    { 
		    bFilled = true; 
		    break; 
	    } 
    } 

    if (!registrationForm.company_type[0].checked && !registrationForm.company_type[1].checked && !registrationForm.company_type[2].checked) 
    { 
	    alert("Please enter the type of company you work for."); 
	    registrationForm.company_type[0].focus(); 
	    return false; 
    } 
    
//    if(registrationForm.question_comment.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.")) 
//	    { 
//		    registrationForm.question_comment.value = registrationForm.question_comment.value.substring(0,1999); 
//		    return true; 
//	    } 
//	    else 
//	    { 
//		    registrationForm.question_comment.focus(); 
//		    return false; 
//	    } 
//    }

//    var cmp = "";
//    for (var i=0; i<registrationForm.company_type.length; i++) 
//    { 
//	    if (registrationForm.company_type[i].checked) 
//	    { 
//		    cmp = "DDT-ContactUs-" + registrationForm.company_type[i].value; 
//	    } 
//    }

//    registrationForm.ActivityTemplateCode.value = cmp;

  
}




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;
      }     
}
        