// JavaScript Document
function hide(sid) {
document.getElementById(sid).style.display = "none";
}

function show(sid) {
document.getElementById(sid).style.display = "block";
}

function alertandgo(msg,tourl){
	if (confirm (msg)){
	window.location=tourl;
	}
}

//去掉前后空格-----
function trim(str) {    
    return (str + '').replace(/(\s+)$/g, '').replace(/\s+/g, '');    
}

//取右侧字符
function Right(Str,n){
    return Str.substr(Str.length-n);
}

//是否图片URL格式--------
function Checkimgurl(Str){
	if (Str.length < 7 ) return false;
	Str= Str.toUpperCase();
	if ( Str.substr( 0,7) !='HTTP://') return false;
	if ( Str.indexOf('.') == -1 ) return false;
	var lei=".jpg.gif.bmp.png.jpeg"	;
	Str=Right(Str,4).toLowerCase();
	if ( lei.indexOf(Str) != -1 ){
		return true;
	}else{
	    return false;
	}
}

//图片缩略图(居中剪裁)-----
function chgimg(obj,w,h){
	imgobj=obj;
	img_w = imgobj.width;
	img_h = imgobj.height;
	if(img_w!=0){
		if(img_w>w || img_h>h){	
			if (img_h*w/img_w>h){
			   imgobj.width=w;
			   imgobj.style.marginTop=Math.round((h - img_h*w/img_w)/2) + "px"
			}else{
			   imgobj.height=h;
			   imgobj.style.marginLeft=Math.round((w - img_w*h/img_h)/2) + "px"
			   
			}
		}else{
			imgobj.style.marginLeft=Math.round((w - img_w)/2) + "px"
			imgobj.style.marginRight=Math.round((w - img_w)/2) + "px"
			imgobj.style.marginTop=Math.round((h - img_h)/2) + "px"	
		}
	}else{
		imgobj.width=w;
	}
}

function lostpic(obj){
	obj.src="images/nophoto.jpg";	
}


//数字校验---------------
function  isNumber(Str){   
var  Letters="1234567890.";  //可以自己增加可输入值 
var  i; 
var  c; 
for(i=0; i<Str.length; i++) {   
	c=Str.charAt(i); 
	if (Letters.indexOf(c)<0) {
	   return  false; 
	}
} 
return  true; 
} 

function checknum(obj){
	if (!isint(trim(obj.value))){
		obj.value=1;
	}
}
function isint(str){
	if(str=="0"){
		return  false;
	}
	var  Letters="1234567890";  //可以自己增加可输入值 
	var  i; 
	var  c; 
	for(i=0; i<str.length; i++) {   
	  c=str.charAt(i); 
	  if (Letters.indexOf(c)<0) {
		 return  false; 
	  }
	} 
    return  true; 	
}


function isIE(){ //ie? 
    if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) 
        return true; 
    else 
        return false; 
} 
//让fireFox支持innerText的方法
if(!isIE()){ //firefox innerText define
    HTMLElement.prototype.__defineGetter__( "innerText", 
        function(){ 
            return this.textContent.replace(/(^\s*)|(\s*$)/g, "");
        } 
    ); 
    HTMLElement.prototype.__defineSetter__( "innerText", 
        function(sText){ 
            this.textContent=sText; 
        } 
    ); 
}

