// JavaScript Document
function writeSwf(img,wsize,hsize)
{
	//alert(img);
    document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\""+wsize+"\" height=\""+hsize+"\">");
    document.write("<param name=\"movie\" value=\""+img+"\">");
    document.write("<param name=\"quality\" value=\"high\">");
    document.write("<param name=wmode value=transparent>");
	document.write("<param name=\"menu\" value=\"false\">");
    document.write("<embed src=\""+img+"\" wmode=\"transparent\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+wsize+"\" height=\""+hsize+"\" menu=\"false\"></embed>");
	document.write("</object>");
}


//================================================= BEGIN AJAX
function GetXmlHttpObject()
{
	var xmlHttp = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return null;
			}
			
		}
	}
	return xmlHttp;
}



function sendRequestCall(url, processor)
{
	//alert ("in send and request = " + url);
	var xmlHttp;

	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert("AJAX not supported");
		return;
	}
	//alert (processor);
	xmlHttp.onreadystatechange = function()
	{
		//alert("status = " + xmlHttp.status + " - ready = " + xmlHttp.readyState);
		if (xmlHttp.readyState == 4) //lay duoc
		{
			if ( (xmlHttp.status == 200) || (xmlHttp.status == 0) )
				processor (xmlHttp.responseText);
			else
				sendRequestCall(url, processor);
		}
	
		/*else
		{
			sendRequestCall(url, processor);
		}*/
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}


//============================================ajax post
function sendRequestCallPost(url, params, processor)
{
	//alert ("in send and request = " + url);
	var xmlHttp;

	xmlHttp = GetXmlHttpObject();
	xmlHttp.open("POST",url,true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	if (xmlHttp == null)
	{
		alert("AJAX not supported");
		return;
	}
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4) //lay duoc
		{
			processor (xmlHttp.responseText);
		}
	}
	//alert (params);
	
	xmlHttp.send(params);

}


// END AJAX


//==========================================
function loadContent (string)
{
	var text	=	"";
	document.getElementById("content").innerHTML = string;
}


//call ajax
/*function callContent (requestPage, catId, topic, language)
{
	//alert("dien");
	url = requestPage  +  "?catId=" + catId + "&topic=" + topic + "&imagePath=../images/" + topic + "/&language=" + language;
	//alert (url);
	sendRequestCall(url, loadContent);
}*/


//call ajax
function callContent (requestPage, catId, imgPath, language)
{
	//alert("dien");
	url = requestPage  +  "?catId=" + catId + "&imagePath=../images/" + imgPath + "/&language=" + language;
	//alert (url);
	sendRequestCall(url, loadContent);
}


//call ajax
function callSearchResult (requestPage, stringSearch, topic, currPage, language, action)
{
	//alert("dien");
	url = requestPage  +  "?search=" + stringSearch + "&topicSearch=" + topic + "&currPage=" + currPage + "&action=" + action + "&language=" + language;
	//alert (url);
	sendRequestCall(url, loadContent);
}




//call ajax to faq
function callFaq (requestPage, catId, currPage, language, action)
{
	//alert("dien");
	var url = requestPage  +  "?catId=" + catId + "&currPage=" + currPage + "&action=" + action + "&language=" + language;
	//alert (url);
	sendRequestCall(url, loadContent);
}



 


//call ajax to faq
function callFaqAnswer(requestPage, catId, faqId, currPage, language, action)
{
	//alert("dien");
	var url = requestPage  +  "?catId=" + catId + "&faqId=" + faqId + "&currPage=" + currPage + "&action=" + action + "&language=" + language;
	//alert (url);
	sendRequestCall(url, loadContent);
}



function Goto (modules)
	{
		window.myForm = document.createElement ("FORM");
		window.myForm.action = "";
		window.myForm.method = "post";
		window.myForm.style.display = "none";
		var tInput = document.createElement ("INPUT");
	
		//alert(modules);
		tInput.name = "modules";
		tInput.value = modules;
		window.myForm.appendChild (tInput);
		document.body.appendChild (window.myForm);
		
		window.setTimeout ("LetsGo ()", 100);
		
		return false;
	}
	
	
	function LetsGo (modules)
	{
		if (window.myForm)
			window.myForm.submit ();
		else
			window.setTimeout ("LetsGo ()", 100);
	}
	
	
	
	function OpenPhotoWindow(imgSrc)
	{	
	   w = document.body.clientWidth;
	   h = document.body.clientHeight;
		
		var popW = 800;
		var popH = 600;
		
		var leftPos = (w-popW)/2;
		var topPos = (h-popH)/2;
		
		//alert ("left= " + w + " - height = " + h  + " - left = " + leftPos + " top = " + topPos);

		var mywindow = window.open (imgSrc, "mywindow","location=0,status=0,scrollbars=0, width=" + popW + ",height=" + popH);
		mywindow.moveTo(leftPos,topPos);
		mywindow.focus();
	}

/*kiem tra phone*/

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 7;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function ValidateForm(){
	var Phone=document.frmSample.txtPhone;
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number");
		Phone.focus();
		return false;
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number");
		Phone.value="";
		Phone.focus();
		return false;
	}
	return true;
 }

/*check email*/
function checkEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) 
	{
		return false;
	}
	return true;
}
/**/

