 function isempty(str) 
{
	atmp = str.split(String.fromCharCode(32));
	for (ii=0; ii< atmp.length;ii++ ) 
	{
		if (atmp[ii] != "") 
		return false;
	}	
	return true;
}
//---------------------------------------------
//Submit form by Javascript
//---------------------------------------------
function selfsubmit(straction) {
	window.document.forms[0].target = "_self";
	window.document.forms[0].action = straction;
	window.document.forms[0].submit();
}
//---------------------------------------------
//Check value for CheckBox
//---------------------------------------------
function setchecked(val,ctrChkName)
{
 	document.forms[0].chkall.checked = val
  with (document.forms[0])
   { 
 
	 len = elements.length;
     for(var ii=0; ii<len; ii++) 
	 {
		if (elements[ii].name == ctrChkName) 
		{
			elements[ii].checked = val;
		}
		
	 }

  }
}
//---------------------------------------------
//Check value for CheckBox
//---------------------------------------------
function chkremove(ctrChkName) {
  fg = false;
  with (document.forms[0]) {
	 len = elements.length;
     for(var ii=0; ii<len; ii++) {
		if ((elements[ii].name == ctrChkName) && (elements[ii].checked)) {
			fg = true;
			break;
		}
	}
  }
  return(fg)
}
//--------------------------------------------------
// EmptyTextbox: Kiem tra textbox co rong hay khong
//--------------------------------------------------
function EmptyTextbox(FormName,TextboxName)
{
	x=document + "." + FormName + "." + TextboxName;
	if (x.value.length==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//---------------------------------------------------
// FocusField(fieldname)
//---------------------------------------------------
function FocusField(FieldName)
{
	window.document.form[0].FieldName.focus();
	//x.focus();
}

//---------------------------------------------------
// eMailCheck(str): Kiem tra dia chi email
//---------------------------------------------------
function eMailCheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		  
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
	}

//---------------------------------------------------
// IsNumeric: Kiem tra du lieu co phai So
//---------------------------------------------------
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
//----------------------------------------------------
// GetDateTime: Lay ngay gio he thong:
// strCountry: VIE or ENG
//----------------------------------------------------

function GetDateTime(strCountry)
{
	var d=new Date()
	var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
	
	if(strCountry.toUpperCase() == "ENG")
		var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	if(strCountry.toUpperCase() == "VIE")
		var weekday=new Array("Chủ nhật","Thứ Hai","Thứ Ba","Thứ Tư","Thứ Năm","Thứ Sáu","Thứ Bảy")

	document.write(weekday[d.getDay()] + ", ng&#224;y ")
	document.write(d.getDate() + " th&#225;ng ")
	//document.write(monthname[d.getMonth()] + " ")
	document.write(d.getMonth() + 1 + " n&#259;m ")
	document.write(d.getFullYear())
	
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
/*ham kiem tra url hop le*/
function isValidURL(e)
{	var check = true
    //var re =/^www+\.[a-zA-Z0-9\-]+\.[a-zA-Z0-9-\.]+$/
	var re1 =/^http:+\/+\/+[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]/
	var re2 =/^http:+\/+\/www+\.[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]/
  //  if(re.test(e)==false)
		if(re1.test(e)==false)
			 if(re1.test(e)==false)
			 	check = false
	return check;
}

function check_anthorizenet() {
  var error = 0;
  var error_message = "Errors have occured.\n\n Please make the following corrections:\n\n";

    var cc_owner = document.frmpayment.txtcardowner.value;
   var cc_number = document.frmpayment.txtcard_number.value;
    
   if (cc_owner == "" || cc_owner.length < 3) {
      error_message = error_message + "* The owner's name of the credit card must be at least 3 characters.\n";
	alert(error_message);
    document.frmpayment.txtcardowner.focus();
	return false;
	  
    }
    if ( cc_number == "" || cc_number.length < 10 || !IsNumeric(cc_number)) {
      alert(error_message + "* The credit card number must be at least 10 characters.\n * OR must be numeric");
    document.frmpayment.txtcard_number.focus();
	return false;
    }
}

function check_paypal() {
	document.frmpayment.action="checkout_success.php?refer=payal&shippingto="+document.frmpayment.shipping_to.value+"&invoice="+document.frmpayment.invoice.value;
	document.frmpayment.submit();
}
function check_cc() {
  var error = 0;
  var error_message = "Errors have occured.\n\n Please make the following corrections:\n\n";

  var cc_owner = document.frmpayment.txtfullname.value;
  var cc_number = document.frmpayment.txtcard_number.value;
    
   if (cc_owner == "" || cc_owner.length < 3) {
      error_message = error_message + "* The owner's name of the credit card must be at least 3 characters.\n";
	alert(error_message);
    document.frmpayment.txtfullname.focus();
	return false;
	  
    }
    if ( cc_number == "" || cc_number.length < 10 || !IsNumeric(cc_number)) {
      alert(error_message + "* The credit card number must be at least 10 characters.\n * OR must be numeric");
    document.frmpayment.txtcard_number.focus();
	return false;
    }
//document.frmpayment.action="checkout_success.php?refer=cc&shippingto="+document.frmpayment.shipping_to.value+"&invoice="+document.frmpayment.invoice.value;
//document.frmpayment.valid.value="cc";
return true;
//document.frmpayment.submit();
}


function check_checkout()
{
		str = toRealCharCode(" Th&#244;ng b&#225;o l&#7895;i")+" \n ";
		str1 = str + toRealCharCode("Vui l&#242;ng nh&#7853;p H&#7885; & T&#234;n");
		str2 = str + toRealCharCode("&#272;&#7883;a ch&#7881; Email kh&#244;g &#273;&#250;ng");
		str3 = str + toRealCharCode("Vui l&#242;ng nh&#7853;p s&#7889; &#273;i&#7879;n tho&#7841;i HO&#7862;C s&#7889; &#273;i&#234;n tho&#7841;i kh&#244;ng &#273;&#250;ng");
		str4 = str + toRealCharCode("Vui l&#242;ng nh&#7853;p T&#7881;nh/Th&#224;nh Ph&#7889;");
		str5 = str + toRealCharCode("Vui l&#242;ng ch&#7885;n Qu&#7889;c gia");
		str6 = str + toRealCharCode("Vui l&#242;ng  ch&#7885;n Bang/T&#7881;nh/Th&#224;nh ph&#7889;");
		str7 = str + toRealCharCode("B&#7841;n ph&#7843;i &#272;&#7891;ng &#253; c&#225;c &#273;i&#7873;u kho&#7843;n s&#7917; d&#7909;ng c&#7911;a ch&#250;ng t&#244;i b&#7857;ng c&#225;ch checked v&#7843;o &#244; &#273;&#7891;ng &#253; &#273;i&#7873;u kho&#7843;n s&#7917; d&#7909;ng");
		str8 =str + toRealCharCode("Vui l&#242;ng nh&#7853;p &#273;&#7883;a ch&#7881;");
	if(document.frmcheckout.txtfullname.value==""){
		alert(str1);
		document.frmcheckout.txtfullname.focus();
		return false;
	}
	if(!eMailCheck(document.frmcheckout.txtemail.value))
	{
		alert(str2);
		document.frmcheckout.txtemail.focus();
		return false;
	}
	if(document.frmcheckout.txtaddress.value==""){
		alert(str8);
		document.frmcheckout.txtaddress.focus();
		return false;
	} 
	if(document.frmcheckout.txtphone.value=="" || !IsNumeric(document.frmcheckout.txtphone.value)){
		alert(str3);
		document.frmcheckout.txtphone.focus();
		return false;
	} 

	if(document.frmcheckout.txtprovince.value==""){
		alert(str4);
		document.frmcheckout.txtprovince.focus();
		return false;
	} 
	if(document.frmcheckout.txtcountry.value==""){
		alert(str5);
		document.frmcheckout.txtcountry.focus();
		return false;
	}
	if(document.frmcheckout.txtstate.value==""){
		alert(str6);
		document.frmcheckout.txtstate.focus();
		return false;
	}
	if(!document.frmcheckout.agreement.checked){
		alert(str7);
		document.frmcheckout.agreement.focus();
		return false;
	}	
	document.frmcheckout.act.value="order";
	document.frmcheckout.action ="payment.php";
	document.frmcheckout.submit();
return true;
}


function check(style)
{
	if(style =="tracking")
	 {
		if(document.frmtracking.txttracking.value=="")
		 { alert("Error. \n You must input your invoice for tracking status.");
		 document.frmtracking.txttracking.focus();
		  return false
		 }
		 document.frmtracking.action="?cfm=_invoice&_actid=invoice_3&invoice=&invoice="+document.frmtracking.txttracking.value;
	 }

if(style ==12)
	 {
		if(document.frm.txtkeyseach.value=="")
		 { alert("You must type keyword");
		 document.frm.txtkeyseach.focus();
		  return false
		 }
		 else
		 { 
		  document.frm.action="result_search.php?server=&key="+document.frm.txtkeyseach.value+"&category=";	 
	 	  document.frm.submit()
		 }
	 }

		if(style ==13)
		 {
			//alert("hay qua"+ document.frm.btsearch.value);
			if(document.frm.txtemail.value!="")
			 { 
				document.frm.action="newsletter.php?email="+document.frm.txtemail.value;	 
				document.frm.submit()
			  }
			 else
				{ 
  				 alert("You must type Email for sign up newsletter");
				 document.frm.txtemail.focus();
				return false
				}
		 }
 		if(style ==14)
		 {
				var strID="";
					with (document.frm)
					{
						len = elements.length;
						for(var ii=0; ii<len; ii++)
						{	
							if (elements[ii].name == "vote") 
								if (elements[ii].checked)
									strID = elements[ii].value;
						}//end for
						
					}//end with
					if(strID=="") 
					{
					 alert("Please check for poll."); 
					 return false;
					}
				 document.frm.action='resultvote.php?option='+strID;	
				 document.frm.submit()
		 }
}

function getID(mode){
	var strID="";
	with (document.frmAddshop)
	{
		len = elements.length;
		for(var ii=0; ii<len; ii++)
		{	
			if (elements[ii].name == "chkid[]") 
				if (elements[ii].checked)
					strID = strID + elements[ii].value+mode;
		}//end for
	}//end with
	return strID;
}
function checkAddcart(proid)
{	
	str2 = "Errors have occured.\n Please select at least item to add cart";  
	strID=""
	strID=getID("*");
	if (strID != "")
	{

			//frmAddshop.act.value="13";
			frmAddshop.action="shopcart.php?server=&qty=false&act=add&valid=false&pid="+proid;
			frmAddshop.submit();
	}
	else 
	alert(str2);
}
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

function OpenWin(URLStr, width, height)
{

  if(popUpWin)

  {

    if(!popUpWin.closed) popUpWin.close();

  }
var left = (screen.width-width)/2;
var top = (screen.height-height)/2;
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

// convert to NCR - UTF8
function  toRealCharCode(str1) 
{
 var c0
 var str2='' ;
 var strx=''
 var idx
 
  idx = str1.indexOf('&')
  if( idx<0 ) return str1

 var i = 0
 while ( i < str1.length )
  {
    c0 = str1.substring(i,i+1)
    i++
    if( c0 != '&' ) continue

    strx  += str1.substring(0,i-1)
    str1 = str1.substring(i-1,str1.length)
    idx = str1.indexOf(';')
    if(idx <0 ) break;
  
    str2 = str1.substring(1,idx)
    
	//2001/03/26 Bo sung them phan nhan biet cac ky tu web (31 ky tu)
	//Cac ky tu co code < 255 khong duoc IE luu duoi dang &#xxx; ma duoi dang &abc;, do do phai chu y den ngoai le nay !!!
	if (str2=='aacute') str2='225' //a'
	else if (str2=='agrave') str2='224' //a`
	else if (str2=='atilde') str2='227'  //a~
	else if (str2=='Aacute') str2='193'  //A'
	else if (str2=='Agrave') str2='192'  //A`
	else if (str2=='Atilde') str2='195'  //A~

	else if (str2=='acirc') str2='226'  //a^
	else if (str2=='Acirc') str2='194'  //A^

	else if (str2=='eacute') str2='233'  //e'
	else if (str2=='egrave') str2='232'  //e`
	else if (str2=='Eacute') str2='201'  //e'
	else if (str2=='Egrave') str2='200'  //e`

	else if (str2=='ecirc') str2='234'  //e^
	else if (str2=='Ecirc') str2='202'  //E^ 

	else if (str2=='iacute') str2='237'  //i'
	else if (str2=='igrave') str2='236'  //i`
	else if (str2=='Iacute') str2='205'  //I'
	else if (str2=='Igrave') str2='204'  //I`

	else if (str2=='oacute') str2='243'  //o'
	else if (str2=='ograve') str2='242'  //o`
	else if (str2=='otilde') str2='245'  //o~
	else if (str2=='Oacute') str2='243'  //O'
	else if (str2=='Ograve') str2='242'  //O`
	else if (str2=='Otilde') str2='245'  //O~

	else if (str2=='ocirc') str2='244'  //o^
	else if (str2=='Ocirc') str2='212'  //O^

	else if (str2=='uacute') str2='250'  //u'
	else if (str2=='ugrave') str2='249'  //u`
	else if (str2=='Uacute') str2='218'  //U'
	else if (str2=='Ugrave') str2='217'  //U`

	else if (str2=='yacute') str2='253'  //y'
	else if (str2=='Yacute') str2='221' //Y'

	else str2=str2.substring(1,str2.length);

	str1 = str1.substring(idx+1,str1.length)

    strx += String.fromCharCode(str2)
    i = 0
  }

 return strx+str1 ;

}

// Convert to UTF-8 sang JP 16 bit
  function  toWebUnicode( str1 ) {
	var code
	var str2 =''
	var len = str1.length
	for(var i=0 ; i<len; i++) {
	  code = str1.charCodeAt(i) ;
	  if (code < 128) continue ;
	  //Chi xu ly cac ky tu tieng Viet
	  //if  ((code >= 192 && code <= 511) || (code >= 7808 && code <= 7929) )
	  //{
	  str2 += str1.substring(0,i) + '%26%23' + code + ';'
	  str1 = str1.substring(i+1,str1.length)
	  len = str1.length
	  i=0
	  //}
	  //else {continue;}

	}
	str1 = str2+str1

	// Once more time
	str2 =''
	len = str1.length
	for(var i=0 ; i<len; i++) {
	  code = str1.charCodeAt(i) ;
	  if(code<128) continue ;
	  str2 += str1.substring(0,i) + '%26%23' + code + ';'
	  str1 = str1.substring(i+1,str1.length)
	  len = str1.length
	  i=0
	}

	str1 = str2+str1

	return str1 ;
  }	  

function checkvote(){
	var strID="";
	with (document.frmvote)
	{
		len = elements.length;
		for(var ii=0; ii<len; ii++)
		{	
			if (elements[ii].name == "vote") 
				if (elements[ii].checked)
					strID = elements[ii].value;
		}//end fo
		
	}//end with
	if(strID=="") 
	{
	 alert("Ban phai binh chon moi duoc xem ket qua./ You must poll before view resulf"); 
	 return false;
	}
 document.frmvote.action='resultvote.php?option='+strID;	
 return true;
}

 function m_over(divID)
{
	document.getElementById(divID).className ="rowxam";
}
function m_out(divID)
{

	document.getElementById(divID).className ="rows";
}


function getID(mode){
	var strID="";
	with (document.forms[0])
	{
		len = elements.length;
		for(var ii=0; ii<len; ii++)
		{	
			if (elements[ii].name == "chkid[]") 
				if (elements[ii].checked)
					strID = strID + elements[ii].value+mode;
		}//end for
	}//end with
	return strID;
}
function getChecked(action)
{	
	strID=""
	strID=getID("*");
	if (strID != "")
	{
		var SayYes = confirm("Cac doi tuong ban chon se bi xoa");
		if (SayYes == true){
			document.forms[0].action=action;
			document.forms[0].submit();
		}
	}
	else 
	alert("Ban phai chon doi tuong de thuc hien");
}
