String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
function enablestate()

{

	var selObj = document.getElementById("00N50000001RYTt");
	var StateDiv = document.getElementById("StatesDiv");



	if ((document.contactform.elements['00N50000001RYTy'].value == 'United States') || (document.contactform.elements['00N50000001RYTy'].value == 'Canada'))

	{


		//selObj.options[66] = null;
                getStates(document.contactform.elements['00N50000001RYTy'].value );
		document.contactform.elements['00N50000001RYTt'].disabled = false;
                changeDivState('invisible');
                StateDiv.style.display='';
	}else {

		selObj.options[66]=new Option("Outside the U.S or Canada" , "Outside the U.S or Canada");

		selObj.options[66].selected = true;

		document.contactform.elements['00N50000001RYTt'].disabled = true;

		getSalesRep(document.contactform.elements['00N50000001RYTy'].value , 'Outside the U.S or Canada');

 		StateDiv.style.display='none';


	}

}

function changeDivState(newState)
    {
        var contactDiv = document.getElementById("contactSalesRepDiv");
        var contactSalesEmailDiv = document.getElementById("contactSalesEmailDiv");
        var contactSalesRepText= document.getElementById("contactSalesRepText");
        if(newState=='invisible')
            {
                contactSalesRepText.innerHTML="";
                contactDiv.style.display='none';
                contactSalesEmailDiv.style.display='none';

            }
            else
            {
                contactSalesRepText.innerHTML=req.responseText;;
                contactDiv.style.display='';
                contactSalesEmailDiv.style.display='';

            }
    }



function getSalesRepForState()
{

    		getSalesRep(document.contactform.elements['00N50000001RYTy'].value , document.contactform.elements['00N50000001RYTt'].value );

}


function getSalesRep(country , state)
{

			//submitting to following servlet

			var url = "/portal/innovateactionservlet?action=contactus&country=" + country + "&state="+ state + "&token=token.html";



	        var req = initRequest(url);

	        req.onreadystatechange = function() {

	            if (req.readyState == 4) {

	                if (req.status == 200) {

	                    populateSalesRepDiv(req,req.responseXML);

	                } else if (req.status == 204){

	                }

	            }

	        };

	        req.open("GET", url, true);

	        req.send(null);

}

function getStates(country  )
{

			//submitting to following servlet

			var url = "/portal/innovateactionservlet?action=getstates&country=" + country + "&token=token.html";



	        var req = initRequest(url);

	        req.onreadystatechange = function() {

	            if (req.readyState == 4) {

	                if (req.status == 200) {

	                    populateStates(req,req.responseXML);

	                } else if (req.status == 204){

	                }

	            }

	        };

	        req.open("GET", url, true);

	        req.send(null);

}


  //Callback function
 function populateStates(req){
  	document.getElementById('00N50000001RYTt').options.length = 0;
        var textToSplit = req.responseText.trim();
       // alert(req.responseText);
          //Split the document
        var  returnElements=textToSplit.split("||")
          document.getElementById('00N50000001RYTt').options[0] = new Option("--Select from Below--","--Select from Below--");
          //Process each of the elements
          var i=1;
          for ( i=1; i<returnElements.length; i++ ){
             valueLabelPair = returnElements[i].split("|")
             document.getElementById('00N50000001RYTt').options[i] = new Option(valueLabelPair[0].trim(), valueLabelPair[1].trim());
          }
         //    document.getElementById('00N50000001RYTt').options[i] = null;;

    }



