function vote(f){
	var v = '';
	for(i=0;i < f.elements.length;++i)
		if(f.elements[i].type == 'checkbox' || f.elements[i].type == 'radio'){
			if(f.elements[i].checked)
				v += '&' + f.elements[i].name + '=' + encodeURI(f.elements[i].value);
		}else{
			v += '&' + f.elements[i].name + '=' + encodeURI(f.elements[i].value);
		}
	v = v.substr(1);//exclude leading &
	var sParams = "status=no,scrollbars=yes,toolbar=no,menubar=no,resizable=yes,location=no,height=300,width=500,top=100,left=200";
	var wname = "pollwindow";
	var dest = f.action+'?'+v;
	window.open(dest,wname,sParams);
	return false;
}
function getChecked(name){
/*	getChecked(name): return a comma seperated value (value1,value2,value3...)for all checkbox with same name
name	: name of the checkbox
need to create array of check box with same name like:
<input type=checkbox name='name' value=value1>
<input type=checkbox name='name' value=value2>
<input type=checkbox name='name' value=value3>
*/
	var o=document.getElementsByName(name);
	var i,s,v;
	for(i=0,s='';i < o.length; ++i){
		v=o.item(i);
		if(v.type == "checkbox"){
			if(v.checked) s += ',' + String(v.value);
		}
	}
	return s.substr(1);//remove leading ,
}
//////////////////////
function getIdValuesPair(name){
/*	getIdValuesPair(name): return a comma seperated (id1=value1,id2=value2...) for all input with same name
name	: name of input
need to create array of input with same name like:
<input type=text name='name' id='id1' value=value1>
<input type=text name='name' id='id2' value=value2>
<input type=text name='name' id='id3' value=value3>
*/
	var o=document.getElementsByName(name);
	var s='';
	for(i=0;i<o.length;++i){
		v=String(o.item(i).value);
		if(v != '') s += ',' + o.item(i).id + '=' + v;
	}
	return s.substr(1);
}
//////////////////////
function checkAll(name){
/*	checkAll(name):	mark as checked all checkbox with same name
name	: name of the checkbox
*/
	var o=document.getElementsByName(name);
	var i;
	for(i=0;i < o.length; o.item(i).checked=true, ++i);
}
////////////////////////
function setAll(name,state){
/*	setAll(name,state):	mark all checkbox with same name to the specified state
name	: name of the checkbox
state	: true/false status
*/
	var o=document.getElementsByName(name);
	var i;
	for(i=0;i < o.length; o.item(i).checked=state, ++i);
}

