// JavaScript Document
var str = document.title;
var page = str.substring(str.indexOf("::") + 3, str.length);
var redFaq = false;

function displayPageTitle() {
  document.write(page.toLowerCase());
}

function openForgot() {
  var openPop = location.search.substring(1);
  if (openPop == 'forgot') {
    showPopup(500, 175);
  }
}

function showPopup(w, h){
  var popUp = document.getElementById("popupContent");
  var popUpBackground = document.getElementById("popupBackground");
  
  popUp.style.top = (document.body.clientHeight) ? (document.body.clientHeight-h)/2 + "px" : 0;
  popUp.style.left = (document.body.clientWidth) ? (document.body.clientWidth-w)/2 + "px" : 0;
  popUp.style.width = w + "px";
  popUp.style.height = h + "px";

  popUpBackground.style.top = "0px";
  popUpBackground.style.left = "0px";
  popUpBackground.style.width = screen.availWidth + "px";
  popUpBackground.style.height = screen.availHeight + "px";
  
  popUp.style.visibility = "visible";
  popUpBackground.style.visibility = "visible";
}

function hidePopup() {   
  var popUp = document.getElementById("popupContent");
  var popUpBackground = document.getElementById("popupBackground");
  
  popUp.style.visibility = "hidden";
  popUpBackground.style.visibility = "hidden";  
}

function wopen(url, name, w, h, location) {
  var location = location || "no";
  var win = window.open(url, name ,'width=' + w + ', height=' + h + ', location=' + location + ', menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes, top=0');
  win.focus();
}

function dateMod(number) {
  var currentDate = new Date();
  var sec = currentDate.getSeconds();
  var modResult =  sec % number;
  return modResult;
}

function highlightFaq(linkId) {
  clearHighlightFaq();

  var faq = document.getElementById(linkId);
  var top = document.getElementById("TOP" + linkId);
  var link = document.getElementById("LINK" + linkId);
  faq.className = "linkRed";
  top.className = "linkRed";
  link.className = "linkRed";
  redFaq = linkId;
}

function clearHighlightFaq() {
  if(redFaq) {
    var prevFaq = document.getElementById(redFaq);
    var prevTop = document.getElementById("TOP" + redFaq);
    var prevLink = document.getElementById("LINK" + redFaq);
    prevFaq.className = "linkGray"; 
    prevTop.className = "linkGray"; 
    prevLink.className = "linkGray"; 
  }
  
}

function displayAnchor(id, num){
  var anchor = document.getElementById("anchor" + num);
  anchor.href = "#" + id;
}

function checkform(form)
{
  if (form.firstName.value == "") {
    alert( "Please fill in First Name." );
    form.firstName.focus();
    return false ;
  }
  if (form.lastName.value == "") {
    alert( "Please fill in Last Name." );
    form.lastName.focus();
    return false ;
  }
  if ((form.email.value == "") || !(EmailIsValid(form.email.value))) {
    alert( "Please fill in valid Email." );
    form.email.focus();
    return false ;
  }
  if ((form.reTypeEmail.value != form.email.value)) {
    alert( "Please Re-Type the email in the above field." );
    form.reTypeEmail.focus();
    return false ;
  }
  if (form.phone.value == "") {
    alert( "Please fill in Phone." );
    form.phone.focus();
    return false ;
  }
  if (form.hearAbout.value == "") {
    alert( "Please select how you heard about us." );
    form.hearAbout.focus();
    return false ;
  }
  if (form.companyName.value == "") {
    alert( "Please fill Company Name." );
    form.companyName.focus();
    return false ;
  }
  if (form.department.value == "") {
    alert( "Please fill Department." );
    form.department.focus();
    return false ;
  }
  if (form.companyAddr1.value == "") {
    alert( "Please fill in Company Address 1." );
    form.companyAddr1.focus();
    return false ;
  }
  if (form.companyCity.value == "") {
    alert( "Please fill in City." );
    form.companyCity.focus();
    return false ;
  }
  if (form.companyState.value == "") {
    alert( "Please select a State." );
    form.companyState.focus();
    return false ;
  }
  if (form.companyZip.value == "") {
    alert( "Please fill in Zip." );
    form.companyZip.focus();
    return false ;
  }
/* Use in ReCAPTCHA
  if (Recaptcha.get_response() == "") {
    alert( "Please type the two words in validation box." );
    form.recaptcha_response_field.focus();
    return false ;
  }
  
  verifyReCaptcha();
*/

  return true ;
}