function cabook(caid,nid,price){
	// 是否有cookie
	if(document.cookie.length==0){ 
	  alert("This Page Need Cookies!");
	  return;
	}
	
	var canum=document.getElementById(nid).value;
	if(!isint(canum)){
		canum=1;
	}
	
	var caname=document.getElementById(caid).innerText;
	caname=caname.replace(/\|\|/g,"| |")
	caname=caname.replace(/\$\$/g,"$ $");
	caname=caname.replace(/\n/g," ");
	var str="";
	var newca=true;//
	
	if(getCookie("myfoods")){ //检查重复，重新负值
		var food_book=getCookie("myfoods").split("||");		
		for (var i=0;i<food_book.length;i++){
			if(food_book[i]){
			  var food=food_book[i].split("$$");
			  if(food[0]==caid){//重复加量
			      var num=Number(food[1])+Number(canum);				  
				  food[1]=num;
				  newca=false;				  
			  }
			  food_book[i]=food[0]+"$$"+food[1]+"$$"+food[2]+"$$"+food[3];
			  str+=food_book[i]+"||";
			}
		}
		if(newca){//新加
			 str+=caid+"$$"+canum+"$$"+price+"$$"+caname+"||";
		}
	}else{ 
		str=caid+"$$"+canum+"$$"+price+"$$"+caname+"||";		
	}
	//写入cookie    
    setCookie("myfoods",str);
	changorder(str);//改变本页定单
	getpos('sbox');
	show('sbox');
	show('bkvid');
	//alert("Add OK!");
}
//div position
NS6 = (document.getElementById&&!document.all)
IE = (document.all)
NS = (navigator.appName=="Netscape" && navigator.appVersion.charAt(0)=="4")

function truebody(){
	return (document.compatMode!="BackCompat")? document.documentElement : document.body
}

function getpos(divid){ //弹出位置
	if (NS||NS6){
		winY = window.pageYOffset+10;
	}
	if (IE){
		winY = truebody().scrollTop+10;
	}
	document.getElementById(divid).style.paddingTop=winY+"px"	
}

function clearorder(){	
	//写入cookie    
    setCookie("myfoods","");
	window.location.reload();
	}

// 设置cookie 
function setCookie(Name,cookievalue) { 
  var newcookie=Name+"="+escape(cookievalue); 
  document.cookie=newcookie; 
} 

// 获取指定名称的cookie值 
function getCookie(name){ 
  var result = null; 
  var myCookie = document.cookie + ";"; 
  var searchName = name + "="; 
  var startOfCookie = myCookie.indexOf(searchName); 
  var endOfCookie; 
  if (startOfCookie != -1) { 
	startOfCookie += searchName.length; 
	endOfCookie = myCookie.indexOf(";",startOfCookie); 
	result = unescape(myCookie.substring(startOfCookie, endOfCookie)); 
  } 
  return result; 
} 

// 删除指定名称的cookie 
function clearCookie(name){ 
  var ThreeDays=3*24*60*60*1000; 
  var expDate = new Date(); 
  expDate.setTime(expDate.getTime()-ThreeDays); 
  document.cookie=name+"=;expires="+expDate.toGMTString(); 
} 

//order car start--------------------------
function showmyorder(){	
	var count=0;
	var numall=0;
	var str="";
	    str+="<table class=\"r_od\"><tr>";
		str+="<th>Produit(s)</td>";
        str+="<th width=\"60\">Prix</td>";		
		str+="<th colspan=\"3\" >Qté</td>";
		str+="<th width=\"80\">FR </td";		
		str+="</tr>";
	var cookiestr="";
	var f_i="";
	var f_n="";
	var f_m="";
	var f_p="";
	for(i=0;i<fid.length;i++){
		cookiestr += fid[i]+"$$"+fnum[i]+"$$"+fprice[i]+"$$"+fname[i]+"||";
		count += Number(fprice[i])*Number(fnum[i]);
		numall += Number(fnum[i]);
		f_i += fid[i]+"||";
		f_n += fname[i]+"||";
		f_m += fnum[i]+"||";
		f_p += fprice[i]+"||";
		str+="<tr>";
		str+="<td>"+fname[i]+"</td>";
        str+="<td class=\"num\">"+formatprice(fprice[i])+"</td>";		
		str+="<td class=\"bw\"><div class=\"but_s\" onclick=\"changenum("+i+",1);\">+</div></td>";		
		str+="<td class=\"num\" >"+fnum[i]+"</td>";
		str+="<td class=\"bw\"><div class=\"but_s\" onclick=\"changenum("+i+",-1);\">-</div></td>";		
		str+="<td class=\"num\">"+formatprice(Number(fprice[i])*Number(fnum[i]))+"</td>";		
		str+="</tr>";
	}
	str+="</table>";
	str+="<div class=\"countall\">"+"Total"+numall+", FR "+formatprice(count)+"</div>";
	document.getElementById("myorder").innerHTML=str;
	//改变cookie
    setCookie("myfoods",cookiestr);
	//表单付值
	if(fid.length>0){
	  document.getElementById("pid").value=f_i;
	  document.getElementById("pname").value=f_n;
	  document.getElementById("pnum").value=f_m;
	  document.getElementById("pprice").value=f_p;
	  document.getElementById("pcount").value=formatprice(count);
	  document.getElementById("psubmit").disabled=false;	  
	}else{
	  document.getElementById("psubmit").disabled=true;
	  
	}
}
//list orders
function changorder(cookiestr){
	if(!cookiestr){
		return
	}
	var count=0;
	var numall=0;
	var cais=cookiestr.split("||");
	var str="";
	    str+="<table class=\"r_od\"><tr>";
		str+="<th>Produit(s)</td>";
        str+="<th>Prix</td>";		
		str+="<th>Qté</td>";
		str+="<th>FR</td";		
		str+="</tr>"
	for(i=0;i<cais.length;i++){
        if(cais[i]){
			var my_o=cais[i].split("$$");
			count += Number(my_o[2])*Number(my_o[1]);
			numall += Number(my_o[1]);
			str+="<tr >";
			str+="<td>"+my_o[3]+"</td>";
			str+="<td align=\"right\">"+formatprice(my_o[2])+"</td>";		
			str+="<td align=\"right\">"+my_o[1]+"</td>";
			str+="<td align=\"right\">"+formatprice(Number(my_o[2])*Number(my_o[1]))+"</td>";		
			str+="</tr>";
		}
	}
	str+="</table>";
	str+="<div class=\"countall\">"+"Total"+numall+", FR "+formatprice(count)+"</div>";
	document.getElementById("r_orders").innerHTML=str;	
}