////////////////////////
function clearAll(name){
/*	clearAll(name):	uncheck all checkbox with same name
name	: name of the checkbox
*/
	var o=document.getElementsByName(name);
	var i;
	for(i=0;i < o.length; o.item(i).checked=false, ++i);
}
/////////////
////////////////////////
function resetAllName(name){
/*	resetAllName(name):	set name to empty string to exclude from submitting
name	: name of the checkbox
*/
	var o=document.getElementsByName(name);
	var i;
	for(i=0;i < o.length; o.item(i).name='', ++i);
}
/////////////
function urlgo(page,q){
/*	urlgo(page,q,[<name1,value1>[,..n]][,'-',<namex>[,...n]])
	tell browser to goto specified location
page:	page name to goto
q	:	query string
<name1,value1>:	append parameter name=value to query string q
namex	: chop this parameter from query string q
*/
	var c=arguments.length;
	var re=new RegExp;
	for(i = 2; (argv=String(arguments[i])) != '-' && i < c; ++i){
		re.compile("(?:\\&|^)" + argv + "=[^\\&]*(?=\\&|$)","ig");
		q=q.replace(re,"&");//append to url
		q += "&" + argv + "=" + String(arguments[++i]);
	}
	for(++i; i < c; ++i){ //ignore -, denote a start of chopping parameters)
		argv=String(arguments[i]);
		re.compile("(?:\\&|^)" + argv + "=[^\\&]*(?=\\&|$)","ig");
		q=q.replace(re,"&"); //chop from url
	}
	q=q.replace(/\&{2,}/ig,"&").replace(/(?:^\&|\&$)/ig,"");
	window.location.href=page + "?" + q;
}
///////////////
function urlchop(q){
/*	urlchop(q,[name][,...n])
	chop name from query string q
q	: original query string
name: chop this parameter from q
*/
	var c=arguments.length;
	var re=new RegExp;
	for(i=1; i < c; ++i){ //ignore -, denote a start of chopping parameters)
		argv=String(arguments[i]);
		re.compile("(?:\\&|^)" + argv + "=[^\\&]*(?=\\&|$)","ig");
		q=q.replace(re,"&"); //chop from url
	}
	return q.replace(/\&{2,}/ig,"&").replace(/(?:^\&|\&$)/ig,"");
}
////////////////
function urlformat(q){
/*	urlgo(q,[<name1,value1>[,..n]][,'-',<namex>[,...n]])
	append or chop specified parameters from query string
q				: original query string
<name1,value1>	: append parameter name1=value1 to query string q
namex			: chop this parameter from q
*/
	var c=arguments.length;
	var re=new RegExp;
	for(i = 1; (argv=String(arguments[i])) != '-' && i < c; ++i){
		re.compile("(?:\\&|^)" + argv + "=[^\\&]*(?=\\&|$)","ig");
		q=q.replace(re,"&");//append to url
		q += "&" + argv + "=" + String(arguments[++i]);
	}
	for(++i; i < c; ++i){ //ignore -, denote a start of chopping parameters)
		argv=String(arguments[i]);
		re.compile("(?:\\&|^)" + argv + "=[^\\&]*(?=\\&|$)","ig");
		q=q.replace(re,"&"); //chop from url
	}
	return q.replace(/\&{2,}/ig,"&").replace(/(?:^\&|\&$)/ig,"");
}
/*created by DungHM*/
function hideButton(){
//An button tren thanh cong cu voi doi so la id cua td chua nut	
	argLen = arguments.length;
	for(i=0;i<argLen;i++){
		arg = String(arguments[i]);
		with(parent.banner){
			document.getElementById(arg).style.display="none";
		}
	}
}
function showButton(){	
//Hien button tren thanh cong cu voi doi so la id cua td chua nut	
	argLen = arguments.length;
	for(i=0;i<argLen;i++){
		arg = String(arguments[i]);
		with(parent.banner){
			document.getElementById(arg).style.display="";
		}
	}
}
function oversort(id){
	document.getElementById(id).style.backgroundImage="url(../images/over.gif)";	
}
function outsort(id){
	document.getElementById(id).style.backgroundImage="url(../images/bg-product.gif)";	
}
function contextmenu(id){	
	document.getElementById(id).style.visibility = "visible";
}
function contextout(id){
	document.getElementById(id).style.visibility = "hidden";
}
function checknumber(val){
	if (isNaN(val)){
		alert("Day khong phai la mot so!");
		return false;
	}
	if (val == null || val ==""){
		alert("Ban khong duoc bo trong truong nay!");
		return false;
	}
	return true;
}
var __tested__ = false;
function storeCaret (textEl) {
	__tested__=true;
	if (textEl.createTextRange) {
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}
function insertAtCaret (textEl, text) {
	if(!__tested__) return;
	if (textEl.createTextRange && textEl.caretPos) {
		var caretPos = textEl.caretPos;
		caretPos.text =
		caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?	text + ' ' : text;
		textEl.focus();
	}
	else textEl.value = text;
}
function EnterToBr(){
	if(window.event.keyCode == 13 && ! window.event.shiftKey){
		insertAtCaret(window.event.srcElement,"<br>");
	}
}

//Gui cho nguoi than
function showpopup(url){
	if (url=="") return;
	var content = "<form id='sendform' action='sendto.php' method='post' onSubmit='return checkform(this);'>";
	content += "<table width='100%' style='font-size:11px' bgcolor='#EFEBDE'><tr><td colspan='2' class='boxTitle'><input type='hidden' name='url' value='"+url+"'>G&#7917;i cho b&#7841;n b&#232;</td></tr>";
	content += "<tr><td class='text'>Ng&#432;&#7901;i g&#7917;i:&nbsp;</td><td><input name='sendername' class='inputbox' type='text' style='font-size:11px;width:250px;' value=''></td></tr>";
	content += "<tr><td class='text'>Email ng&#432;&#7901;i g&#7917;i:&nbsp;</td><td><input class='inputbox' name='senderemail' type='text' style='font-size:11px;width:250px;'  value=''></td></tr>";
	content += "<tr><td class='text'>Ng&#432;&#7901;i nh&#7853;n:&nbsp;</td><td><input type='text' class='inputbox' name='receivername' style='font-size:11px;width:250px;'  value=''></td></tr>";
	content += "<tr><td class='text'>Email ng&#432;&#7901;i g&#7917;i:&nbsp;</td><td><input type='text' class='inputbox' name='receiveremail' style='font-size:11px;width:250px;'  value=''></td></tr>";
	content += "<tr><td class='text'>Ghi ch&#250;:&nbsp;</td><td><textarea style='overflow:auto' class='inputbox' name='sendernote' style='font-size:11px;width:250px;height:100px'></textarea></td></tr>";
	content += "<tr><td class='text' colspan='2' style='text-align:center'><input type='submit' value='G&#7917;i &#273;i' style='font-family:tahoma;font-size:11px;border:1px solid #000000'>&nbsp;<input type='button' value='B&#7887; qua' style='font-family:tahoma;font-size:11px;border:1px solid #000000' onClick=\"javascript:closebox('sendtofriend');\"></td></tr></table>";
	content += "</form>";
	if ($('sendtofriend')) document.body.removeChild($('sendtofriend'));
	oDiv = document.createElement('DIV');	
	oDiv.id="sendtofriend";
	oDiv.style.fontSize="11px";
	oDiv.style.border="1pt solid #0A2C6F";
	oDiv.style.padding="0px";
	oDiv.style.position="absolute";
	oDiv.style.textAlign="justify";
	oDiv.style.width="380px";
	oDiv.style.height="150px";
	oDiv.style.top="200px";
	oDiv.style.left="320px";
	oDiv.style.backgroundColor="#FFFFFF";
    oDiv.innerHTML = content;
	oSelect = document.getElementsByTagName("select");
	for(var i=0;i<oSelect.length;i++){
		oSelect[i].style.visibility="hidden";	
	}
	document.body.appendChild(oDiv);	
}

function showpicture(imgsrc){
	if (imgsrc=="") return;
	if ($('picturebox')) document.body.removeChild($('picturebox'));
	oDiv = document.createElement('DIV');
	oDiv.id = "picturebox";
	oDiv.innerHTML = "<img src='"+imgsrc+"' style='cursor:pointer;' title='Click chu&#7897;t l&#234;n &#7843;nh &#273;&#7875; &#273;&#243;ng' onClick=\"javascript:closebox('picturebox');\">";
	oDiv.style.border="1px solid #CCCCCC";
	oDiv.style.padding="0px";
	oDiv.style.position="absolute";
	oDiv.style.width="10px";
	oDiv.style.height="10px";
	oDiv.style.top="200px";
	oDiv.style.left="400px";
	oDiv.style.backgroundColor="#FFFFFF";	
	document.body.appendChild(oDiv);
	oDiv.style.visibility ="hidden";
	fade(oDiv,"visible");
}

function closebox(id){
	oDiv = $(id);
	document.body.removeChild(oDiv);	
	oSelect = document.getElementsByTagName("select");
	for(var i=0;i<oSelect.length;i++){
		oSelect[i].style.visibility="visible";	
	}
}

function fade(obj,type) {
	try{
    obj.style.filter="blendTrans(duration=2)";
    // Make sure the filter is not playing.
    if (obj.filters.blendTrans.status != 2) {
        obj.filters.blendTrans.apply();
        obj.style.visibility=type;
        obj.filters.blendTrans.play();
    }
	}catch(e){};
}

function checkform(oForm){
	var regex = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
	var sender=oForm.senderemail.value;
	var receiver=oForm.receiveremail.value;
	for (var i=0;i<oForm.elements.length-2;i++){
		if (oForm.elements[i].value==""){
			alert("Ban khong duoc de trong truong nay");
			return false;
		}
	}
	if (!regex.test(sender) || !regex.test(receiver)){
		alert("Email khong dung dinh dang");
		return false
	}
	return true;
}	
function loadingbox(){
	this.draw = function (content,width,height){
		if (document.getElementById("loading")){
			document.body.removeChild(document.getElementById("loading"));
		}
		var div = document.createElement("DIV");
		div.setAttribute("id","loading");
		div.innerHTML = content;
		div.style.position = "absolute";
		div.style.left = "400px";
		div.style.bottom = "300px";
		div.style.width = width+"px";
		div.style.height = height+"px";
		div.style.textAlign = "center";
		div.style.verticalAlign = "middle";
		div.style.border = "2px solid #333333";
		div.style.fontColor = "#FFFF00";
		div.style.padding = "2px 0px 0px 0px";
		div.style.zIndex = 1;
		div.style.backgroundColor = "#FFFFCC";
		//div.onclick = function (){
			//document.body.removeChild(div);
		//}
		document.body.appendChild(div);
	}
	this.messagebox = function (content,width,height){
		if (document.getElementById("messagebox")){
			document.body.removeChild(document.getElementById("messagebox"));
		}
		var div = document.createElement("DIV");
		content = content + "<input type = 'button' value = '&nbsp;&nbsp;&nbsp;&#272;&oacute;ng&nbsp;&nbsp;&nbsp;' onClick = \"document.body.removeChild($('messagebox'));\" style='border:1px solid #CCCCCC;'><br/><a href='index.php' style='color:blue;text-decoration:none;'>http://www.esnc.net</a><br/>";
		div.setAttribute("id","messagebox");
		div.innerHTML = content;
		div.style.position = "absolute";
		div.style.left = "310px";
		div.style.bottom = "100px";
		div.style.width = width+"px";
		div.style.height = height+"px";
		div.style.textAlign = "center";
		div.style.verticalAlign = "middle";
		div.style.border = "2px solid #333333";
		div.style.fontColor = "#FFFF00";
		div.style.padding = "2px 0px 0px 0px";
		div.style.zIndex = 1;
		div.style.backgroundColor = "#FFFFCC";
		//div.onclick = function (){
			//document.body.removeChild(div);
		//}
		document.body.appendChild(div);
	}
	this.clear = function (){
		var div = document.getElementById("loading");
		if (div){
			document.body.removeChild(div);
		}else{
			return;
		}
		
	}
}
function mediaPreview(url){
	window.open("trial.php?url="+url,null,"height=200,width=200,status=no,toolbar=no,menubar=no,location=no");
}

function setcookie(cookieName,cookieValue){	
	document.cookie = cookieName+"=" + cookieValue;
}
function getcookie(cookieName){
	cookies = String(document.cookie) + ";";
	var pos = cookies.indexOf(cookieName + "=");
	if (pos!=-1){
		var start = pos + (cookieName + "=").length;
		var end = cookies.indexOf(";",start);
		var value = cookies.substring(start, end); 
		return value;
	}
	else return 0;
}
function swapmenu(cookieName){
	try{
		var id = getcookie(cookieName);
		var o = document.getElementById(id);
		o.style.backgroundImage = "url(images/bg_menu1.gif)";
	}catch(e){}
}
function showdemo(id){
	if ($(id).value=="NULL"){
		alert("Xin moi chon mot mau thiet ke!");
		return;
	}else{
		window.open($(id).value, "subWindow");
	}
}
var hWin;
function windowOpen(){
	var sParams = "toolbar=no,resizable=yes,menubar=no,location=no,height=320,width=470,top=200,left=50";
	var support;
	//if(hWin && (!hWin.closed))hWin.focus();
	//else{
	window.open("http://support.esnc.net/logonsupport.asp?qrid=1",support,sParams);
}
/*---end---*/
function checkEmail(form,input){
	var regex = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
	if (!regex.test(input)){
		alert("Email khong dung dinh dang");
		return;
	}
	submitTextForm(form,'text','cust-process.php?act=resetpass');
	return true;
}
function utf8ToISO(o){
	var s,ss,i,n;
	s=String(o.value);
	ss="";
	n=s.length;
	for(i=0;i<n;++i){
		if((code=Number(s.charCodeAt(i))) > 128){
			ss += "&#" + code + ";";
		}else ss += s.charAt(i);
	}
	o.value=ss;
//	window.alert(o.value);
}

function getObject(id){
	return document.getElementById(id);
}

function isEmpty(obj){
	var value = obj.value;
	if (value=="" || value == null){
		alert("Not leave blank");
		obj.focus();
		return false;
	}
	return true;
}

function isEmail(obj){
	var str = obj.value;
	var emailPattern = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
	if (isEmpty(obj)){
		if (!emailPattern.test(str)){
			alert("Email incorrected format");
			obj.focus();
			return false;
		}
	}	
	return true;
}
function isSelected(obj){
	var value = obj.value;
	if (value == "" || value == null){
		alert("Pls, choose a value");
		obj.focus();
		return false
	}
	return true;
}
function isZipcode(obj){
	var value = obj.value;
	if (isNaN(value)){
		alert("Not a number");
		obj.focus();
		return false
	}
	if (value.length <5){
		alert("Zip code not le");
		obj.focus();
		return false
	}
	return true;
}
function isDomanin(){

}
function isPhone(){

}
function isMatch(){

}
function getSelectText(oSelect,oHidden){
	var oT = oSelect.options[oSelect.selectedIndex].text;
	oHidden.value = oT;
}