function populateSalesRepDiv(req,responseXML)
 {
    var contactDiv = document.getElementById("contactSalesRepDiv");
    var contactSalesEmailDiv = document.getElementById("contactSalesEmailDiv");
    var contactSalesRepText= document.getElementById("contactSalesRepText");
    //alert(req.responseText);
    contactSalesRepText.innerHTML=req.responseText;;
    contactDiv.style.display='';
    contactSalesEmailDiv.style.display='';
 }


 // Check job title
 function checkJobTitle(busType) {
    //modified for HD176407
    if (((busType.value).toLowerCase()).indexOf("student") != -1) {
        return true;
    } else {
        return false;
    }
 }


 function isValidEmail (s) {
    if (s.indexOf('@') == -1) return false;
    if (s.indexOf('.') == -1) return false;
    if (s.length < 4) return false;
    return true;
 }


 function isPersonalEmail(s) {
    if ((s.indexOf('@yahoo') != -1) || (s.indexOf('@hotmail') != -1) || (s.indexOf('@gmail') != -1) || (s.indexOf('@aol') != -1) || (s.indexOf('@rediffmail') != -1)
        || (s.toLowerCase().indexOf('@yahoo')!= -1) || (s.toLowerCase().indexOf('@hotmail')!= -1) || (s.toLowerCase().indexOf('@gmail')!= -1) || (s.toLowerCase().indexOf('@aol')!= -1) || (s.toLowerCase().indexOf('@rediffmail')!= -1)
        ){
        return true;
    } else {
        return false;
    }
 }


 function checkFields() {
    missinginfo = "";

    if (document.contactform.elements.first_name.value == "") {
        missinginfo += "\n     -  First Name";
    }
    if (document.contactform.elements.last_name.value == "") {
        missinginfo += "\n     -  Last Name";
    }
    if (document.contactform.elements.company.value == "") {
        missinginfo += "\n     -  Company Name";
    }
    if (document.contactform.elements['00N50000001Q9mU'].value == "") {
        missinginfo += "\n     -  Job Title";
    }
    if (document.contactform.elements.email.value == "") {
        missinginfo += "\n     -  Work Email";
    }
    if (document.contactform.elements['00N50000001RYTy'].value == "") {
        missinginfo += "\n     -  Country";
    }
    if ((document.contactform.elements['00N50000001RYTy'].value == "United States" ||
        document.contactform.elements['00N50000001RYTy'].value == "Canada") &&
        document.contactform.elements['00N50000001RYTt'].value == "") {
        missinginfo += "\n     -  State/Province";
    }

    if (document.contactform.elements['description'].value == "") {
        missinginfo += "\n     -  Your Message";
    }
    if (missinginfo != "") {
        missinginfo = "_____________________________\n" + "The following information is missing:\n" + missinginfo + "\n_____________________________" + "\nPlease re-enter and submit again!";
        alert(missinginfo);
        return false;
    } else {
        if(!isValidEmail(document.contactform.elements.email.value)){
            missinginfo += "\n     -  Work Email is not valid.";
        }
        if(isPersonalEmail(document.contactform.elements.email.value)){
            missinginfo += "\n     -  You have provided a personal e-mail address. Please provide a corporate e-mail address.";
        }
        if(checkJobTitle(document.contactform.elements['00N50000001Q9mU'])){
            missinginfo += "\n     -  Attention Students: Please direct your IEEE questions to www.ieee.org/contactcenter";
        }
        if (missinginfo != "") {
            missinginfo = "  __________________________________________________________________\n" + missinginfo
                        + "\n__________________________________________________________________";
            alert(missinginfo);
            return false;
        }
        return true;
    }
}

function doSubmit() {
    if (checkFields()) {
        trackSubmit();
    }
}

 function trackSubmit() {
     var baseURL = "http://stats.manticoretechnology.com/Data/553/11619/635DAC29-E924-436E-AE29-AF9DEC5D3007/mtcContactReg.aspx";
     var parameters = "?SendToCRM=" + encodeURIComponent(encodeURIComponent("true")) +
         "&FormID=" + encodeURIComponent(encodeURIComponent("7106")) +
         "&FirstName=" + encodeURIComponent(encodeURIComponent(document.contactform.elements.first_name.value)) +
         "&LastName=" + encodeURIComponent(encodeURIComponent(document.contactform.elements.last_name.value)) +
         "&Company=" + encodeURIComponent(encodeURIComponent(document.contactform.elements.company.value)) +
         "&Job_Title__c=" + encodeURIComponent(encodeURIComponent(document.contactform.elements['00N50000001Q9mU'].value)) +
         "&EmailAddress=" + encodeURIComponent(encodeURIComponent(document.contactform.elements.email.value)) +
         "&Description__c=" + encodeURIComponent(encodeURIComponent(document.contactform.elements['description'].value)) +
         "&Topic_of_Interest__c=" + encodeURIComponent(encodeURIComponent(document.contactform.elements['00N50000001jHK2'].value)) +
         "&Country_Nation__c=" + encodeURIComponent(encodeURIComponent(document.contactform.elements['00N50000001RYTy'].value)) +
         "&State_or_Province__c=" + encodeURIComponent(encodeURIComponent(document.contactform.elements['00N50000001RYTt'].value)) +
         "&MTC_GROUP=" + encodeURIComponent(encodeURIComponent("553")) +
         "&MTC_ID=" + encodeURIComponent(encodeURIComponent("11619")) +
         "&MTC_KEY=" + encodeURIComponent(encodeURIComponent("635DAC29-E924-436E-AE29-AF9DEC5D3007"));
     var url = encodeURIComponent(baseURL + parameters);
     url = "http://www.ieee.org/portal/portalproxy.html?url=" + url;
     var req = initRequest(url);
     req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
            	// success
                document.contactform.submit();
            } else if (req.status == 204){
                // failure
                document.contactform.submit();
            } else {
            	document.contactform.submit();
            }
        }
     };
     req.open("GET", url, true);
     req.send(null);
 }
