/* - - - - - - - - - - - - - - - - - - - - - - -
Ajax Javascript
 - - - - - - - - - - - - - - - - - - - - - - - */
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 http=createRequestObject();
var objectId = '';
var loadok = 0;

function createRequestObject(htmlObjectId){
    var obj;
    var browser = navigator.appName;
    
    if(browser == "Microsoft Internet Explorer"){
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        obj = new XMLHttpRequest();
    }
    return obj;    
}

function send_post(strurl, parameters,objId) {      
  objectId = objId;
  http.open('POST', strurl, true);
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", parameters.length);
  http.setRequestHeader("Connection", "close");
  http.onreadystatechange = handleResponse;
  http.send(parameters);
}

function sendReq(serverFileName, variableNames, variableValues,objId) {
	var paramString = '';
	
	objectId = objId;
	
	variableNames = variableNames.split(',');
	variableValues = variableValues.split(',');
	
	for(i=0; i<variableNames.length; i++) {
		paramString += variableNames[i]+'='+variableValues[i]+'&';
	}
	paramString = paramString.substring(0, (paramString.length-1));
			
	if (paramString.length == 0) {
	   	http.open('get', serverFileName);
	}
	else {
		http.open('get', serverFileName+'?'+paramString);
	}
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
	
	if (http.readyState == 4) {
		responseText = http.responseText;
		getobj(objectId).innerHTML = responseText;
    } else {
		getobj(objectId).innerHTML = "<br><div class='loading'><img src='images/loading.gif' border=0><br>&#272;ang l&#7845;y d&#7919; li&#7879;u ...</div>";
	}
		
}

function change_icon(imgDocID,url) {
document.images[imgDocID].src = url;
}  

function showhide(id) {
el = document.all ? document.all[id] :   dom ? document.getElementById(id) :   document.layers[id];
els = dom ? el.style : el;
  if (dom){
    if (els.display == "none") {
        els.display = "";
      } else {
        els.display = "none";
      }
    }
  else if (ns4){
    if (els.display == "show") {
        els.display = "hide";
      } else { 
      els.display = "show";
         }
  }
}

function getobj(id) {
el = document.all ? document.all[id] :   dom ? document.getElementById(id) :   document.layers[id];
return el;
}

function showobj(id) {
obj=getobj(id);
els = dom ? obj.style : obj;
 	if (dom){
	    els.display = "";
    } else if (ns4){
        els.display = "show";
  	}
}

function hideobj(id) {
obj=getobj(id);
els = dom ? obj.style : obj;
 	if (dom){
	    els.display = "none";
    } else if (ns4){
        els.display = "hide";
  	}
}

// khong the phong to cua so
function openPopUp(url, windowName, w, h, scrollbar) {
   var winl = (screen.width - w) / 2;
   var wint = (screen.height - h) / 2;
   winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar ;
   win = window.open(url, windowName, winprops);
   if (parseInt(navigator.appVersion) >= 4) { 
       	win.window.focus(); 
   } 
}

// co the phong to cua so
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
win=window.open(mypage,myname,settings);}

//============================================================


