var error = 0;
var queryString = "";

function GetXmlHttpObject () {
		var xmlHttp = null;
		try { // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
		}
		catch (e) { // Internet Explorer
		  try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		  catch (e) {
			try {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		    }
			catch (e) {
			  alert("Your browser does not support AJAX!");
			  return false;
		    }
	      }
		}
	return xmlHttp;
}

function checkEmail() {
    var email = document.getElementById("email");
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if (!filter.test(email.value)) {
        return 1;
	} else {
		return 0;
	}
}

function checkConf() {
		var conf = document.getElementById("conf");
		var filter = /^20(\d{12,})$/;
		if (!filter.test(conf.value)) {
			return 1;
		} else {
			return 0;
		}
}

function doLogic() {
		if (document.tracking.email.value != "") {
			error += checkEmail();
		}
		if (document.tracking.conf.value != "") {
			error += checkConf();
		}
		if (document.tracking.lname.value != "") {
			error++;
		}
		if (error > 0) {
			alert("Please provide valid values for at least two of the criteria.");
			document.tracking.email.focus();
		} else {
			ajaxFunction();
		}
}

function RMAOrder(s_witch) {
  xmlHttp = GetXmlHttpObject();
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4 && xmlHttp.status==200) {
	  var frame = document.getElementById("results");
      frame.innerHTML=xmlHttp.responseText;
      }
    }
  queryString = "/tools/rma.php?orderID=" + orderID + "&switch=" + s_witch;
  xmlHttp.open("GET",queryString,true);
  xmlHttp.send(null);
}

function TrackOrder(s_witch) {
  xmlHttp = GetXmlHttpObject();

  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4 && xmlHttp.status==200) {
	  var frame = document.getElementById("results");
      frame.innerHTML=xmlHttp.responseText;
      }
    }
  queryString = "/tools/tracking.php?switch=" + s_witch;

  if(s_witch == 0){
  	xmlHttp.open("GET",queryString,true);
	xmlHttp.send(null);
  }else{
	var params = "last_name="+document.tracking.last_name.value+"&pnref="+document.tracking.pnref.value+'&email='+document.tracking.email.value;
	xmlHttp.open("POST",queryString,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
  }
}

function inquiryForm() {
  xmlHttp = GetXmlHttpObject();
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4 && xmlHttp.status==200) {
	  var frame = document.getElementById("results");
	  frame.innerHTML="";
      frame.innerHTML=xmlHttp.responseText;
      }
    }
  var data = document.getElementById("inquiry");
  var fname = data.First_Name.value;
  var lname = data.Last_Name.value;
  var cc = data.CC_Number.value;
  var pnref = data.PNREF.value;
  var phone = data.phone.value;
  var email = data.email.value;
  var comments = data.comments.value;
  if (fname == "" || lname == "" || cc == "" || email == "") {
	alert("Please provide information for the required fields.");
  } else {
    queryString = "/tools/inquiry.php?fname=" + fname + "&lname=" + lname + "&cc=" + cc + "&pnref=" + pnref + "&phone=" + phone + "&email=" + email + "&comments=" + comments;
    xmlHttp.open("GET",queryString,true);
    xmlHttp.send(null);
  }
}

function cancelOrder(s_witch) {
  xmlHttp = GetXmlHttpObject();
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4 && xmlHttp.status==200) {
	  var frame = document.getElementById("results");
      frame.innerHTML=xmlHttp.responseText;
      }
    }
  queryString = "/tools/cancel.php?switch=" + s_witch;

  if(s_witch == 0){
        xmlHttp.open("GET",queryString,true);
        xmlHttp.send(null);
  }else{
        var params = "last_name="+document.cancel.last_name.value+"&pnref="+document.cancel.pnref.value+'&email='+document.cancel.email.value;
        xmlHttp.open("POST",queryString,true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", params.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.send(params);
  }
}

function ajaxFunction() {
  xmlHttp = GetXmlHttpObject();
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4 && xmlHttp.status==200) {
	  var frame = document.getElementById("results");
      frame.innerHTML=xmlHttp.responseText;
      }
    }
  queryString = "/tools/customers.php?email=" + document.tracking.email.value + "&conf=" + document.tracking.conf.value + "&lname=" + document.tracking.lname.value;
  xmlHttp.open("GET",queryString,true);
  xmlHttp.send(null);
}

