// tim control
function $$(id)
{
	return document.getElementById(id);
}
// lay noi dung FCK Editor
function getTextFCK(FCKname)
{
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(FCKname) ;
		// Get the editor contents in XHTML.
	return oEditor.GetXHTML(true);// "true" means you want it formatted.
}
function showHidden(id)
    {
        id =$$(id);
        if(id.style.display=="")
            id.style.display="none";
        else    
            id.style.display="";
    }
function isMaxLength(title,maxLength,ctrl)
{
	s = trimSpace($$(ctrl).value);
	if(s!=null && s!="" && s.length>maxLength)
	{
		alert("Chieu dai cua " + title + " khong vuot qua " + maxLength + " ky tu!");
		$$(ctrl).focus();
		return false;
	}	
	return true;
}
function trimSpace(s)
{
	for(i=0;i<s.length;i++)
		s = s.replace("&nbsp;"," ");
	while(s.charCodeAt(0)==32 && s.length>1)
	{
		s=s.substr(1,s.length)
	}
	
	//-------------------------
	if(s.charCodeAt(0)==32 && s.length==1)
		return "";
	else
		return s;	
}
function form()
{
	return document.forms[0];
}

function isEmail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);	
}
function getEditorFCK(nameControl,type)
{
	var content = findNameControl(nameControl,type);
		content = content.replace(":","_");
		if($$(content).value=="")
			return false;
		else
			return true;
}
function showdate()
		{
			d = new Date();
			var a = new Array("Chá»§ nháº­t","Thá»© hai","Thá»© ba","Thá»© tÆ°","Thá»© nÄƒm","Thá»© sÃ¡u","Thá»© báº£y");
			s = "<b>" + a[d.getDay()] + 
				", " +	(d.getDate()<10?"0" + d.getDate():d.getDate()) +
				"/" +	(d.getMonth()+1 <10?"0" + (d.getMonth() + 1):(d.getMonth()+1)) +
				"/" +	d.getFullYear() + "</b>";
			document.write(s);			
}
function isNumber(textNum)
{
	var re = /^[0-9]{1,4}$/;	
	if (!re.test($$(textNum).value)){
		alert("Vui long dien so!");
		$$(textNum).value="1";
		$$(textNum).focus();
		return false;
	}else{ return true;}
}
function funTextArea(names, classname, width, values, rows){		
		var sBasePath = 'fckeditor/';
		var oFCKeditor = new FCKeditor( names ) ;
		oFCKeditor.BasePath	= sBasePath ;
		oFCKeditor.Height	= 250 ;
		oFCKeditor.Value	=  values ;
		oFCKeditor.Create() ;		
}
function ConvertDate(_date)	
{
	var aDate = new Array();
	aDate = _date.split("/");
	return (aDate[2] + (aDate[1].length<2?"0"+aDate[1]:aDate[1]) + (aDate[0].length<2?"0"+aDate[0]:aDate[0]));
}	
function keyWords(s0)
{
	s=$$(s0).value;		
	for(i=0;i<s.length;i++)
	{				
		a=s.charCodeAt(i);		
		if(a<48 || (a>57 && a<65) || a>122 || a==92 || a==94 || a==95 || a==96 ) //`^_\
		{			
			s = s.substr(0,i) + s.substr(i+1,s.length);
			$$(s0).value=s;
		}
	}
}
function showImage(url)
{
	var img = new Image();
	img.src = url;	
	w = img.width;
	h = img.height;
	t=(screen.availHeight - h)/2;
	l=(screen.availWidth - w)/2;
	window.open(url,"","width=" + (w + 20)+ "px,height=" + (h + 20)+ ",top=" + t +"px,left=" + l + "px,resizable=0,status=0,scrollbars=yes");
}
function delKeyWords(c1,c2){
	$$(c1).value="";	
	$$(c2).value="";
}

