var httpObj;

function checkPartnerSubmit(){
  document.getElementById("results_submit").blur();
  var emailValue = Trim(document.getElementById("email").value);
  if (emailValue != "" && check_email(emailValue)){
    alert("Please type a valid email address");
    return false;
  }
  document.getElementById("results_submit").disabled = true;
  document.getElementById("results_submit").style.backgroundColor = "#718571";
  document.getElementById("results_submit").style.color = "#b7b7b7";
  document.getElementById("results_submit_text").innerHTML = "sending";  
  document.getElementById("results_wait").style.display = "inline";
  document.getElementById("results_submit").blur();
  var dUrl = "submitPartners.php";
  var params = "email=" +  escape(emailValue);
  params    += "&website=" +  escape(document.getElementById("website").value);
  params    += "&comments=" + escape(document.getElementById("comments").value);
  httpObj = createRequestObject();
  httpObj.open('POST', dUrl, true);
  httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpObj.setRequestHeader("Content-length", params.length);
  httpObj.setRequestHeader("Connection", "close");
  httpObj.onreadystatechange = handleSubmitResponse;
  httpObj.send(params);
}

function handleSubmitResponse(){
  if(httpObj.readyState == 4 && httpObj.status == 200){
    if(httpObj.responseText == "success"){
      document.getElementById("results_submit").style.display = "none";
      document.getElementById("submitted").style.display = "inline";
    } else {
      alert("Error, please send an email to us: info@guessthelogo.com");
      document.getElementById("results_submit").disabled = false;
      document.getElementById("results_submit").style.backgroundColor = "#008000";
      document.getElementById("results_submit").style.color = "#ffffff";
      document.getElementById("results_submit_text").innerHTML = "Submit ->";  
      document.getElementById("results_wait").style.display = "none";
    }
  }
}

function checkSubmit(){
  var ok = "1234567890qwertyuiopasdfghjklzxcvbnm .@-_QWERTYUIOPASDFGHJKLZXCVBNM";
  var curName = (Trim(document.getElementById("name").value));
  for (var i = 0; i < curName.length; i++){
    if (ok.indexOf(curName.charAt(i)) < 0){
      alert("Name can contain letters, numbers, spaces '-', '_', '.' and '@'s");
      return false;
    }
  }
  var emailValue = Trim(document.getElementById("email").value);
  if (emailValue != "" && !check_email(emailValue)){
    alert("Please type a valid email address");
    return false;
  }
  document.getElementById("submit").disabled = true;
  return true;
}

function check_email(e) {
  var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
  for(i = 0; i < e.length; i++)
    if (ok.indexOf(e.charAt(i)) < 0)
      return (true);
  if (document.images) {
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two))
      return (true);
  }
  return false;
}

function Trim(TRIM_VALUE){
  if(TRIM_VALUE.length < 1){
    return "";
  }
  TRIM_VALUE = RTrim(TRIM_VALUE);
  TRIM_VALUE = LTrim(TRIM_VALUE);
  if(TRIM_VALUE==""){
    return "";
  } else {
    return TRIM_VALUE;
  }
} //End Function

function RTrim(VALUE){
  var w_space = String.fromCharCode(32);
  var v_length = VALUE.length;
  var strTemp = "";
  if(v_length < 0){
    return"";
  }
  var iTemp = v_length -1;
  while(iTemp > -1){
    if(VALUE.charAt(iTemp) != w_space){
      strTemp = VALUE.substring(0,iTemp +1);
      break;
    }
    iTemp = iTemp-1;
  } //End While
  return strTemp;
} //End Function

function LTrim(VALUE){
  var w_space = String.fromCharCode(32);
  if(v_length < 1){
    return"";
  }
  var v_length = VALUE.length;
  var strTemp = "";
  var iTemp = 0;
  while(iTemp < v_length){
    if(VALUE.charAt(iTemp) != w_space){
      strTemp = VALUE.substring(iTemp,v_length);
      break;
    }
    iTemp = iTemp + 1;
  } //End While
  return strTemp;
} //End Function

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == 'Microsoft Internet Explorer'){
        ro = new ActiveXObject('Microsoft.XMLHTTP');
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}