var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var loading=0;
var update = '001';
var loadingText = "<img src='images/loading.gif' align=absmiddle hspace=5><font color=red>Đang tải dữ liệu ...</font>";

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();
	}
	return  xmlhttp;
}

function sendRequest(current_url, act) {
	try{
		var sBasePath = window.location.pathname.substring(0,document.location.href.lastIndexOf('.php'));	// www.dongnghispa.vn
//		var sBasePath = window.location.pathname.substring(13,document.location.href.lastIndexOf('.php'));	// /DongNghiSpa/
		site	= sBasePath;
		switch(act)
		{
			case 'List_My_Pham':
			case 'My_Pham':
				field	= document.getElementById('main');
				break;
			case 'View_Help':
				self.scrollTo(0, 0);
				field	= document.getElementById('View_Help');
				field.style.display = "";
				break;
			default:
				self.scrollTo(0, 0);
				field	= document.getElementById('main');
				break;
		}
		current_url = encodeURIComponent(current_url);
		if (loading == 0) 
		{
			loading = 1;
			show_Loading();
		}
		
		http.open('POST', site);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		http.onreadystatechange = handleResponse;
		http.send('url=' + current_url);
	}
	catch(e){}
	finally{}
}

function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			hide_Loading();
			loading = 0;
			response = http.responseText;

			field.innerHTML = response;
			field.style.display = "";
		}
  	}
	catch(e){}
	finally{}
}

function sendRequest_Service( current_url, serviceID )
{
	try{
		current_url = encodeURIComponent(current_url);
		if (loading == 0) 
		{
			loading = 1;
			show_Loading();
		}
		http.open('POST', 'actions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		http.onreadystatechange = handleResponse;
		http.send('url=' + current_url +','+ serviceID);
	}
	catch(e){}
	finally{}
}

function sendRequest_Product( current_url, productID )
{
	self.scrollTo(0, 0);
	field = document.getElementById('My_Pham');
	try{
		current_url = encodeURIComponent(current_url);
		if (loading == 0) 
		{
			loading = 1;
			show_Loading();
		}
		http.open('POST', current_url +'.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		http.onreadystatechange = Product_handleResponse;
		http.send('url=' + current_url +','+ productID);
	}
	catch(e){}
	finally{}
}

function Product_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			hide_Loading();
			loading = 0;
			field.innerHTML = http.responseText;
			field.style.display = "";
		}
  	}
	catch(e){}
	finally{}
}

function request_Help()
{
	try{
		document.getElementById('Help').innerHTML = '<img src="images/spacer.gif" width=46 height=1>'+ loadingText;
		http.open('POST', 'actions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		http.onreadystatechange = Help_handleResponse;
		http.send('url=HELP');
	}
	catch(e){}
	finally{}
}

function Help_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200))
			document.getElementById('Help').innerHTML = http.responseText;
  	}
	catch(e){}
	finally{}
}

function reload_ConfirmCode() {
	field = 'Confirmation_Code';
	try{
		document.getElementById(field).innerHTML = loadingText;
		http.open('POST', 'actions.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = ConfirmCode_handleResponse;
		http.send('url='+ field);
	}
	catch(e){}
	finally{}
}

function ConfirmCode_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200))
			document.getElementById(field).innerHTML = http.responseText;
  	}
	catch(e){}
	finally{}
}

// bOOm Loading
/* // code in html
<div id=bOOm_Loading style="display: none; position:absolute; top:0px; right:0px; padding: 5px; vertical-align:middle; text-align:center; background-color:#CC0033; width:140px; color:#FFFFFF; font-weight:bold; font-size:10px; font-family: Arial"><em>Đang tải trang</em></div>
<a href="javascript:void(0)" onclick="show_bOOm()">show</a> | <a href="javascript:void(0)" onclick="hide_bOOm()">hide</a>
*/
function show_bOOm()
{
	document.getElementById('bOOm_Loading').style.display = 'inline';
}

function hide_bOOm()
{
//	document.getElementById('bOOm_Loading').style.visibility = 'hidden';
	document.getElementById('bOOm_Loading').style.display = 'none';
}


// NDK Loading
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	= -220;
var cury	= 350;
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[id] :   dom ? document.getElementById(id) :   document.layers[id];
return el;
}

function show_Loading() {
	obj = getobj('LoadingDiv');
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nshow()",1);
}

function hide_Loading() {
	obj = getobj('LoadingDiv');
	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('LoadingDiv');
	newy = cury+((pY-cury)/16)+12;
	moveobj(obj,curx,newy);
}

function nshow() {
	obj = getobj('LoadingDiv');
	newx = curx+((mid_w-curx)/16)-7;
	moveobj(obj,newx,cury);
}
function nhide() {
	obj = getobj('LoadingDiv');
	newx = curx+((0-curx)/16)-15;
	moveobj(obj,newx,cury);
}
// End

function getVar(url, cnt)
{
	url = url + '#';
	url = url.split('#');
//	if (!url[1]) window.location.href = '#Home';
	url = url[1];
	url = url + ',';
	url = url.split(',');
	if (url[0] == 'Home') window.location.href = './';
	if (cnt != -1) {
		url = url[cnt];
		if (!url) return '';
	}
	return url;
}

function loadPage() {
	act = getVar(window.location.href, 0);
	if (act) {
		current_url = window.location.href;
		current_url = current_url+'#';
		current_url = current_url.split('#');
		current_url = current_url[1];
		if (current_url) sendRequest(current_url, act);
	}
}