// id=showInfoThisHere 
function showInfoReplay(url,id,tbl){
	$('#'+id).html('<img src="skins/default/loading.gif">');
	$('#'+id).load(url,{'id':id,'tbl':tbl});
}
function showPrint(root,id,tbl){	
	table = (tbl!="news" && tbl.length>0)?tbl:"news";
	url = root+"modules/news/print.php?id="+id+"&tbl="+table;
	t=50;
	l=50;
	window.open(url,"","width=600px,height="+(screen.availHeight-100)+"px,top=" + t +"px,left=" + l + "px,resizable=0,status=0,scrollbars=yes");		
	
}
function showMenu(menuName,i){
	var s = $$(menuName+i).style.display;		
	$('div[id*='+menuName+']').each(function(){
		$(this).hide(); 
	});	
	h = (s=="none")?85:-80;
	s = (s=="none")?"":"none";
	$$(menuName+i).style.display = s;		
	/*
	hMid = $$("divCenter").clientHeight;	
	hLeft = $$("divLeft0").clientHeight;
	hMax= hMid;	
	if(hLeft > hMax)
		hMax = hLeft;				
	hContent = $$("divContent").clientHeight;		
	if(hContent > hMax)
		hMax = hContent;	
	$$("divLeft").style.height = (hMax + h)+"px";	
	$$("divCenter").style.height = (hMax)+"px";
	$$("divRight").style.height = (hMax + h)+"px";	
	$$("divLine").style.height = (hMax  + h)+"px";	
	*/
}
function randCaptCha(img,text){		
		var strCaptCha = CyloCaptcha.genRan(6);
		var HTMLCode = CyloCaptcha.display(strCaptCha);	
		$(text).val(strCaptCha);
		$(img).html(HTMLCode);
}
function showEmail(a,b,c){			
	$("#showInfoThisHere").html("<img src=" + a +"/skins/default/loading.gif>Loading...");
	$("#showInfoThisHere").load(a+"/modules/"+c+"/email.php?url="+b+"&tbl="+c);
}
function showReplay(a,b,c){
	$("#showInfoThisHere").html("<img src=" + a +"/skins/default/loading.gif>Loading...");
	$("#showInfoThisHere").load(a+"/modules/"+c+"/replay.php?id="+b+"&tbl="+c);
}
function checkReplay(s){
	if($("#txtSender").val()=="" || $("#txtAddress").val()=="" || $("#txtContent").val()==""){
		alert('Vui long nhap day du thong tin!');
		return false;
	}else{
		email = $("#txtFromMail").val();		
		if(email.length>0 && !isEmail(email)){			
			alert('Vui long kiem tra dia chi email!');
			return false;
		}
		strCC = $("#txtCaptCha").val();
		strCC_ = ($("#hidCaptCha").val()).substr(0,6);
		if(strCC.toLowerCase()!=strCC_.toLowerCase()){				
			alert('Vui long nhap dung ma bao ve hoac ma bao ve chua dung!');
			return false;
		}
		else{			
			$.ajax({
				type:'POST',
				url:'modules/'+s+'/replay.php',
				data:{	'act':'insert','id':$('#hidNewsID').val(),
						'name':$('#txtSender').val(),
						'address':$('#txtAddress').val(),
						'email':$('#txtFromMail').val(),
						'phone':$('#txtPhone').val(),
						'content':$('#txtContent').val()},
				success:function(data){					
					$("#showInfoThisHere").html("");
					alert("Thong tin da duoc cap nhat. Xin chan thanh cam on ban!" + data);
					},
				error:function(){
					alert('May chu qua tai...');
					}
				});
		}
	}
}
function sendMail(){
	if($("#txtFrom").val()=="" ||
		$("#txtFromEmail").val()=="" ||
		$("#txtTo").val()=="" ||
		$("#txtToEmail").val()=="" ){
		alert('Vui long nhap day du thong tin!');
		return false;
	}else{
		strCC = $("#txtCaptCha").val();
		strCC_ = ($("#hidCaptCha").val()).substr(0,6);
		if(strCC.toLowerCase()!=strCC_.toLowerCase()){				
			alert('Vui long nhap dung ma bao ve!');
			return false;
		}
		else{
			if(isEmail($("#txtFromEmail").val()) && isEmail($("#txtToEmail").val())){	
				$.ajax({
					   	type:'POST',
						url:'modules/inc/sendmail.php',
						data:{  'act':'send1',
								'id':$('#hidNewsID').val(),
								'sender':$('#txtSender').val(),
								'femail':$('#txtFromMail').val(),
								'reciever':$('#txtReciever').val(),
								'temail2':$('#txtToMail').val()								
							},
						success:function(){
							$("#showInfoThisHere").html("");
							alert("Thong tin da duoc goi");
							},
						error:function(){
							alert('May chu qua tai.Thanh that xin loi!');
							}
				});
			}else{
				alert('Vui long kiem tra lai dinh dang email!');				
				return false;
			}			
		}
	}		
}
// lien he