function formatprice(p){
	var n=Math.floor(p*100)/100;
	var num=n.toString();
	var pp=num.split(".");
	if(pp.length<2){
		num+=".00"
	}else if(pp[1].length<2){
		num+="0"
	}
	return num;
}

function changenum(id,num){
	//处理数组
	var ct=Number(fnum[id])+num;	
	if(ct<1){
		//清除数组
		fid.splice(id,1);
		fname.splice(id,1);
		fnum.splice(id,1);
		fprice.splice(id,1);		
	}else{
		fnum[id]=ct;
	}	
	//表单重绘
	showmyorder()
}
//order car end--------------------------

//套餐start--------------------
function taocanaddcai(){
	//数组处理
	var oSelect=document.getElementById("selcai");	
	 for(var i=0;i<oSelect.options.length;i++){
		if(oSelect.options[i].selected){
			var mid=oSelect.options[i].value;
			var na=oSelect.options[i].text;
			if(("||"+c_ids).indexOf("||"+mid+"||")<0){
				aids.push(mid);
				anames.push(na);
				apces.push(1);
			}			  
		}		 
	 }	
    drawlist();	
}

function delid(id){
	aids[id]="";
	anames[id]="";
	apces[id]="";
	drawlist();	
}

function setpice(obj,id){
	apces[id]=obj.value.replace(/\|/g,"");
	setform();
}

function drawlist(){
	var str="";
	for(i=0;i<aids.length;i++){
		if(aids[i]){
			str+="<ul>";
			str+="<li class=\"cname\">"+anames[i]+"</li>";
			str+="<li>Modèle:<input  type=\"text\" class=\"input\" value=\""+apces[i]+"\" size=\"12\" onchange=\"setpice(this,"+i+")\"/></li>";
			str+="<li class=\"close\" onclick=\"delid("+i+")\">X</li>";
			str+="</ul>";
		}
	}	
	if(str){
		document.getElementById("clist").innerHTML=str;		
	}else{
		document.getElementById("clist").innerHTML="Choisir votre commande";
	}
	setform();
}

function setform(){
	c_ids="";
	c_names="";
	c_pces="";
	for(i=0;i<aids.length;i++){
		if(aids[i]){
			//组串
			c_ids+=aids[i]+"||";
			c_names+=anames[i]+"||";
	        c_pces+=apces[i]+"||";
		}		
	}
	document.getElementById("m_ids").value=c_ids;
	document.getElementById("m_names").value=c_names;
	document.getElementById("m_pces").value=c_pces;
}
//套餐end--------------------

