//Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;

//Check if we are using IE.

//If the Javascript version is greater than 5.
if (window.ActiveXObject){
 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 if (!xmlhttp){
  //If not, then use the older active x object.
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  if (!xmlhttp){
   xmlhttp = false;
  }
 }
}

//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
 xmlhttp = new XMLHttpRequest();
}

function requestget(serverPage, objID) {
 var obj = document.getElementById(objID);
 xmlhttp.open("GET", serverPage);
 xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   obj.innerHTML = xmlhttp.responseText;
  } else {
   obj.innerHTML = "<div style='width:92px;height:13px;position:absolute;left:500px;'>processing...</div>";
  }
 }
 xmlhttp.send(null);
}

function requestpost(formname,serverPage,objID) {
 var obj = document.getElementById(objID);
 var fobj=document.getElementById(formname);
 var str = "";
 for(var i = 0; i < fobj.elements.length; i++){
  str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
 }
 xmlhttp.open("POST", serverPage);
 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
 xmlhttp.setRequestHeader("Content-length", str.length);
 xmlhttp.setRequestHeader("Connection", "close");
 xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   obj.innerHTML = xmlhttp.responseText;
  } else {
   obj.innerHTML = "<div style='width:92px;height:13px;position:absolute;left:500px;'>processing...</div>";
  }
 }
 xmlhttp.send(str);
}

function domainsubmit(domname,tldname,serverPage,objID){
 var str = "";
 if (validateDomain(domname)){
  str = "domain=" + escape(domname) + "&";
	str += "tld=" + escape(tldname);
  if (objID!="") str += "&content=yes";
  xmlhttp.open("POST", serverPage);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
  xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    if (objID!=""){
     if (xmlhttp.responseText=="OK"){
      window.location="https://www.farnhost.com/order.php?domain="+domname+"."+tldname+"&chk=yes";
     } else {
      document.getElementById(objID).innerHTML = xmlhttp.responseText;
     }
    } else {
     if (xmlhttp.responseText=="OK"){
      document.getElementById("domainstat").src="ok.jpg";
      document.getElementById("stattxt").innerHTML="Available";
     } else if (xmlhttp.responseText=="NOT OK") {
      document.getElementById("domainstat").src="not_ok.png";
      document.getElementById("stattxt").innerHTML="Not Available";
     } else {
      document.getElementById("domainstat").src="qmark.gif";
      document.getElementById("stattxt").innerHTML="Verify Error / Invalid Domain";
     }
    }
   } else {
    if (objID==""){
     document.getElementById("domainstat").src="verify_txt.gif";
     document.getElementById("stattxt").innerHTML="";
    }
   }
  }
  xmlhttp.send(str);
 }
}

function ordersubmit(formname,serverPage,objID){
 var str = "", prevstr=""; 
 fobj=document.getElementById(formname);
 if (validateOrder()){
  //Run through a list of all objects contained within the form.
  for(var i = 0; i < fobj.elements.length; i++){
	 prevstr = fobj.elements[i].name;
	 if (fobj.elements[i].type=="radio"){
	  if (fobj.elements[i].checked) str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	 } else {
    str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	 }
	}
  var obj = document.getElementById(objID);
  xmlhttp.open("POST", serverPage);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
  xmlhttp.setRequestHeader("Content-length", str.length);
  xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    obj.innerHTML = xmlhttp.responseText;
   } else {
    obj.innerHTML = "<div style='width:92px;height:13px;position:absolute;left:500px;'>processing...</div>";
   }
  }
  xmlhttp.send(str);
 }
}

