﻿//Function trim()
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
//End trim()

//my xmlhttp POST
var http = createRequestObject();
var url = '';
var loading=0;
//Create Oject xmlhttp
function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
	 if (xmlhttp.overrideMimeType) {
            //http_request.overrideMimeType('text/xml');
            xmlhttp.overrideMimeType('text/html');
         }
  }
	return  xmlhttp;
}
function sendRequest(url,param) {
	try{
		document.getElementById("errCode").style.cssText="color:#fff";
		if (loading==0) {
		loading=1;
		show_Loading();
		}
		http.open('POST', url);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send(param);
	}
	catch(e){}
	finally{}
}
//Xu ly reponse
function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){	
		response = http.responseText;
		if(response=='errorCode'){
			document.getElementById("errCode").style.cssText="color:red";
		}
		else if(response=='sended'){
			alert('Mail gởi thành công...');
			document.getElementById("third").reset();
		}
		else{
		alert(response);
		}		
		hide_Loading();
            loading=0;
		//Get a reference to CAPTCHA image
	   img = document.getElementById('imgCaptcha'); 
	   //Change the image
	   img.src = 'create_image.php?' + Math.random();
	   }
	}
	catch(e) {}
	finally{}
   
 }

//Called every time when fo rm is perfomed
function getParam(form) {
url = 'contact/send.php';
 //Set up the parameters of our AJAX call
 var postStr =  "spamcheck="+ encodeURIComponent( document.getElementById("spamcheck").value )  +
				"&name="	+ encodeURIComponent( document.getElementById("name").value )		+
				"&email="	+ encodeURIComponent( document.getElementById("email").value ) 		+
				"&phone="	+ encodeURIComponent( document.getElementById("phone").value )		+
				"&website="	+ encodeURIComponent( document.getElementById("website").value )	+
				"&subject="	+ encodeURIComponent( document.getElementById("subject").value )	+
				"&mailTo="	+ encodeURIComponent( document.getElementById("mailTo").value )		+
				"&receiver="+ encodeURIComponent( document.getElementById("receiver").value )	+
				"&message="	+ encodeURIComponent( document.getElementById("message").value );
 
 //Call the function that initiate the AJAX request
 sendRequest(url,postStr);
}
// + ---------------------- +
// |  BEGIN Loading Alpha   |
// + ---------------------- +
var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer") ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){  ns6=parseInt(navigator.appVersion)>=5;  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;

var timershow=false;
var curx=-200;
var cury=200;
var win_w=window.innerWidth ? window.innerWidth : document.body.offsetWidth;
var mid_w=win_w/2;
var timershow1=window.setInterval("stayMiddle()",1);

function getobj(id) {
el = document.all ? document.all['LoadingDiv'] :   dom ? document.getElementById('LoadingDiv') :   document.layers['LoadingDiv'];
return el;
}

function show_Loading() {
    obj = getobj();
    if (timershow) window.clearTimeout(timershow);
    timershow=window.setInterval("nshow()",1);
}

function hide_Loading() {
    obj = getobj();
    if (timershow) window.clearTimeout(timershow);
    timershow=window.setInterval("nhide()",1);
}

function moveobj(obj,x,y) {
    obj.style.left=x + "px";
    obj.style.top=y+ "px";
    curx=x;
    cury=y;
}

function stayMiddle() {
    if (document.documentElement && document.documentElement.scrollTop)
        var pY =  document.documentElement.scrollTop;
    else if (document.body)
        var pY =  document.body.scrollTop;

    obj = getobj();
    newy = cury+((pY-cury)/16)+12;
    moveobj(obj,curx, newy);
}

function nshow() {
    obj = getobj();
    newx = curx+((mid_w-curx)/16)-7;
    moveobj(obj,newx, cury);
}
function nhide() {
    obj = getobj();
    newx = curx+((0-curx)/16)-15;
    moveobj(obj,newx, cury);
}
// + ---------------------- +
// |    END Loading Alpha   |
// + ---------------------- +  
