/* Create a new XMLHttpRequest object to talk to the Web server */

var xmlHttp = false;

var ERROR = "";


function init() {
	
	String.prototype.isEmail = function() // custom function to validate email address format
	{
	        return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
	};

	/* @cc_on @ */
	 //@if(@_jscript_version >= 5) 
		 try { xmlHttp = new	 ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { 
	 			try { xmlHttp = new	 ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } 
	 		}	//@end@
	 
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
	//alert(xmlHttp);
	
	return;
}

function submitForm() {
	giveMsg("<img src=\"img/running.gif\" alt=\"\"/>&nbsp;");
	//chgButton("hidden");
	window.setTimeout(sendReq, 500);
}

function sendReq() {
	var p1 = document.getElementById("name").value;
	var p2 = document.getElementById("uemail").value;
	var p3 = document.getElementById("company").value;
	var p4 = document.getElementById("report").value;
	
	if ( ! checkForm(p1,p2,p3,p4)){
		giveMsg(ERROR);
		//chgButton("visible");
		return ;
	}

	var url = "j.bugreport";
	var parameters = "p1=" + escape(p1) + "&p2=" + escape(p2) + "&p3="
			+ escape(p3) + "&p4=" + escape(p4);
	xmlHttp.open("post", url, true);
	xmlHttp.onreadystatechange = updatePage;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.send(parameters);
}

function updatePage() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			// alert("Server is done!");
			var responseData = xmlHttp.responseText;
			if (responseData == "OK") {
				giveMsg("<span style=\"color:green;\">Successful !</span>");
				document.bugreportForm.reset();
			} else {
				giveMsg("Error: submit failed ; check your report and be sure it's without special characters !");
			}

		} else if (xmlHttp.status == 404) {
			giveMsg("Request URL does not exist.");
		} else {
			giveMsg("Error: status code is " + xmlHttp.status);
		}
	}
	
	//window.setTimeout(giveMsg,15000,"&nbsp;");
	//window.setTimeout(chgButton, 1000,"visible");
}

function chgButton(state){
	document.getElementById("submitButton").style.visibility = state;
}

function checkForm(p1,p2,p3,p4){
	if (p1==""){
		ERROR = "Name is required !";
		return false;
	}
	if (p2==""){
		ERROR = "E-mail is required !";
		return false;
	}
	
	if (!checkEmail(p2)){
		ERROR = "<img src='img/icon_sad.gif'alt=''/> Check your Email address ,and input again !";
		return false;
	}
	
	if (p4==""){
		ERROR = "<img src='img/icon_sad.gif'alt=''/> Bug description is required !";
		return false;
	}
	
	if (p4.length > 800){
		ERROR = "<img src='img/icon_sad.gif'alt=''/> Your bug description is too long ( length < 800 is expected)!";
		return false;
	}
	
	if (p4.length < 10){
		ERROR = "So short bug description ?(length > 10 is expected) Or you can send email to us. <img src='img/icon_hap.gif'alt=''/> ";
		return false;
	}
	
	return true;
}


function giveMsg(s){
	document.getElementById("bugError").innerHTML = s;
}

function checkEmail(email){
	return email.isEmail();
}


function countCharNum(){
	var obj = document.getElementById("charNum");
	obj.innerHTML = ""+document.getElementById("report").value.length;
}