function preContact (frmName)
{
	var flagSubmit = true;
	frmName = "frmContact";
	
	/*name 	= eval("document.forms['" + frmName + "'].Name.value");
	email 	= eval("document.forms['" + frmName + "'].Email.value");
	phone	= eval("document.forms['" + frmName + "'].Phone.value");
	content = eval("document.forms['" + frmName + "'].Content.value");*/
	
	

	var formElements = eval("document.forms['" + frmName + "'].elements");
	var content	=	formElements["myContent"].value;
	
	name 	= 	formElements["Name"].value;
	email	=	formElements["Email"].value;
	phone	=	formElements["Phone"].value;
	//content	=	formElements["myContent"].value;
	
	if ( (name == "") || (email == "") || (phone == "") || (content == ""))
	{
		flagSubmit = false;
		alert("Xin vui long nhap day du thong tin bat buoc");
	}
	else if (!checkEmail(email))
	{
		flagSubmit = false;
		alert("email k hop le");
	}
	else if( !checkInternationalPhone(phone))
	{
		flagSubmit = false;
		alert("So dien thoai k hop le")		
	} 
	
	
	if (flagSubmit)
	{
		var requestPage = "../send_mail.php";
		callSendmail(frmName, requestPage, name, email, phone, content);
	}
	
}


function callSendmail (frmName, requestPage, name, email, phone, content)
{
	company = address  = "";
	
	if ( eval("document.forms['" + frmName + "'].CompanyName.value") != null)
		company = eval("document.forms['" + frmName + "'].CompanyName.value");
	
		
	if ( eval("document.forms['" + frmName + "'].CompanyAddress.value") != null)
		address =  eval("document.forms['" + frmName + "'].CompanyAddress.value");
	
	var new_content = content.replace(/\n/g,"<br/>");
	
	//url = requestPage  +  "?name=" + name + "&company=" + company + "&address=" + address +  "&email=" + email + "&phone=" + phone + "&content=" + new_content;
	//alert (url);
	var params	=	"name=" + name + "&company=" + company + "&address=" + address +  "&email=" + email + "&phone=" + phone + "&content=" + new_content;
	sendRequestCallPost(requestPage, params, sendMail);
}



function sendMail (string)
{
	var result = false;
	//alert (string);
	try 
	{
		eval(string);
	}
	catch(e)
	{
		alert("Impossible a contacter le serveur");
	}

	if (result)
		alert("Thong tin da goi di, chung toi se tra loi ban trong thoi gian som nhat");
	else
		alert("hien tai chua goi duoc");

}



function preSendFaq (frmName)
{
	var flagSubmit = true;
	//frmName = "frm";
	
	//name 	= eval("document.forms['" + frmName + "'].txtName.value");
	//email 	= eval("document.forms['" + frmName + "'].txtEmail.value");
	//faq		= eval("document.forms['" + frmName + "'].txtFaq.value");
	var formElements = eval("document.forms['" + frmName + "'].elements");
	
	var question =	formElements["myQuestion"].value;
	var catId 	= 	formElements["catId"].value;
	//catId 	=	2;
	var name 	= 	formElements["txtName"].value;
	var email	=	formElements["txtEmail"].value;
	var phone	=	formElements["txtPhone"].value;
	
	
	if ( (name == "") || (email == "")  || (question == "") )
	{
		flagSubmit = false;
		alert("Xin vui long nhap day du thong tin bat buoc");
	}
	else if (!checkEmail(email))
	{
		flagSubmit = false;
		alert("email k hop le");
	}
	else if (phone != "")
	{
		if( !checkInternationalPhone(phone))
		{
			flagSubmit = false;
			alert("So dien thoai k hop le")	
		}
	}
	
	
	if (flagSubmit)
	{
		var requestPage = "../admin/faq_controler.php";
		var language = "vn";
		callSendFaq(requestPage, catId, name, email, phone, question, language);
		//callSendFaq(requestPage, action);
	}
	
}


/*function callSendFaq(requestPage, catId, name, email, phone, question, language)
{
	var url = "";
	var new_question = question.replace(/\n/g,"<br/>");
	//new_question  = url_encode(new_question);
	//alert(question);
	//var s = url_encode(question);
	//alert(s);
	
	url = requestPage + "?catId=" + catId +"&senderName=" + name + "&senderEmail="+ email + "&senderPhone=" + phone + "&question=" + new_question +"&action=add_faq&language=" + language;
	alert(url);
	//document.location.href = url;
	//sendRequestCall(url, sendFaqResult);
}*/

function callSendFaq(requestPage, catId, name, email, phone, question, language)
{
	var params = "";
	var new_question = question.replace(/\n/g,"<br/>");
	//new_question  = url_encode(new_question);
	//alert(question);
	//var s = url_encode(question);
	//alert(s);
	//url = requestPage + "?catId=" + catId +"&senderName=" + name + "&senderEmail="+ email + "&senderPhone=" + phone + "&question=" + new_question +"&action=add_faq&language=" + language;
	params = "catId=" + catId +"&senderName=" + name + "&senderEmail="+ email + "&senderPhone=" + phone + "&question=" + new_question +"&action=add_faq&language=" + language;
	//alert(params);
	//document.location.href = url;
	sendRequestCallPost(requestPage, params, sendFaqResult);
}



function sendFaqResult(string)
{
	var language = "";
	//alert (string);
	try 
	{
		eval(string);
	}
	catch(e)
	{
		alert("Impossible a contacter le serveur");
	}
	
	if (language == "vn")
		alert("Cau hoi cua ban da duoc gui");
	else
		alert("Send question successful!");
}