//菜单------------------
//drop menu
var mmHideMenuTimer = null;
var hideTimeout=900; //timeout 
var m01 = null;  //ID for hide
var pos_y =0 ; 
var pos_X =0 ; 
function pmenu(div_id){
	if (m01) {
	hide(m01);
	}
    show(div_id);
	m01 = div_id;
	ResetTimeout();
	Timeout();
}
	

function popmenu(div_id){
	if (m01) {
	hide(m01);
    }
    document.getElementById(div_id).style.left = pos_X + 'px';
	document.getElementById(div_id).style.top = pos_y + 'px';
	show(div_id);
	m01 = div_id;
	ResetTimeout();
	Timeout();
}
	
//for Firefox mouse position
function DisplayMp(event){
	pos_y = event.clientY + document.documentElement.scrollTop; //page rolled height 'scrollLeft'
	pos_X = event.clientX + document.documentElement.scrollLeft;
   	pos_y = pos_y - 70 ;	
	pos_X = pos_X - 60 ;
}

function Timeout() {
	mmHideMenuTimer = setTimeout("HideMenus()", hideTimeout);	
}

function HideMenus() {
	ResetTimeout();
	if(m01) {
		hide(m01);
	}
}
function ResetTimeout() {
	if (mmHideMenuTimer) clearTimeout(mmHideMenuTimer);
	mmHideMenuTimer = null;
}
//菜单 end-------------------------------------

function delconfirm(url){
	if(confirm("Vous etres sur de la supprimer?")){
		window.location=url;
	}else{
		return
	}
	
	}
 
 var mmOpenContainer = null;
var mmOpenMenus = null;
var mmHideMenuTimer = null;

function MM_menuStartTimeout(hideTimeout) {
	mmHideMenuTimer = setTimeout("MM_menuHideMenus()", hideTimeout);	
}

function MM_menuHideMenus() {
	MM_menuResetTimeout();
	if(mmOpenContainer) {
		var c = document.getElementById(mmOpenContainer);
		c.style.display= "block";
		mmOpenContainer = null;
	}
	if( mmOpenMenus ) {
		for(var i in mmOpenMenus) {
			var m = document.getElementById(mmOpenMenus[i]);
			m.style.display= "none";			
		}
		mmOpenMenus = null;
	}
}

function MM_menuHideSubmenus(menuName) {
	if( mmOpenMenus ) {
		var h = false;
		var c = 0;
		for(var i in mmOpenMenus) {
			if( h ) {
				var m = document.getElementById(mmOpenMenus[i]);
				m.style.display= "none";
			} else if( mmOpenMenus[i] == menuName ) {
				h = true;
			} else {
				c++;
			}
		}
		mmOpenMenus.length = c+1;
	}
}

function MM_menuOverMenuItem(menuName, subMenuSuffix) {
	MM_menuResetTimeout();
	MM_menuHideSubmenus(menuName);
	if( subMenuSuffix ) {
		var subMenuName = "" + menuName + "_" + subMenuSuffix;
		MM_menuShowSubMenu(subMenuName);
	}
}

function MM_menuShowSubMenu(subMenuName) {
	MM_menuResetTimeout();
	var e = document.getElementById(subMenuName);
	e.style.display= "block";
	if( !mmOpenMenus ) {
		mmOpenMenus = new Array;
	}
	mmOpenMenus[mmOpenMenus.length] = "" + subMenuName;
}

function MM_menuResetTimeout() {
	if (mmHideMenuTimer) clearTimeout(mmHideMenuTimer);
	mmHideMenuTimer = null;
}

function MM_menuShowMenu(containName, menuName) {
	MM_menuHideMenus();
	MM_menuResetTimeout();
	MM_menuShowMenuContainer(containName);
	MM_menuShowSubMenu(menuName);
}

function MM_menuShowMenuContainer(containName) {	
	var c = document.getElementById(containName);
	var s = c.style;
	s.display = "block";
	
	mmOpenContainer = "" + containName;
}
