var postState = '';
var postCountry = '';

// State table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var state = '\
USA:AL:Alabama|\
USA:AK:Alaska|\
USA:AZ:Arizona|\
USA:AR:Arkansas|\
USA:CA:California|\
USA:CO:Colorado|\
USA:CT:Connecticut|\
USA:DC:District of Columbia|\
USA:DE:Delaware|\
USA:FL:Florida|\
USA:GA:Georgia|\
USA:HI:Hawaii|\
USA:ID:Idaho|\
USA:IL:Illinois|\
USA:IN:Indiana|\
USA:IA:Iowa|\
USA:KS:Kansas|\
USA:KY:Kentucky|\
USA:LA:Louisiana|\
USA:ME:Maine|\
USA:MD:Maryland|\
USA:MA:Massachusetts|\
USA:MI:Michigan|\
USA:MN:Minnesota|\
USA:MS:Mississippi|\
USA:MO:Missouri|\
USA:MT:Montana|\
USA:NE:Nebraska|\
USA:NV:Nevada|\
USA:NH:New Hampshire|\
USA:NJ:New Jersey|\
USA:NM:New Mexico|\
USA:NY:New York|\
USA:NC:North Carolina|\
USA:ND:North Dakota|\
USA:OH:Ohio|\
USA:OK:Oklahoma|\
USA:OR:Oregon|\
USA:PA:Pennsylvania|\
USA:RI:Rhode Island|\
USA:SC:South Carolina|\
USA:SD:South Dakota|\
USA:TN:Tennessee|\
USA:TX:Texas|\
USA:UT:Utah|\
USA:VT:Vermont|\
USA:VA:Virginia|\
USA:WA:Washington|\
USA:WV:West Virginia|\
USA:WI:Wisconsin|\
USA:WY:Wyoming|\
Canada:AB:Alberta|\
Canada:BC:British Columbia|\
Canada:MB:Manitoba|\
Canada:NB:New Brunswick|\
Canada:NL:Newfoundland|\
Canada:NT:Northwest Territories|\
Canada:NS:Nova Scotia|\
Canada:NU:Nunavut|\
Canada:ON:Ontario|\
Canada:PE:Prince Edward Island|\
Canada:QC:Quebec|\
Canada:SK:Saskatchewan|\
Canada:YT:Yukon Territory|\
';

