function createRequestObject() {
var xmlhttp = false;
   try {
     xmlhttp = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         xmlhttp = false;
       }  
     }
   }

   if (!xmlhttp) {
     alert("Error: Your Browser does not support this page!");
   } else {
	 return xmlhttp;
   }
}
var capp = createRequestObject();
var frmm = createRequestObject();
var subutton = document.getElementById("submit");
function getCaptcha() {
	var captchar = document.getElementById("captchar").value;
	var url = "/process/captcha/" + escape(captchar);
	capp.open("GET", url, true);
    capp.onreadystatechange = updateCaptcha;
    capp.send(null);
}
function updateCaptcha() {
	if (capp.readyState == 4) {
		if (capp.status == 200) {
			var response = capp.responseText;
			var captinput = document.getElementById("captchar");
			var capterr = document.getElementById("capterr");
			if (response == 'true') {
				capterr.innerHTML = '&nbsp;';
				captinput.removeAttribute('className');
				captinput.removeAttribute('class');
			} else {
				capterr.innerHTML = response;
				captinput.setAttribute('className','redborder');
				captinput.setAttribute('class','redborder');
			}
		} else {
			alert("Error:  " + capp.status);
		}
	}
}

function checkEmail() {
	var emailid = document.getElementById("emailid").value;
	var emailerr = document.getElementById("emailerr");
	var emailinput = document.getElementById("emailid");
	var emailchk = (emailid.indexOf(".") > 2) && (emailid.indexOf("@") > 0);
	if ((emailchk == false)|| (emailchk == 'undefined')) {
		emailerr.innerHTML = "<font color=\"#ff0000\"> <br />  Invalid email address.</font>";
		emailinput.setAttribute('className','redborder');
		emailinput.setAttribute('class','redborder');
	} else {
		emailid = escape(emailid);
		emailerr.innerHTML = "&nbsp;";
		emailinput.removeAttribute('className');
		emailinput.removeAttribute('class');
	}
}
function checkName() {
	var unametxt = document.getElementById("uname").value;
	var unamerr = document.getElementById("unamerr");
	var unametxtinput = document.getElementById("uname");
	if ((unametxt == '') || (unametxt == 'undefined')) {		
		unamerr.innerHTML = "<font color=\"#ff0000\"> <br /> Please enter your name.</font>";
		unametxtinput.setAttribute('className','redborder');
		unametxtinput.setAttribute('class','redborder');
	} else {
		unametxt = escape(unametxt);
		unamerr.innerHTML = "&nbsp;";
		unametxtinput.removeAttribute('className');
		unametxtinput.removeAttribute('class');
	}
}
function checkFirst(cf) {
	if (((cf.charAt(0)) == 7 ) || ((cf.charAt(0)) == 9 )) {
		return true;
	} else {
		return false;
	}
}
function checkMobileNo() {
	var mobilenotxt = document.getElementById("mobileno").value;
	var mobilnoerr = document.getElementById("mobileerr");
	var mobilenotxtinput = document.getElementById("mobileno");
	if (((mobilenotxt == '') || ((isNaN(mobilenotxt)) == true)) || (mobilenotxt.length < 7)) {		
		mobilnoerr.innerHTML = "<font color=\"#ff0000\"> <br /> Please enter a valid mobile number.</font>";
		mobilenotxtinput.setAttribute('className','redborder');
		mobilenotxtinput.setAttribute('class','redborder');
	} else if ((checkFirst(mobilenotxt)) == false) {
		mobilnoerr.innerHTML = "<font color=\"#ff0000\"> <br />Mobile number(s) should start with a '9' or a '7'.</font>";
		mobilenotxtinput.setAttribute('className','redborder');
		mobilenotxtinput.setAttribute('class','redborder');
	} else {
		mobilenotxt = escape(mobilenotxt);
		mobilnoerr.innerHTML = "&nbsp;";
		mobilenotxtinput.removeAttribute('className');
		mobilenotxtinput.removeAttribute('class');
	}
}
function checkComments() {
	var commtxt = document.getElementById("commtxt").value;
	var commerr = document.getElementById("commerr");
	var commtxtinput = document.getElementById("commtxt");
	if ((commtxt == '') || (commtxt == 'undefined')) {		
		commerr.innerHTML = "<font color=\"#ff0000\"> <br /> You did not enter any comments.</font>";
		commtxtinput.setAttribute('className','redborder');
		commtxtinput.setAttribute('class','redborder');
	} else {
		commtxt = escape(commtxt);
		commerr.innerHTML = "&nbsp;";
		commtxtinput.removeAttribute('className');
		commtxtinput.removeAttribute('class');
	}
}
function showLoad() {
	var loading = document.createElement("img");
	loading.setAttribute("src", "/images/loading.gif");
	loading.setAttribute("id", "loading");
	loading.setAttribute("width", "29");
	loading.setAttribute("height", "23");
	loading.setAttribute("alt", "Processing Form");
	// get location to insert loading image
	var frmHead = document.getElementById("frmHead");
	cmmHparent = frmHead.parentNode;
	cmmHparent.insertBefore(loading, frmHead);
}
function hideLoad() {
	var loading = document.getElementById("loading");
	loading.parentNode.removeChild(loading);
}
function submitForm() {
	showLoad();
	var captchar = document.getElementById("captchar").value; captchar = (captchar=='')?0:captchar;
	var commtxt = document.getElementById("commtxt").value; commtxt = (commtxt=='')?0:commtxt;
	var uname = document.getElementById("uname").value; uname = (uname=='')?0:uname;
	var emailid = document.getElementById("emailid").value; emailid = (emailid=='')?0:emailid;
	var mobileno = document.getElementById("mobileno").value; mobileno = (mobileno=='')?0:mobileno;
	var curl = location.href.split("/"); var curldata = (!curl[4]) ? 0: curl[4];
	var url = "/process";
	var urlparams = "/frmm/" + escape(captchar) + "/" + escape(uname) + "/" + escape(emailid) + "/" + escape(commtxt) + "/" + escape(mobileno) + "/" + escape(curldata);
	frmm.open("POST", url+urlparams, true);
	frmm.onreadystatechange = processComm;
	frmm.send("");
}
function processComm() {
	if (frmm.readyState == 4) {
		if (frmm.status == 200) {
			var xmldoc = frmm.responseXML;
			var errs = xmldoc.getElementsByTagName('errors').item(0).childNodes.item(0).data;
			if (errs == "true") {
				getCaptcha(); checkComments(); checkEmail(); checkName();
				alert("Error: Please review the marked fields.");
				hideLoad();
			} else {
				var success = xmldoc.getElementsByTagName('success').item(0).childNodes.item(0).data;
				document.getElementById("commtxt").value = '';
				document.getElementById("captchar").value = '';
				document.getElementById("uname").value = '';
				document.getElementById("emailid").value = '';
				//targetHTML = target.innerHTML;
				//target.innerHTML = successdata+targetHTML;
				var para = document.createElement("p");
				var text = document.createTextNode(success);
				para.setAttribute('className','msg');
				para.setAttribute("class","msg");
				para.appendChild(text);
				
				var emailus = document.getElementById("emailus");
				emailusParent = emailus.parentNode;
				emailusParent.insertBefore(para, emailus);
				hideLoad(); emailusParent.removeChild(emailus);
			}
		} else {
			alert("Error:  " + frmm.status);
			hideLoad();
		}
	}
}