function is_num(event,f){
	if (event.srcElement) {kc =  event.keyCode;} else {kc =  event.which;}
	if ((kc < 47 || kc > 57) && kc != 8 && kc != 0) return false;
	return true;
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function ToggleTextView(s, l, t, b)
{
 var $ = function (id) {return document.getElementById(id); };
 s=$(s); l=$(l); t=$(t); b=$(b);
 if (t) { t.detailView = !t.detailView; t.innerHTML=t.detailView?l.innerHTML:s.innerHTML; b.innerHTML=t.detailView?"[-]":"[+]"; }
}
function log_out(theURL){
		ht = document.getElementsByTagName("body");
		ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
		if (confirm('Do you want logout ? '))
		{
			
			ht[0].style.filter = "";
			window.location.href=theURL;
			return true;
		}
		else
		{
			ht[0].style.filter = "";
			return false;
		}
}

function submitDoc(formName) { 
  var obj;
 
	if (obj=findObj(formName)!=null) 
	{
		findObj(formName).submit(); 
	}
	else 
	{
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}

}

function do_mailist(f) {
	furl = 'act:mail_list|f_email:'+f.f_email.value;
	sendReq('index.php', 'ict', furl,'ext_maillist');
	return false;
}

function del_maillist() {
	f_email = getobj('f_email').value;
	furl = 'act:mail_list|f_email:'+ f_email + '|dodel:1' ;
	sendReq('index.php', 'ict', furl,'ext_maillist');
}


function do_sendmail(f) {
	furl = 'act:tellfriend|f_email:'+f.f_email.value;
	sendReq('index.php', 'ict', furl,'ext_tellfriend');
	return false;
}

function show_comment(p_id,mem_id,lang) {
	key='id,mem_id,lang';
	value = p_id+','+mem_id+','+lang;
	url ='ajax/_do_comment.php';
	sendReq(url,key ,value,'ext_comment');
}


function do_AddCart(id){
	quantity = 'quantity'+id;
	quan = getobj(quantity).value ;
	
	if (quan==0) {
		alert ('Vui lòng nhập số lượng cần mua')	
	}else{
		location.href='?ict=act:cart|do:add|p_id:'+id+'|quantity:'+quan;
	}
	
}

/* ------------ DoWishList -----------------*/
function do_AddWishList(mem_id,p_id){
	if (mem_id==0){
		alert ('Chức năng này chỉ dành cho thành viên . Vui lòng đăng nhập');
	}else{
		
		key='id,mem_id,action';
		value = p_id+','+mem_id+',add';
		url = 'ajax/_favourite.php';
		sendReq(url,key ,value,'ext_favorite');
	}
	
}

function PriceToEmail(){
	cat_id = getobj('model_c').value ;
	producer = getobj('producer_c').value ;
	p_id = getobj('product').value ;
	if (p_id==0){
		alert ('Vui lòng chọn 1 sản phẩm ')	
	}else{
		location.href='?ict=act:price_to_email|p_id:'+p_id;	
	}
}

function ViewComment(){
	
	p_id = getobj('product').value ;
	if (p_id==0) {
		alert ('Vui lòng chọn sản phẩm muốn xem')
	}else{
		location.href='?ict=act:product_detail|p_id:'+p_id+'#Comment';
	}
}

function GoProduct(){
	
	p_id = getobj('product').value ;
	if (p_id==0) {
		alert ('Vui lòng chọn 1 sản phẩm ')
	}else{
		location.href='?ict=act:product_detail|p_id:'+p_id;
	}
}


function CompareProduct(){
	
	p_id = getobj('product').value ;
	if (p_id==0) {
		alert ('Vui long chon 1 san pham de so sanh')
	}else{
		location.href='?ict=act:compare_product|p_id:'+p_id;
	}
}

/* ------------ DoCompare -----------------*/
function DoCompare(){
	var catRoot = 	document.f_category.catRoot.value;
	var len = document.f_category.ch_pid.length;
	var count = 0;
	var aList = new Array;
	if (len>1){
		for (i=0;i<len;i++)	{
			if (document.f_category.ch_pid[i].checked==true){
				aList.push(document.f_category.ch_pid[i].value);
				count=count+1;
			}
		}
	}else{
		if (document.f_category.ch_pid.checked==true){
			aList.push(document.f_category.ch_pid.value);
			count=count+1;
		}	
	}
	if(catRoot =='') { alert ('Không thể so sánh do không cùng chủng loại') }
	else if (count==0){ alert ('Vui lòng chọn 1 sản phẩm để so sánh') }
	else if (count>3){ alert ('Vui lòng chọn tối đa 3 sản phẩm để so sánh') }
	else {
		p_id = aList.join(',');	
		location.href='?ict=act:compare_product|catRoot:'+catRoot+'|p_id:'+p_id;	
	}
}

/* ------------ DoWishList -----------------*/
function DoWishList(mem_id,action){
	var len = document.f_category.ch_pid.length;
	var count = 0;
	var aList = new Array;
	if (mem_id==0){
		alert ('Chức năng này chỉ dành cho thành viên . Vui lòng đăng nhập');
	}else{
		if (len>1){
			for (i=0;i<len;i++)	{
				if (document.f_category.ch_pid[i].checked==true){
					aList.push(document.f_category.ch_pid[i].value);
					count=count+1;
				}
			}
		}else{
			if (document.f_category.ch_pid.checked==true){
				aList.push(document.f_category.ch_pid.value);
				count=count+1;
			}	
		}
		
		if (count==0){ alert ('Vui lòng chọn 1 sản phẩm ') }
		else {
			p_id = aList.join('-');	
			key='id,mem_id,action';
			value = p_id+','+mem_id+','+action;
			url = 'ajax/_favourite.php';
			sendReq(url,key ,value,'ext_favorite');
		}
	}
	
}

/* ------------ DoPriceEmail -----------------*/
function DoPriceEmail(){
	var len = document.f_category.ch_pid.length;
	var count = 0;
	var aList = new Array;
	if (len>1){
		for (i=0;i<len;i++)	{
			if (document.f_category.ch_pid[i].checked==true){
				aList.push(document.f_category.ch_pid[i].value);
				count=count+1;
			}
		}
	}else{
		if (document.f_category.ch_pid.checked==true){
			aList.push(document.f_category.ch_pid.value);
			count=count+1;
		}	
	}
	if (count==0){ alert ('Vui lòng chọn 1 sản phẩm ') }
	else {
		p_id = aList.join(',');	z
		location.href='?ict=act:price_to_email|p_id:'+p_id;	
	}
}

/* ------------ DoTellFriend -----------------*/
function DoTellFriend(){
	var len = document.f_category.ch_pid.length;
	var count = 0;
	var aList = new Array;
	if (len>1){
		for (i=0;i<len;i++)	{
			if (document.f_category.ch_pid[i].checked==true){
				aList.push(document.f_category.ch_pid[i].value);
				count=count+1;
			}
		}
	}else{
		if (document.f_category.ch_pid.checked==true){
			aList.push(document.f_category.ch_pid.value);
			count=count+1;
		}	
	}
	
	if (count==0){ alert ('Vui lòng chọn 1 sản phẩm ') }
	else {
		p_id = aList.join(',');	
		location.href='?ict=act:tell_friend|p_id:'+p_id;	
	}
}

/* ------------ DoAddCart -----------------*/
function DoAddCart(){
	var len = document.f_category.ch_pid.length;
	var count = 0;
	var aList = new Array;
	if (len>1){
		for (i=0;i<len;i++)	{
			if (document.f_category.ch_pid[i].checked==true){
				aList.push(document.f_category.ch_pid[i].value);
				count=count+1;
			}
		}
	}else{
		if (document.f_category.ch_pid.checked==true){
			aList.push(document.f_category.ch_pid.value);
			count=count+1;
		}	
	}
	if (count==0){ alert ('Vui lòng chọn 1 sản phẩm ') }
	else {
		p_id = aList.join(',');	
	}
}


function show_comment(p_id,mem_id,lang) {
	key='id,mem_id,lang';
	value = p_id+','+mem_id+','+lang;
	url ='ajax/_do_comment.php';
	sendReq(url,key ,value,'ext_comment');
}

function do_comment(f,p_id,mem_id,lang) {
	
	var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;
	
	
	var email =f.email.value;
	if (email == '') 	{
		alert('Vui lònh nhập email của bạn');
		f.email.focus();
		return false;
	}
		
	if (f.email.value != '' && f.email.value.match(re)==null) {
		alert('Email của bạn không hợp lệ ');
		f.email.focus();
		return false;
	}
	
	var title =f.title.value;
	if (f.title.value == '') 	{
		alert('Vui lòng nhập tiêu đề');
		f.title.focus();
		return false;
	}
	
	var content =f.content.value;
	if (f.content.value == '') {
		alert('Vui lòng nhập nội dung ');
		f.content.focus();
		return false;
	}
	
	title = encodeURIComponent(title);
	email = encodeURIComponent(email);
	content = encodeURIComponent(content);
	
	var strpara = 'title='+title+'&email='+email+'&content='+content;
	
	furl = 'ajax/_do_comment.php?id='+p_id+'&mem_id='+mem_id+'&lang='+lang+'&do_submit=1';
	send_post(furl,strpara,'ext_comment');
	
	return false;
}


function show_write_faqs() {
	furl = 'act:faqs|do:write';
	sendReq('index.php','ict', furl,'ext_faqs');
	return false;
}


function do_write_faqs(f) {
		var re =/^[a-z][a-z0-9]+[@][a-z0-9]+([.][a-z]+)+$/gi;
		var name = f.name.value;
		if (name == '') {
			alert('Vui lòng nhập tên chủa bạn');
			f.name.focus();
			return false;
		}

		var email = f.email.value;
		if (email == ''){
			alert("Vui lòng nhập đại chỉ email");
			f.email.focus();
			return false;
		}
		if (email.match(re)==null)
		{
			alert('Email không hợp lệ');
			f.email.focus();
			return false;
		}	
		var title = f.title.value;
		if (title == '') {
			alert('Vui lòng nhập nội dung cần hỏi');
			f.title.focus();
			return false;
		}
		
	
	var strpara = 'name='+encodeURIComponent(name)+'&email='+encodeURIComponent(email)+'&title='+encodeURIComponent(title);
	furl = 'index.php?ict=act:faqs|do:write|do_submit:1';
	send_post(furl,strpara,'ext_faqs');
	return false;
}

function show_faqs(id) {
	furl = 'act:faqs|do:view|f_id:'+id;
	sendReq('index.php','ict',furl,'ext_faqs');
	return false;
}


function show_tab (id) {
	key='id,lang';
	value = id+',vn';
	
	url = 'ajax/show_tab.php';
	sendReq(url,key ,value,'box_middle_tab');
	for (i=1;i<6;i++){
		var tab = 'tab'+i;
		if (i==id){
			getobj(tab).className = "on";	
		}else{
			getobj(tab).className = '';
		}
	}
	
	span = document.getElementById("div_js");
	script = document.createElement('script'); 
	script.id = 'hiddenScript'; 
	script.type = 'text/javascript'; 
	script.src = 'js/hidden.js'; 
	span.appendChild(script);
	
}

function LoadProduct(cat_id,p_id) {
	key='cat_id,p_id';
	value =cat_id+','+p_id ;
	url = 'ajax/ajax_list_product.php';
	sendReq(url,key ,value,'ext_product');
	return false;
}

function goCategory (cat_id){
	location.href='?ict=act:category|cat:'+cat_id;	
}


/*------------- button -------------*/

var fsub=false;
var mobj;
var osrc="";
var mouseOvers = new Array();
var mouseOuts = new Array();
var detect = navigator.userAgent.toLowerCase();

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function mouseGoesOver(fsub, mEvent) {
	if (checkIt("msie") != 0) { // use in IE
		if (fsub==true) {
			mobj.src=osrc;
			fsub=false;
		} else {
			this.src = mouseOvers[this.number].src;
		}
		osrc=event.srcElement.src;
		mobj=event.srcElement;
	}
	else {	// use in firefox
		if (fsub==true) {
			mobj.src=osrc;
			fsub=false;
		} else if (fsub=='')  {
			osrc=mEvent.target.src;
			mobj=mEvent.target;
		} else {
			this.src = mouseOvers[this.number].src;
		}
	}
}

function mouseGoesOut() {this.src = mouseOuts[this.number].src;	}

buttonHover = function() {
	var inputButton = document.getElementsByTagName('input');
	var imageImgs = document.getElementsByTagName('img');
	var images = new Array();
	for (var i=0; i<inputButton.length; i++) {images.push(inputButton[i]);}
	for (var i=0; i<imageImgs.length; i++) {images.push(imageImgs[i]);}
	
	for (var i=0; i<images.length; i++) 
	{
		
		if (images[i].src.indexOf('.gif') != -1){
			var imgsrc = images[i].src;
			var tmp=(imgsrc.substring(imgsrc.lastIndexOf('/') + 1)).substring(0,(imgsrc.substring(imgsrc.lastIndexOf('/') + 1)).lastIndexOf('.'));
			
			if ( tmp.indexOf('_') != -1) /* search all img have "_" */
			{
				var last_img =  tmp.substring(0,tmp.lastIndexOf('_'));
				if (tmp.substring(tmp.lastIndexOf('_')+1) == "n" )
				{
					images[i].onmouseover = mouseGoesOver;
					images[i].onmouseout = mouseGoesOut;
					mouseOuts[i] = new Image();
					mouseOuts[i].src = images[i].src;
					mouseOvers[i] = new Image();	
						
					var suffix = images[i].src.substring(images[i].src.lastIndexOf('.'));
					if (images[i].src.lastIndexOf('.') != -1) { mouseOvers[i].src = images[i].src.substring(0,images[i].src.lastIndexOf('_')) + "_o" + suffix;}
					images[i].number = i;
					
				}
			}
		}
		
	}
}