function validateDomain(strDom)
{
	if(strDom == "")
	{
    document.getElementById("domainstat").src="qmark.gif";
    document.getElementById("stattxt").innerHTML="Verify Error / Invalid Domain";
		alert("Please enter valid domain name in the box.");
		return false;
	}
	if(strDom.length < 3)
	{
		alert("Please enter at least 3 characters in the domain name box.");
		return false;
	}
	if(strDom.length > 63)
	{
		alert("Please enter at most 63 characters in the domain name box.");
		return false;
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789--.";
	var checkStr = strDom;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if(ch == checkOK.charAt(j))
				break;
		
		if(j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
  	if(checkStr.charAt(0)=="-"||checkStr.charAt(checkStr.length-1)=="-")
  	{
    		allValid = false;
  	}
  	if(!allValid)
  	{
    		alert("Please enter only letters, digits and \"-\" characters in the domain name box.\n\nRemembering that domains cannot begin or end with \"-\"");
    		return false;
  	}
  	return true;
}

/* Validating Order Form */
function validateOrder()
{
 if (document.orderForm.chkreg.checked&&document.orderForm.domain.value==""){
  alert("Please Enter Domain Name");
  document.orderForm.domain.focus();
  return false;
 }

 /* Check the hostplan selected */
 var planhost=-1;
 for (i=0;i<document.orderForm.hostplan.length;i++){
  if (document.orderForm.hostplan[i].selected){
   planhost=i;
   break; //exist for loop, as target acquired.
  }
 }

 if(planhost < 0){
  alert("Please choose one of the web hosting plan.");
  document.orderForm.hostplan[0].focus();
  return false;
 }

 if (document.orderForm.clientuser.value==""){
  alert("Please specify user's name.");
  document.orderForm.clientuser.focus();
  return false;
 }
	if (document.orderForm.clientpw1.value=="")
	{
		alert("Please enter password.");
		document.orderForm.clientpw1.focus();
		return false;
	}
	if (document.orderForm.clientpw2.value=="")
	{
		alert("Please re-type password.");
		document.orderForm.clientpw2.focus();
		return false;
	}
	if (document.orderForm.clientpw1.value!=document.orderForm.clientpw2.value)
	{
		alert("Password does not match.");
		document.orderForm.clientpw1.focus();
		return false;
	}
	if (document.orderForm.clientfname.value=="")
	{
		alert("Please enter your First Name.");
		document.orderForm.clientfname.focus();
		return false;
	}
	if (document.orderForm.clientlname.value=="")
	{
		alert("Please enter your Last Name.");
		document.orderForm.clientlname.focus();
		return false;
	}
	if (document.orderForm.clientaddr.value=="")
	{
		alert("Please enter your Mailing Address.");
		document.orderForm.clientaddr.focus();
		return false;
	}
  emailvalid=0;
// courtesy of http://javascript.internet.com
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.orderForm.clientemail.value))
//	if (document.orderForm.clientemail.value=="")
	{
    emailvalid=1;
  }
  if (emailvalid==0){
		alert("Please enter Valid E-mail Address.");
		document.orderForm.clientemail.focus();
    return false;
	}
	if (document.orderForm.clientcont.value=="")
	{
		alert("Please enter your Contact No.");
		document.orderForm.clientcont.focus();
		return false;
	}
	//if (getCookie("ordcode")!=hex_md5(document.orderForm.ordcode.value))
	//{
	//	alert("Invalid Order Code");
	//	document.orderForm.ordcode.focus();
	//	return false;
	//}
	if (document.orderForm.usage_policy.checked==false)
	{
		alert("Please read and accept Usage Policy Agreement.");
		document.orderForm.usage_policy.focus();
		return false;
	}
	if (document.orderForm.service_terms.checked==false)
	{
		alert("Please accept Terms of Service.");
		document.orderForm.service_terms.focus();
		return false;
	}
  document.orderForm.orderok.value="ok";
	document.orderForm.submit();
}

function changeplan()
{
 var hostptr=0, regprice=0, totprice=0;
 for (i=0;i<document.orderForm.hostplan.length;i++) {
  if (document.orderForm.hostplan[i].selected) { hostptr=i; break;} 			
 }
 if (document.orderForm.chkreg.checked){
  regprice=document.orderForm.domreg.value;
  document.orderForm.regprice.value=regprice;
 } else {
  regprice=0;
  document.orderForm.regprice.value="0.00";
 }
 hostplan=document.orderForm.hostplan[hostptr].value;
 planvalue=document.getElementById(hostplan).value;
 planvalue=planvalue.substr(0,planvalue.indexOf("."));
 if (planvalue.length > 3) strplan=planvalue.substr(0,1)+","+planvalue.substr(1,planvalue.length);
 else strplan=planvalue;
/*
 if (document.orderForm.payscheme[0].checked){
  planfee = parseInt(planvalue)*3;
  document.orderForm.submonth.value="x 3";
 } else if (document.orderForm.payscheme[1].checked){
  planfee = parseInt(planvalue)*6;
  document.orderForm.submonth.value="x 6";
 } else if (document.orderForm.payscheme[2].checked){
  planfee = parseInt(planvalue)*12;
  document.orderForm.submonth.value="x 12";
 }
*/
 planfee = parseInt(planvalue)*12;
 strtotfee=planfee.toString();
 if (strtotfee.length > 3) strtotfee=strtotfee.substr(0,1)+","+strtotfee.substr(1,strtotfee.length);
 totprice = planfee+parseInt(regprice);
 strtotal = totprice.toString();
 if (strtotal.length > 3) strtotal=strtotal.substr(0,1)+","+strtotal.substr(1,strtotal.length);
 document.orderForm.subfee.value = "PHP "+strplan+".00";
 document.orderForm.totalfee.value = "PHP "+strtotfee+".00";
 document.orderForm.totaldue.value = "PHP "+strtotal+".00";
 document.orderForm.dollarequi.value = dollarequiv(totprice);
 return true;
}