//GIO HANG
function checkSendOrder(){
	if(trimSpace($("#txtNameOrder").val())=="")		 
	{
		alert('Vui long nhap ho ten!');
		$("#txtNameOrder").focus();
		return false;
	}
	if(trimSpace($("#txtPhone").val())=="")		 
	{
		alert('Vui long nhap so dien thoai!');
		$("#txtPhone").focus();
		return false;
	}
	if(trimSpace($("#txtEmail").val())=="")		 
	{
		alert('Vui long nhap email!');
		$("#txtEmail").focus();
		return false;
	}
	email = trimSpace($("#txtEmail").val());		
	if(email.length>0 && !isEmail(email))
	{			
		alert('Vui long kiem tra dia chi email!');
		$("#txtEmail").focus();
		return false;
	}
	if(trimSpace($("#txtAddress").val())=="")		 
	{
		alert('Vui long nhap dia chi giao hang!');
		$("#txtAddress").focus();
		return false;
	}
	if(trimSpace($("#txtTime").val())=="" || trimSpace($("#txtDate").val())=="")		 
	{
		alert('Vui long nhap thoi gian giao hang!');
		$("#txtTime").focus();
		return false;
	}
	
	if (trimSpace($('#txtDate').val())!=""){
		d = CompDateOrder(trimSpace($('#txtDate').val()),trimSpace($('#hidToday').val()));
		if(d){
		  alert("Ngay giao hang phai lon hon ngay hien tai!");
		    $("#txtDate").focus();
			return false;
		}
	}
	request = trimSpace($("#txtOtherInfo").val());		
	if(request.length>500)
	{			
		alert('Noi dung cac yeu cau khac khong qua 500 ky tu!');
		$("#txtOtherInfo").focus();
		return false;
	}
	
		strCC = $("#txtCaptCha").val();
		strCC_ = ($("#hidCaptCha").val()).substr(0,6);
		if(strCC.toLowerCase()!=strCC_.toLowerCase()){				
			alert('Vui long nhap dung ma bao ve hoac ma bao ve chua dung!');
			$("#txtCaptCha").focus();
			return false;
		}
		else{			
			$.ajax({
				   	type:'POST',
					url:'modules/basket/sendorder.php',
					data:{  'act':'sendorder',							
							'name':$('#txtNameOrder').val(),
							'email':$('#txtEmail').val(),
							'phone':$('#txtPhone').val(),
							'address':$('#txtAddress').val(),
							'otherinfo': $('#txtOtherInfo').val(),
							'recievetime': ($('#txtTime').val() +  " - " + $('#txtDate').val())
						},
					success:function(data){						
						$('#divOrderform').html("");
						strinfo = "<table  cellpadding='2' cellspacing='2'  border='0'  width='100%'>";
						strinfo +="<tr><td height=30 align=center style='color:#Bd0000;'><b>C&#7843;m &#417;n qu&#253; kh&#225;ch &#273;&#227; mua h&#224;ng <br>Th&#244;ng tin &#273;&#227; &#273;&#432;&#7907;c g&#7903;i !</b></td></tr>";
						strinfo +="<tr><td height=30 align='center'>[ <a href='./'> Trang ch&#7911;... </a> ]</td></tr></table>";
						$('#divInfo').html(strinfo);
						},
					error:function(){
						alert('May chu qua tai.Thanh that xin loi!');
						}
				});
		}
}