function verifyReCaptcha() {
  var xmlhttp = null;
  var privatekey = "6LchlAUAAAAAAIVwrDnJWM_QuMjKv62FnUJPv4HY";
  var remoteip = '65.104.232.98';
  //var remoteip = '<!--#echo var="REMOTE_ADDR"-->';
  var challenge = Recaptcha.get_challenge();
  var response = Recaptcha.get_response();
  
  //url = http://api-verify.recaptcha.net/verify?privatekey=6LeFvwUAAAAAAMK0Qruj8MhQlR1qZ6qbCVw54vHH&remoteip=65.104.232.98&challenge=&response=
  var url = "http://api-verify.recaptcha.net/verify?privatekey=" + privatekey + "&remoteip=" + remoteip + "&challenge=" + challenge + "&response=" + response;
  //alert(url);
  
  document.write(url);
  
  if (window.XMLHttpRequest) {
    // code for IE7, Firefox, Opera, etc.
    xmlhttp=new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlhttp!=null) {
    return xmlhttp.open("GET", url, false);
  }
  else {
    alert("Your browser does not support XMLHTTP.");
  }
}

function verifyXmlHttp() {
  var xmlhttp = null;
  var url = "http://www.hpdi.com";
  
  if (window.XMLHttpRequest) {
    // code for IE7, Firefox, Opera, etc.
    xmlhttp=new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlhttp!=null) {
    return xmlhttp.open("POST", url, false);
  }
  else {
    alert("Your browser does not support XMLHTTP.");
  }
}

function EmailIsValid(checkThisEmail)
{
  var myEMailIsValid = true;
  var myAtSymbolAt = checkThisEmail.indexOf('@');
  var myLastDotAt = checkThisEmail.lastIndexOf('.');
  var mySpaceAt = checkThisEmail.indexOf(' ');
  var myLength = checkThisEmail.length;

  if (myAtSymbolAt < 1 ) {myEMailIsValid = false}
   
  if (myLastDotAt < myAtSymbolAt) {myEMailIsValid = false}

  if (myLength - myLastDotAt <= 2) {myEMailIsValid = false}

  if (mySpaceAt != -1) {myEMailIsValid = false}

  return myEMailIsValid;
}