function changepay()
{
 var hostptr=0, regprice=0, totprice=0;
	for (i=0;i<6;i++)
	{
		 if (document.payform.paymode[i].checked) { 
			 hostptr=i; 
				break;
			} 			
	}

	if (hostptr==0){
	 	document.payform.cfname.disabled=true;
	 	document.payform.clname.disabled=true;
	 	document.payform.ccardno.disabled=true;
	 	document.payform.ctype.disabled=true;
	 	document.payform.cmonth.disabled=true;
	 	document.payform.cyear.disabled=true;
	 	document.payform.cccv2.disabled=true;
	} else { 
	 	document.payform.cfname.disabled=false;
	 	document.payform.clname.disabled=false;
	 	document.payform.ccardno.disabled=false;
	 	document.payform.ctype.disabled=false;
	 	document.payform.cmonth.disabled=false;
	 	document.payform.cyear.disabled=false;
	 	document.payform.cccv2.disabled=false;
  }
}

function dollarequiv(pesoamt){
 xrate = parseFloat(document.orderForm.xrate.value);
 n_dollar = pesoamt / xrate;
 if ((n_dollar-parseInt(n_dollar))>0){
  n_dollar = Math.round(n_dollar*100)/100;
  s_dollar = n_dollar.toString();
  n_cents = s_dollar.substr(s_dollar.indexOf(".")+1);
  s_dollar = s_dollar.substr(0,s_dollar.indexOf("."));
  n_cents = parseInt(n_cents);
  if (n_cents<10) n_cents = n_cents*10;
  outstr = "(USD "+s_dollar+"."+n_cents.toString()+")";
 } else {
  s_dollar = n_dollar.toString();
  outstr = "(USD "+s_dollar+ ".00)";
 }
 return outstr;	
}

function flashfile_DoFSCommand(command, args)
{
if ( command == "color" ) {document.bgColor=args}
}

function faq(param1) {
 var faqobj=document.getElementById(param1);
 if (faqobj.style.visibility=="hidden"){
  faqobj.style.visibility="visible";
  faqobj.style.display="block";
 } else {
  faqobj.style.visibility="hidden";
  faqobj.style.display="none";
 }
}

/* sending message */
function sendmsg(formname) {
 var fobj=document.getElementById(formname);
 /* Check the domain availability */
 if (fobj.firstname.value==""){
  alert("Please provide your first name");
  fobj.firstname.focus();
  return false;
 }
 if (fobj.lastname.value==""){
  alert("Please provide your last name.");
  fobj.lastname.focus();
  return false;
 }
 // courtesy of http://javascript.internet.com
 var emailvalid=0;
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(fobj.email.value)){
  emailvalid=1;
 }
 if (emailvalid==0){
  alert("Please provide valid e-mail address.");
  fobj.email.focus();
  return false;
 }
 if (fobj.address.value==""){
  alert("Please provide your address.");
  fobj.address.focus();
  return false;
 }
 if (fobj.txtmsg.value==""){
  alert("Please specify your message.");
  fobj.txtmsg.focus();
  return false;
 }
 if (fobj.seccode.value==""){
  alert("Please provide security code.");
  fobj.seccode.focus();
  return false;
 }
 var str = "";
 for(var i = 0; i < fobj.elements.length; i++){
  //str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
  str += fobj.elements[i].name + "=" + fobj.elements[i].value + "&";
 }
 xmlhttp.open("POST","sendmsg.php");
 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
 xmlhttp.setRequestHeader("Content-length", str.length);
 xmlhttp.setRequestHeader("Connection", "close");
 xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   alert(xmlhttp.responseText);
   requestget('contact.php','content');
  }
 }
 xmlhttp.send(str);
}

function clearfrm() {
var secno=10*Math.random();
 document.contactform.firstname.value="";
 document.contactform.lastname.value="";
 document.contactform.address.value="";
 document.contactform.txtmsg.value="";
 document.contactform.email.value="";
 document.contactform.seccode.value="";
 document.getElementById("seccode").src=document.getElementById("seccode").src+"&ref="+secno;
}

function chkdomain(domstr){
 var mypat=/(http)|(www.)|[\^\*()\[\]?!@#%$:;\"\'\<\>,\/]/gi;
 var tldpat=/(com)|(net)|(biz)|(info)|(org)/gi;
 var newstr=domstr.replace(mypat,"");
 var domain=newstr.substr(0,newstr.indexOf("."));
 var tld=newstr.substr(newstr.indexOf(".")+1);
 if (tld.search(tldpat)<0){
  alert("Domain Extension should only be \n .com .net .biz .info .org");
  return false;
 }
 domainsubmit(domain,tld,"domaincheck.php","");
}