// add to store
function Add2Store(id){	
	var numProduct = "#store_"+id;
	var fExist= false;
	var total = 0;
	try{
		//chua ton tai
			if(typeof($(numProduct).val())=="undefined"){
				fExist= false;
			}else{
				fExist= true;
				total = $(numProduct).val();
			}
	}catch(ex){
		fExist= false;
		}
		//if (total==99)
		//{
			//$(numProduct).val()="1234";
			//$("#store_"+id).attr("disabled", true);

		//}	
		$('#divStore').load('modules/products/add2store.php?id='+id+'&total='+total);
		
}
//-- chuyen so sang dinh dang chuoi
function changeSo(so)
{
	loop=1;	
	kq="";	
	for(i=so.length-1;i>=0;i--){												
		if(loop==3){ // 000							
			kq="." + so.substr(i,1) + kq;
			loop=1;
		}else{
			kq=so.substr(i,1) + kq;
			loop++;
		}					
	}		
	if(kq.substr(0,1)=='.'){
		kq=kq.substr(1,kq.length);
	}
	return kq;	
}
//------
function updateStore(id){		
		if(isNumber(id)){						
			var Store_value_toal = 0;
			$('input[id*=store_]').each(function(){
				storeItemtValue = $(this).val();				
				store_id= this.id; // luu tru so luong
				
				arr_Store = Array();
				arr_Store = store_id.split("_");				
				
				product_id = arr_Store[1];
				tdCost = $('#tdCost_'+product_id).html(); 												
							
				Store_value_toal = Store_value_toal + tdCost*storeItemtValue; // tong gia thanh
				value = "";				
				value = tdCost*storeItemtValue;								
				$('#tdTotal_'+product_id).html(value);				
			});
			value = Store_value_toal;				
			$('#tdTotal').html('<font color="red"><b>'+value+' VNÐ</b></font>');			
		}
}
function saveStore(){
		save2Store = "";
		ok=false;
		del=false;
		
		$('input[id*=store_]').each(function(){			
			storeItemtValue = $(this).val();				
			store_id = this.id; // luu tru so luong				
			arr_Store = Array();
			arr_Store = store_id.split("_");				
			product_id = arr_Store[1]; 
			//kiem tra co xoa item nao khong??			
			chk = $$('chkStore_' + product_id).checked;	
			//------------------------------------------
			if(!ok && chk){
				if(confirm('Ban thuc su muon xoa nhung san pham da chon trong gio hang khong?')){
					del=true;
				}
				ok=true;
			}			
			// xoa neu chap nhan xoa trong gio hang
			if(del && !chk){
				save2Store += "|" + product_id + "," + storeItemtValue;				
			}
			if(!del){ // khong dong y xoa trong gio hang
				save2Store += "|" + product_id + "," + storeItemtValue;								
			}
		});
		if(save2Store.length>0){			
			$('#divStore').load('modules/products/add2store.php?act=update&v='+save2Store);
			window.location.href="?cat_id=1001";
		}
	}
function deleteStore()
	{
		if(confirm('Ban co chac chan xoa tat ca san pham co trong gio hang khong?')){
			$('#divStore').load('modules/products/add2store.php?act=del');
			window.location.href="./";
		}		
	}
function deleteOne(product_id)
	{
		if(confirm('Ban co chac chan xoa san pham nay trong gio hang khong?')){		
			$('#divStore').load('modules/products/add2store.php?act=delone&v=' + product_id);
			window.location.href = "?cat_id=1001";
		}		
	}	
function CompDateOrder(strDate1,strDate2){
		var m1, d1, y1;
		var m2, d2, y2;
		var s, pos1, pos2;
		var s = strDate1, s1 = strDate2;
		if (s.length == 0 || s1.length == 0) return true;

		pos1 = s.indexOf("/",0);
		pos2 = s.indexOf("/",pos1+1);
		d1 = parseInt(s.substr(0,pos1),10);
		m1 = parseInt(s.substr(pos1+1,pos2-pos1-1),10);
		y1 = parseInt(s.substr(pos2+1,s.length-pos2),10);
		
		pos1 = s1.indexOf("/",0);
		pos2 = s1.indexOf("/",pos1+1);
		d2 = parseInt(s1.substr(0,pos1),10);
		m2 = parseInt(s1.substr(pos1+1,pos2-pos1-1),10);
		y2 = parseInt(s1.substr(pos2+1,s1.length-pos2),10);
		
		var d1 = new Date(y1,m1,d1);
		var d2 = new Date(y2,m2,d2);
		if (d1 < d2 || d1 == d2) 
			return true;
		else
			return false;
	}	
	