function urlCheck()
{
	url = window.location.href;
	if (url != '' && url != lastUrl)
	{
		loadPage();
		lastUrl = url;
	}
}

function startLoad() 
{
	interval = setInterval('urlCheck()',100);
}

/*------------------------------------------------------*/

function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function popup(url,wdname,width,height)
{
	if (width == null)  { width  = 600; }   // default width
	if (height == null) { height = 500; }   // default height
	newwin = window.open(url, wdname, 'fullscreen=0, toolbar=0, location=0, channelmode=0, dependent=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width='+width+', height='+height);
	if (document.all)
	{
		newwin.moveTo(0,0);
		newwin.focus();
	}
}

function Shockwave_Flash( strSource, fwidth, fheight )
{
	var width=fwidth;
	var height=fheight;
    var s;
	s='<object width="'+ width +'" height="'+ height +'" codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000>';
		s+='<param name="movie" value="Avertise">';
		s+='<PARAM NAME="Src" VALUE="'+ strSource +'">';
		s+='<PARAM NAME="WMode" VALUE="Window">';
		s+='<PARAM NAME="Quality" VALUE="High">';
		s+='<PARAM NAME="AllowScriptAccess" VALUE="always">';
		s+='<PARAM NAME="Scale" VALUE="ShowAll">';
		s+='<embed src="'+ strSource +'" width="'+ width +'" height="'+ height +'" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"></embed>';
	s+='</object>';

	document.write(s);
}

var submitcount = 0;
//Function to check form is filled in correctly before submitting
function CheckForm ()
{
	var errorMsg = "";
	if (document.formsend.full_name.value == "")
		errorMsg += "\n-- Xin hãy cho biết họ tên đầy đủ của bạn !";
	if (document.formsend.telephone.value == "")
		errorMsg += "\n-- Xin hãy cho biết số điện thoại cá nhân của bạn !";
	if (document.formsend.email.value == "")
		errorMsg += "\n-- Xin hãy cho biết địa chỉ email của bạn !";
	else if ( !isEmail(document.formsend.email.value) )
		errorMsg += "\n-- Địa chỉ email không hợp lệ, xin hãy nhập lại !";
	if (document.formsend.message.value.length <= 10)
		errorMsg += "\n-- Nội dung thông điệp quá ngắn, xin hãy nhập vào nội dung !";

	//If there is aproblem with the form then display an error
	if (errorMsg != ""){
		msg = "______________________________________________________________\n\n";
		msg += "Error.\n";
		msg += "______________________________________________________________\n";
		
		errorMsg += alert(msg + errorMsg + "\n");
		return false;
	}
	
	if (submitcount == 0)
	{
		submitcount++;
		formsend.Submit.disabled = true;
		return true;
	}
	/*else
	{
		alert("Waiting...............................................!");
		return false;
	}*/
	return false;
}
function CheckForm_En ()
{
	var errorMsg = "";
	if (document.formsend.full_name.value == "")
		errorMsg += "\n-- Full name : Enter your name please !";
	if (document.formsend.telephone.value == "")
		errorMsg += "\n-- Telephone : Enter your telephone number please !";
	if (document.formsend.email.value == "")
		errorMsg += "\n-- E-mail : Enter your email address please !";
	else if ( !isEmail(document.formsend.email.value) )
		errorMsg += "\n-- E-mail : Your email invalid, please try again !";
	if (document.formsend.message.value.length <= 10)
		errorMsg += "\n-- Content : Your message is too short, please enter !";

	//If there is aproblem with the form then display an error
	if (errorMsg != ""){
		msg = "______________________________________________________________\n\n";
		msg += "Error.\n";
		msg += "______________________________________________________________\n";
		
		errorMsg += alert(msg + errorMsg + "\n");
		return false;
	}
	
	if (submitcount == 0)
	{
		submitcount++;
		formsend.Submit.disabled = true;
		return true;
	}
	/*else
	{
		alert("Waiting...............................................!");
		return false;
	}*/
	return false;
}
function isEmail(s)
{
	if (s=="") return false;
	if(s.indexOf(" ")>0) return false;
	var i = 1;
	var sLength = s.length;
	if (s.indexOf(".")==sLength) return false;
	if (s.indexOf(".")<=0) return false;
	if (s.indexOf("@")!=s.lastIndexOf("@")) return false;
	while ((i < sLength) && (s.charAt(i) != "@"))  { i++  }
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;
	while ((i < sLength) && (s.charAt(i) != "."))  { i++  }
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	var str="1234567890abcdefghikjlmnopqrstuvwxyzABCDEFGHIKJLMNOPQRSTUVWXYZ-@._";
	for(var j=0;j<s.length;j++)
		if(str.indexOf(s.charAt(j))==-1)
			return false;
	return true;
}
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		alert("Nội dung thông điệp không vượt quá " + maxlimit + " ký tự. Xin vui lòng tóm tắt lại");
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}
function hideHelp()
{
	//document.getElementById('Help').style.display='none';
	document.getElementById('Help').innerHTML = '<a href="javascript:void(0)" onclick="request_Help()" class="mainmenu"><img src="images/mail_help.gif" width="40" height="40" border="0" hspace="5" align="absmiddle" />CLICK VÀO ĐÂY ĐỂ GỞI CÂU HỎI CỦA BẠN</a>';
}
function jemail(user, domain, suffix){
	document.write('<a href="' + 'mailto:' + user + '@' + domain + '.' + suffix + '" title="bOOm bOOm" class=copyright>' + user + '@' + domain + '.' + suffix + '</a>');
}