function setStateDropDown() {
  var countryIndex = document.add_user.companyCountry.selectedIndex;
  var country = document.add_user.companyCountry.options[countryIndex].value;
  //alert("country: " + country);

  if (country == "unitedStates") {
    document.add_user.companyState.options.length = 51;
    document.add_user.companyState.options[0].value = "";
    document.add_user.companyState.options[0].text = "";
    document.add_user.companyState.options[1].value = "AL";
    document.add_user.companyState.options[1].text = "AL";
    document.add_user.companyState.options[2].value = "AR";
    document.add_user.companyState.options[2].text = "AR";
    document.add_user.companyState.options[3].value = "AZ";
    document.add_user.companyState.options[3].text = "AZ";
    document.add_user.companyState.options[4].value = "CA";
    document.add_user.companyState.options[4].text = "CA";
    document.add_user.companyState.options[5].value = "CO";
    document.add_user.companyState.options[5].text = "CO";
    document.add_user.companyState.options[6].value = "CT";
    document.add_user.companyState.options[6].text = "CT";
    document.add_user.companyState.options[7].value = "DC";
    document.add_user.companyState.options[7].text = "DC";
    document.add_user.companyState.options[8].value = "DE";
    document.add_user.companyState.options[8].text = "DE";
    document.add_user.companyState.options[9].value = "FL";
    document.add_user.companyState.options[9].text = "FL";
    document.add_user.companyState.options[10].value = "GA";
    document.add_user.companyState.options[10].text = "GA";
    document.add_user.companyState.options[11].value = "HI";
    document.add_user.companyState.options[11].text = "HI";
    document.add_user.companyState.options[12].value = "IA";
    document.add_user.companyState.options[12].text = "IA";
    document.add_user.companyState.options[13].value = "ID";
    document.add_user.companyState.options[13].text = "ID";
    document.add_user.companyState.options[14].value = "IL";
    document.add_user.companyState.options[14].text = "IL";
    document.add_user.companyState.options[15].value = "IN";
    document.add_user.companyState.options[15].text = "IN";
    document.add_user.companyState.options[16].value = "KS";
    document.add_user.companyState.options[16].text = "KS";
    document.add_user.companyState.options[17].value = "KY";
    document.add_user.companyState.options[17].text = "KY";
    document.add_user.companyState.options[18].value = "LA";
    document.add_user.companyState.options[18].text = "LA";
    document.add_user.companyState.options[19].value = "MA";
    document.add_user.companyState.options[19].text = "MA";
    document.add_user.companyState.options[20].value = "MD";
    document.add_user.companyState.options[20].text = "MD";
    document.add_user.companyState.options[21].value = "ME";
    document.add_user.companyState.options[21].text = "ME";
    document.add_user.companyState.options[22].value = "MI";
    document.add_user.companyState.options[22].text = "MI";
    document.add_user.companyState.options[23].value = "MN";
    document.add_user.companyState.options[23].text = "MN";
    document.add_user.companyState.options[24].value = "MO";
    document.add_user.companyState.options[24].text = "MO";
    document.add_user.companyState.options[25].value = "MS";
    document.add_user.companyState.options[25].text = "MS";
    document.add_user.companyState.options[26].value = "MT";
    document.add_user.companyState.options[26].text = "MT";
    document.add_user.companyState.options[27].value = "NC";
    document.add_user.companyState.options[27].text = "NC";
    document.add_user.companyState.options[28].value = "ND";
    document.add_user.companyState.options[28].text = "ND";
    document.add_user.companyState.options[29].value = "NE";
    document.add_user.companyState.options[29].text = "NE";
    document.add_user.companyState.options[30].value = "NH";
    document.add_user.companyState.options[30].text = "NH";
    document.add_user.companyState.options[31].value = "NJ";
    document.add_user.companyState.options[31].text = "NJ";
    document.add_user.companyState.options[32].value = "NM";
    document.add_user.companyState.options[32].text = "NM";
    document.add_user.companyState.options[33].value = "NV";
    document.add_user.companyState.options[33].text = "NV";
    document.add_user.companyState.options[34].value = "NY";
    document.add_user.companyState.options[34].text = "NY";
    document.add_user.companyState.options[35].value = "OH";
    document.add_user.companyState.options[35].text = "OH";
    document.add_user.companyState.options[36].value = "OK";
    document.add_user.companyState.options[36].text = "OK";
    document.add_user.companyState.options[37].value = "OR";
    document.add_user.companyState.options[37].text = "OR";
    document.add_user.companyState.options[38].value = "PA";
    document.add_user.companyState.options[38].text = "PA";
    document.add_user.companyState.options[39].value = "RI";
    document.add_user.companyState.options[39].text = "RI";
    document.add_user.companyState.options[40].value = "SC";
    document.add_user.companyState.options[40].text = "SC";
    document.add_user.companyState.options[41].value = "SD";
    document.add_user.companyState.options[41].text = "SD";
    document.add_user.companyState.options[42].value = "TN";
    document.add_user.companyState.options[42].text = "TN";
    document.add_user.companyState.options[43].value = "TX";
    document.add_user.companyState.options[43].text = "TX";
    document.add_user.companyState.options[44].value = "UT";
    document.add_user.companyState.options[44].text = "UT";
    document.add_user.companyState.options[45].value = "VA";
    document.add_user.companyState.options[45].text = "VA";
    document.add_user.companyState.options[46].value = "VT";
    document.add_user.companyState.options[46].text = "VT";
    document.add_user.companyState.options[47].value = "WA";
    document.add_user.companyState.options[47].text = "WA";
    document.add_user.companyState.options[48].value = "WI";
    document.add_user.companyState.options[48].text = "WI";
    document.add_user.companyState.options[49].value = "WV";
    document.add_user.companyState.options[49].text = "WV";
    document.add_user.companyState.options[50].value = "WY";
    document.add_user.companyState.options[50].text = "WY";
  }
  else if (country == "canada") {
    document.add_user.companyState.options.length = 14;
    document.add_user.companyState.options[0].value = "";
    document.add_user.companyState.options[0].text = "";
    document.add_user.companyState.options[1].value = "AB";
    document.add_user.companyState.options[1].text = "AB";
    document.add_user.companyState.options[2].value = "BC"; 
    document.add_user.companyState.options[2].text = "BC";
    document.add_user.companyState.options[3].value = "MB"; 
    document.add_user.companyState.options[3].text = "MB";
    document.add_user.companyState.options[4].value = "NB"; 
    document.add_user.companyState.options[4].text = "NB";
    document.add_user.companyState.options[5].value = "NL"; 
    document.add_user.companyState.options[5].text = "NL";
    document.add_user.companyState.options[6].value = "NT"; 
    document.add_user.companyState.options[6].text = "NT";
    document.add_user.companyState.options[7].value = "NS"; 
    document.add_user.companyState.options[7].text = "NS";
    document.add_user.companyState.options[8].value = "NU"; 
    document.add_user.companyState.options[8].text = "NU";
    document.add_user.companyState.options[9].value = "ON"; 
    document.add_user.companyState.options[9].text = "ON";
    document.add_user.companyState.options[10].value = "PE"; 
    document.add_user.companyState.options[10].text = "PE";
    document.add_user.companyState.options[11].value = "QC"; 
    document.add_user.companyState.options[11].text = "QC";
    document.add_user.companyState.options[12].value = "SK"; 
    document.add_user.companyState.options[12].text = "SK";
    document.add_user.companyState.options[13].value = "YT"; 
    document.add_user.companyState.options[13].text = "YT";
  }
  else if (country == "unitedKingdom") {
    document.add_user.companyState.options.length = 2;
    document.add_user.companyState.options[0].value = "";
    document.add_user.companyState.options[0].text = "";
    document.add_user.companyState.options[1].value = "UK";
    document.add_user.companyState.options[1].text = "UK";
    document.add_user.companyState.options[1].selected = true;
  }
}