function selVal(s_ele,s_val){
	for(var sel_i=0;sel_i<s_ele.length;sel_i++){
		if(s_ele.options[sel_i].value==s_val){
			s_ele.selectedIndex=sel_i;
			break;
		}
	}
}
function chkedVal(s_ele,s_val){
	for(var sel_i=0;sel_i<s_ele.length;sel_i++){
		if(s_ele[sel_i].value==s_val){
			s_ele[sel_i].checked=true;
			break;
		}
	}
}
function checkData(){
	showValErrors();
	fillData();
}
// VAL E INC: null
function showValErrors(){
	var fm = document.forms['signup_form'];
	 try{
	}catch(e){
		 alert('show val e: ' + e);
	}
}
function fillData(){
	var fm = document.forms['signup_form'];
		 try{
		}catch(e){
			 alert(e);
	}
}

	var fm;
	
	var arCompanyStateAllOpts;
	var state_fd;
	var country_fd;
	var bStateInit = false;
	var formname_ele;	
	var campaignid_ele;

	
		var arCompanyTemplateDefOpt;
		var arCompanyTemplateOpts;
		var arCompanyTemplateTeamOpt;
	
	
		arCompanyTemplateDefOpt = Array(new Option("Generic", 'pe_default'));
		arCompanyTemplateTeamOpt = Array(new Option("Generic", 'te_default'));
		
		arCompanyTemplateOpts = Array( new Option("Generic", "pe_default",true,true),  new Option("Banking", "banking"),  new Option("Insurance", "insurance"),  new Option("Media/Advertising", "media"),  new Option("Manufacturing", "manufacturing"),  new Option("Professional Services", "profserv"),  new Option("Software", "software"),  new Option("Telecommunications", "telecommunications"));

		var oProVars = {"cid":"701300000002cXN","ndx":0,"fm":"Signup1"};
		var oTeamVars = {"cid":"70130000000DAld","ndx":0,"fm":"TeamSignup"};
	
		var oCurOrg={"l":'en_US',"o":"PlanOrgPE","t":"pe_default"};
	
					
	arNotApply = Array(	new Option("Not Required", ''));
			
	
	arCanada = Array(	new Option("Select One", '')
	
						, new Option("Alberta", "AB")
	
						, new Option("British Columbia", "BC")
	
						, new Option("Manitoba", "MB")
	
						, new Option("New Brunswick", "NB")
	
						, new Option("Newfoundland", "NL")
	
						, new Option("Northwest Territories", "NT")
	
						, new Option("Nova Scotia", "NS")
	
						, new Option("Nunavut", "NU")
	
						, new Option("Ontario", "ON")
	
						, new Option("Prince Edward Island", "PE")
	
						, new Option("Quebec", "QC")
	
						, new Option("Saskatchewan", "SK")
	
						, new Option("Yukon", "YT")
	
	);
	
	arUSA = Array(		new Option("Select One", '')
	
						, new Option("Alabama", "AL")
	
						, new Option("Alaska", "AK")
	
						, new Option("Arizona", "AZ")
	
						, new Option("Arkansas", "AR")
	
						, new Option("California", "CA")
	
						, new Option("Colorado", "CO")
	
						, new Option("Connecticut", "CT")
	
						, new Option("Delaware", "DE")
	
						, new Option("District of Columbia", "DC")
	
						, new Option("Florida", "FL")
	
						, new Option("Georgia", "GA")
	
						, new Option("Hawaii", "HI")
	
						, new Option("Idaho", "ID")
	
						, new Option("Illinois", "IL")
	
						, new Option("Indiana", "IN")
	
						, new Option("Iowa", "IA")
	
						, new Option("Kansas", "KS")
	
						, new Option("Kentucky", "KY")
	
						, new Option("Louisiana", "LA")
	
						, new Option("Maine", "ME")
	
						, new Option("Maryland", "MD")
	
						, new Option("Massachusetts", "MA")
	
						, new Option("Michigan", "MI")
	
						, new Option("Minnesota", "MN")
	
						, new Option("Mississippi", "MS")
	
						, new Option("Missouri", "MO")
	
						, new Option("Montana", "MT")
	
						, new Option("Nebraska", "NE")
	
						, new Option("Nevada", "NV")
	
						, new Option("New Hampshire", "NH")
	
						, new Option("New Jersey", "NJ")
	
						, new Option("New Mexico", "NM")
	
						, new Option("New York", "NY")
	
						, new Option("North Carolina", "NC")
	
						, new Option("North Dakota", "ND")
	
						, new Option("Ohio", "OH")
	
						, new Option("Oklahoma", "OK")
	
						, new Option("Oregon", "OR")
	
						, new Option("Pennsylvania", "PA")
	
						, new Option("Rhode Island", "RI")
	
						, new Option("South Carolina", "SC")
	
						, new Option("South Dakota", "SD")
	
						, new Option("Tennessee", "TN")
	
						, new Option("Texas", "TX")
	
						, new Option("Utah", "UT")
	
						, new Option("Vermont", "VT")
	
						, new Option("Virginia", "VA")
	
						, new Option("Washington", "WA")
	
						, new Option("West Virginia", "WV")
	
						, new Option("Wisconsin", "WI")
	
						, new Option("Wyoming", "WY")
	
	);
	
	arCompanyStateAllOpts = Array(	new Option("Select One", "")
	
						, new Option("Alabama", "AL")
	
						, new Option("Alaska", "AK")
	
						, new Option("Arizona", "AZ")
	
						, new Option("Arkansas", "AR")
	
						, new Option("California", "CA")
	
						, new Option("Colorado", "CO")
	
						, new Option("Connecticut", "CT")
	
						, new Option("Delaware", "DE")
	
						, new Option("District of Columbia", "DC")
	
						, new Option("Florida", "FL")
	
						, new Option("Georgia", "GA")
	
						, new Option("Hawaii", "HI")
	
						, new Option("Idaho", "ID")
	
						, new Option("Illinois", "IL")
	
						, new Option("Indiana", "IN")
	
						, new Option("Iowa", "IA")
	
						, new Option("Kansas", "KS")
	
						, new Option("Kentucky", "KY")
	
						, new Option("Louisiana", "LA")
	
						, new Option("Maine", "ME")
	
						, new Option("Maryland", "MD")
	
						, new Option("Massachusetts", "MA")
	
						, new Option("Michigan", "MI")
	
						, new Option("Minnesota", "MN")
	
						, new Option("Mississippi", "MS")
	
						, new Option("Missouri", "MO")
	
						, new Option("Montana", "MT")
	
						, new Option("Nebraska", "NE")
	
						, new Option("Nevada", "NV")
	
						, new Option("New Hampshire", "NH")
	
						, new Option("New Jersey", "NJ")
	
						, new Option("New Mexico", "NM")
	
						, new Option("New York", "NY")
	
						, new Option("North Carolina", "NC")
	
						, new Option("North Dakota", "ND")
	
						, new Option("Ohio", "OH")
	
						, new Option("Oklahoma", "OK")
	
						, new Option("Oregon", "OR")
	
						, new Option("Pennsylvania", "PA")
	
						, new Option("Rhode Island", "RI")
	
						, new Option("South Carolina", "SC")
	
						, new Option("South Dakota", "SD")
	
						, new Option("Tennessee", "TN")
	
						, new Option("Texas", "TX")
	
						, new Option("Utah", "UT")
	
						, new Option("Vermont", "VT")
	
						, new Option("Virginia", "VA")
	
						, new Option("Washington", "WA")
	
						, new Option("West Virginia", "WV")
	
						, new Option("Wisconsin", "WI")
	
						, new Option("Wyoming", "WY")
	
	
	
						, new Option("Alberta", "AB")
	
						, new Option("British Columbia", "BC")
	
						, new Option("Manitoba", "MB")
	
						, new Option("New Brunswick", "NB")
	
						, new Option("Newfoundland", "NL")
	
						, new Option("Northwest Territories", "NT")
	
						, new Option("Nova Scotia", "NS")
	
						, new Option("Nunavut", "NU")
	
						, new Option("Ontario", "ON")
	
						, new Option("Prince Edward Island", "PE")
	
						, new Option("Quebec", "QC")
	
						, new Option("Saskatchewan", "SK")
	
						, new Option("Yukon", "YT")
			
	);
		
	function InitStateOpts(){	
		state_fd = fm.elements['CompanyState'];
		country_fd = fm.elements['CompanyCountry'];
		mapCountries = {"CA":arCanada, "US":arUSA};
		bStateInit = true;
	}

	
		function updOrgT(fd){
			if(fd.selectedIndex>0){oProVars["ndx"]=fd.selectedIndex;}
			updOrg("t",this.value);
		}
		function updOrg(n,v){
			oCurOrg[n]=v;
			coordOrg();
		}
		function coordOrg(){
			o_fd = fm.elements['PlanOrgType'];
			t_fd = fm.elements['CompanyTemplate'];
			l_fd = fm.elements['CompanyLanguage'];
	
			if(oCurOrg.o=="PlanOrgWE"){
				oCurOrg.t=="te_default";
			}else{
				if((oCurOrg.l!="en_US") || (oCurOrg.t=="te_default")){
					oCurOrg.t="pe_default";
				}
			}
			arTmp = (oCurOrg.o=="PlanOrgWE")?arCompanyTemplateTeamOpt:((oCurOrg.l=="en_US")?arCompanyTemplateOpts:arCompanyTemplateDefOpt);
			
			if(typeof(t_fd)!='undefined'){
				old_ubound = t_fd.options.length;
				new_ubound = arTmp.length;
				z_ubound = old_ubound;
				if(new_ubound > old_ubound){z_ubound=new_ubound;}
				for(oi=0; oi < new_ubound; oi++){t_fd.options[oi] = arTmp[oi];}
				for(zi = t_fd.options.length-1;zi>=oi;zi--){t_fd.options[zi]= null;}
			}
			if(oCurOrg.o=="PlanOrgWE"){
				if(typeof(t_fd)!='undefined'){t_fd.selectedIndex=0;}
				formname_ele.value=oTeamVars.fm;
				campaignid_ele.value=oTeamVars.cid;
			}else{
				if(typeof(t_fd)!='undefined'){t_fd.selectedIndex=((oCurOrg.l=="en_US")?oProVars.ndx:0);}
				formname_ele.value=oProVars.fm;
				campaignid_ele.value=oProVars.cid;
			}
		}
	

		function swapCountryStates(arCountry){
			state_fd = fm.elements['CompanyState'];
			country_fd = fm.elements['CompanyCountry'];
			new_ubound = arCountry.length;
			old_ubound = state_fd.options.length;
			z_ubound = old_ubound;
			if(new_ubound > old_ubound){z_ubound=new_ubound;}
			for(oi=0; oi < new_ubound; oi++){state_fd.options[oi] = arCountry[oi];}
			for(zi = state_fd.options.length-1;zi>=oi;zi--){state_fd.options[zi]= null;}
		}

		function checkStates(c){
			try{
				if(!bStateInit){InitStateOpts();}
				if(c == ''){
					arCountry = arCompanyStateAllOpts;
				}else{
					arCountry = mapCountries[c];
					if(typeof(arCountry) == 'undefined'){arCountry = arNotApply;}
					if(arCountry){swapCountryStates(arCountry);}
					var st_span = document.getElementById('state_req_span');
					if(st_span){st_span.style.display = ((c == 'US')||(c == 'CA'))?'inline':'none';}
				}
			}catch(e){
					alert('reg fm states' + e.description);
			}finally{}
		}

	function isValidPhoneNumber(num, requiredDigits) {
	    var digits = 0;
	    if (num == null) return false;
	    for( i=0; i<num.length; i++ ){
	        var c = num.charCodeAt(i);  
	        if( (c>=48) && (c<=57) ) digits++;
	    }    
	    return (digits >= requiredDigits);
	}
	function isValidEmail(emailad) {
	   var exclude=/[^@\-\.\w\']|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	   var check=/@[\w\-]+\./;
	   var checkend=/\.[a-zA-Z]{2,4}$/;
	   return (((emailad.length > 80) || (emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1))?false:true;
	}
	
	 function rand ( n )
	  {
		return ( Math.floor ( Math.random ( ) * n + 1 ) );
	  }

  function setUsername()
    {

      try{

		  var appName = "Ascent";  //TODO:  Partner should change trialforce to their application name
		  var at="@";
		  var emailAddr = document.forms[0].Email.value;
	
		  var lat=emailAddr.indexOf(at);
		  if (lat == -1) {
			 return false;
		  }
	
		  var userFront = emailAddr.substring(0, lat);
	
		  document.forms[0].UserUsername.value = userFront + "@" + rand(50) + appName + ".com";

      }catch(e){
       alert("E:CAUGHT " + e.description);
       return false;
      }
      return true;

  }

	
	function validate() {
	    var ArErrMsg= new Array();
	    var err_msg = new String();
	    try{
	    	
		    if((fm.elements['FirstName']) && (fm.elements['FirstName'].value.length < 1)){
			    ArErrMsg['FirstName']="Enter your First Name";
		    }
		    if((fm.elements['LastName']) && (fm.elements['LastName'].value.length < 1)){
			    ArErrMsg['LastName']="Enter your Last Name";
		    }
		    
		    if((fm.elements['Title']) && (fm.elements['Title'].value.length < 1)){
			    ArErrMsg['Title']="Enter your Title";
		    }
		    
		    if((fm.elements['Company']) && (fm.elements['Company'].value.length < 1)){
			    ArErrMsg['Company']="Enter your Company Name";
		    }
		     		
			if (!isValidPhoneNumber(fm.elements['Phone'].value, 10)) { ArErrMsg['Phone']="Enter a Valid Phone Number";}
			
	    	if (!isValidEmail(fm.elements['Email'].value)) { ArErrMsg['Email']="Enter a Valid Email Address";}

		    if(fm.elements['Country'].selectedIndex < 1){
		    	ArErrMsg['Country']="Choose a Valid Country";
		    }else{	
		    	c_ele = fm.elements['Country'];
		    	var c_val = c_ele[c_ele.selectedIndex].value;
		    	if((c_val == 'CA') || (c_val == 'US')){if(fm.elements['State'].selectedIndex < 1) {ArErrMsg['State']="Enter your State/Province";}}
			}							
		    
			if(fm.elements['NumberOfEmployees'].selectedIndex < 1) {ArErrMsg['NumberOfEmployees']="Select the Number of Employees";}
			
	    	if ((fm.process_form.value != false) && (fm.SubscriptionAgreement.checked != true)){
				ArErrMsg['SubscriptionAgreement']="Please read and agree to the Master Subscription Agreement";			
		    }
		    var fd_i=0;
		    for(var fd_n in ArErrMsg){
		    	err_msg = new String(err_msg.concat("\n" + ArErrMsg[fd_n]));
		    	fd_i++;
		    }
		    if(fd_i>0){
		    	alert(err_msg);
		    	return false;
		    }
		
		
	    }catch(e){
	    	alert("E CAUGHT2: " + e.description);
	    	return false;
	    }      
		
		var result = setUsername();
		if (result == false) {
			return false;
		}
		
		return true;
		
    }
	
	function init_validate () {
		if(document.forms['signup_form']){
			fm=document.forms['signup_form'];
			formname_ele = fm.elements['formName'];
		}
		checkData();
	}