// Country data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var country = '\
Angola:Angola|\
Argentina:Argentina|\
Australia:Australia|\
Bahamas:Bahamas|\
Bahrain:Bahrain|\
Belgium:Belgium|\
Belize:Belize|\
Bolivia:Bolivia|\
Bosnia-Herzegovina:Bosnia-Herzegovina|\
Brazil:Brazil|\
Bristish Virgin Islands:Bristish Virgin Islands|\
Bulgaria:Bulgaria|\
Canada:Canada|\
Chile:Chile|\
China:China|\
Colombia:Colombia|\
Costa Rica:Costa Rica|\
Croatia:Croatia|\
Cyprus:Cyprus|\
Czech Republic:Czech Republic|\
Denmark:Denmark|\
Dominican Republic:Dominican Republic|\
Ecuador:Ecuador|\
Egypt:Egypt|\
Estonia:Estonia|\
Finland:Finland|\
France:France|\
Germany:Germany|\
Ghana:Ghana|\
Greece:Greece|\
Guam:Guam|\
GT:Guatemala|\
Hong-Kong:Hong-Kong|\
Hungary:Hungary|\
India:India|\
Indonesia:Indonesia|\
Ireland:Ireland|\
Israel:Israel|\
Italy:Italy|\
Japan:Japan|\
Jordan:Jordan|\
Kenya:Kenya|\
Korea:Korea|\
Kuwait:Kuwait|\
Latvia:Latvia|\
Lebanon:Lebanon|\
Libyan Arab Jamahir:Libyan Arab Jamahir|\
Lithuania:Lithuania|\
Luxembourg:Luxembourg|\
Malaysia:Malaysia|\
Malta:Malta|\
Mauritius:Mauritius|\
Mexico:Mexico|\
Moldavia:Moldavia|\
Morocco:Morocco|\
Mozambique:Mozambique|\
Netherlands:Netherlands|\
New Zealand:New Zealand|\
Nicaragua:Nicaragua|\
Nigeria:Nigeria|\
Norway:Norway|\
Oman:Oman|\
Pakistan:Pakistan|\
Panama:Panama|\
Paraguay:Paraguay|\
Peru:Peru|\
Philippines:Philippines|\
Poland:Poland|\
Portugal:Portugal|\
Romania:Romania|\
Russia:Russia|\
Saudi Arabia:Saudi Arabia|\
Singapore:Singapore|\
Slovakia:Slovakia|\
South Africa:South Africa|\
Spain:Spain|\
Sri Lanka:Sri Lanka|\
St. Lucia:St. Lucia|\
Sweden:Sweden|\
Switzerland:Switzerland|\
Syria:Syria|\
Taiwan:Taiwan|\
Tanzania:Tanzania|\
Thailand:Thailand|\
Turkey:Turkey|\
Ukraine:Ukraine|\
UK:United Kingdom|\
USA:United States|\
Uruguay:Uruguay|\
Venezuela:Venezuela|\
Vietnam:Vietnam|\
Yugoslavia:Yugoslavia|\
Zambia:Zambia|\
ZW:Zimbabwe\
';

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById('00NA00000020dLP');
  selObj.options[0] = new Option('Please Select','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' ) {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateState() {
  var selObj = document.getElementById('00NA00000020dLK');
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('Please Select','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with states from the selected country
  var stateLineArray = state.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < stateLineArray.length; loop++) {
    lineArray = stateLineArray[loop].split(":");
    countryCode  = TrimString(lineArray[0]);
    stateCode    = TrimString(lineArray[1]);
    stateName    = TrimString(lineArray[2]);
  if (document.getElementById('00NA00000020dLP').value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('00NA00000020dLK').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","00NA00000020dLK");
        inputSel.setAttribute("id","00NA00000020dLK");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('00NA00000020dLK');
        selObj.options[0] = new Option('Please Select','');
        selObj.selectedIndex = 0;
      }
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
      if ( stateCode == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
   parentObj = document.getElementById('00NA00000020dLK').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
   var inputEl = document.createElement("SELECT");
    inputEl.setAttribute("id", "00NA00000020dLK");
    inputEl.setAttribute("name", "00NA00000020dLK");
    inputEl.setAttribute("class", "formfield");
    inputEl.setAttribute("value", postState);
	parentObj.appendChild(inputEl) ;
	newObj = document.getElementById('00NA00000020dLK');
    newObj.options[0] = new Option('N/A','N/A');
    newObj.selectedIndex = 0;
  }
}

function textCounter(theField, charsLeft, maxChars) {
	if (theField.value.length > maxChars) {
		theField.value = theField.value.substring(0, maxChars);
	} else {
		charsLeft.value = maxChars - theField.value.length;
	}
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}


function getURL(){
	var url = window.location.href;
	document.forms[0].elements['00NA0000003EAXE'].value = url;
}

function getTY(tyDir){
	var thnx = window.location.protocol + "//" + window.location.host + tyDir;
	document.forms[0].elements['retURL'].value = thnx;
}

function getSF(){
	var url = "" + window.location.host;
	if(url.indexOf("svoreview")>0){
	   document.forms[0].action = 'https://test.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
	   document.forms[0].elements['oid'].value = '00DT0000000HKcM';
	   document.forms[0].elements['Campaign_ID'].value = '701T0000000AMdD';
	} else {
	   document.forms[0].action = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
	   document.forms[0].elements['oid'].value = '00DA0000000JKL3';
	}
}

function getTags(){
	document.forms[0].elements['00NA00000020n11'].value = getCookie('EM');
	document.forms[0].elements['00NA00000020moI'].value = getCookie('IM');
	document.forms[0].elements['00NA00000020n16'].value = getCookie('PS');
	document.forms[0].elements['utm_source'].value = getCookie('utm_source');
	document.forms[0].elements['utm_medium'].value = getCookie('utm_medium');
	document.forms[0].elements['utm_term'].value = getCookie('utm_term');
	document.forms[0].elements['utm_content'].value = getCookie('utm_content');
	document.forms[0].elements['utm_campaign'].value = getCookie('utm_campaign');
	document.forms[0].elements['00NA0000006KAt1'].value = getCookie('formtest');
}

$(document).ready(function(){
    $.validationEngineLanguage.newLang();
    $("#formID").validationEngine();
	$(".popupwindow").popupwindow();
	$.getJSON("http://jsonip.appspot.com?callback=?",function getIP(data){
		$('#REMOTEADDR').attr('value', data.ip);
	});
});
