//var hostname = "http://localhost:8080/buy";
 
//10.0.0.21
var hostname = "http://buy.officedepot.com.cn";
 
var v_pid = '';

 function displayPage(currentPage,pageCount) {
   var endPage = 10;
   var startPage = 1;
   var str = "结果页码： ";
   var j = 0;
   if(pageCount > 1 && currentPage != 1){
     j = currentPage - 1; 
     str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>&lt;&nbsp;上一页&nbsp;</strong></A> | ";
   }

   if(pageCount >= currentPage){
     if(pageCount <= endPage){
       if(pageCount == 1){
           str += "<font color='#FF0000'><strong>" + 1 + "</strong></font>";
       }else{
           for (j = 1; j <= pageCount; j++) {
             if(j != currentPage)
               str += " <A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>" + j + "</strong></A> | ";
             else str += "<font color='#FF0000'><strong>" + j + "</strong></font> | ";
           }
       }
     }else{
       if(currentPage == 1){
         endPage = 10;
         startPage = 1;            	
       }else if(currentPage > 1 && currentPage <= 10){
         endPage = endPage + currentPage - 1;
         startPage = 1;
       }else{
         endPage = endPage + currentPage - 1;
         startPage = currentPage - 10;
       }

       if(endPage <= pageCount){
         for (j = startPage; j <= endPage; j++) {
           if(j != currentPage)
             str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>" + j + "</strong></A> | ";
           else str += "<font color='#FF0000'><strong>" + j + "</strong></font> | ";
         }
       }else{
         for (j = startPage; j <= pageCount; j++) {
           if(j != currentPage)
             str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>" + j + "</strong></A> | ";
           else str += "<font color='#FF0000'><strong>" + j + "</strong></font> | ";
         }         
       }
     }    
   }

   if(pageCount > 1 && currentPage < pageCount){
     j = currentPage + 1; 
     str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>&nbsp;下一页&nbsp;></strong></A> ";
   }
   document.writeln(str);
 }
 
 function displayPage_en(currentPage,pageCount) {
   var endPage = 10;
   var startPage = 1;
   var str = "Page： ";
   var j = 0;
   if(pageCount > 1 && currentPage != 1){
     j = currentPage - 1; 
     str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>&lt;&nbsp;Prev&nbsp;</strong></A> | ";
   }

   if(pageCount >= currentPage){
     if(pageCount <= endPage){
       if(pageCount == 1){
           str += "<font color='#FF0000'><strong>" + 1 + "</strong></font>";
       }else{
           for (j = 1; j <= pageCount; j++) {
             if(j != currentPage)
               str += " <A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>" + j + "</strong></A> | ";
             else str += "<font color='#FF0000'><strong>" + j + "</strong></font> | ";
           }
       }
     }else{
       if(currentPage == 1){
         endPage = 10;
         startPage = 1;            	
       }else if(currentPage > 1 && currentPage <= 10){
         endPage = endPage + currentPage - 1;
         startPage = 1;
       }else{
         endPage = endPage + currentPage - 1;
         startPage = currentPage - 10;
       }

       if(endPage <= pageCount){
         for (j = startPage; j <= endPage; j++) {
           if(j != currentPage)
             str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>" + j + "</strong></A> | ";
           else str += "<font color='#FF0000'><strong>" + j + "</strong></font> | ";
         }
       }else{
         for (j = startPage; j <= pageCount; j++) {
           if(j != currentPage)
             str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>" + j + "</strong></A> | ";
           else str += "<font color='#FF0000'><strong>" + j + "</strong></font> | ";
         }         
       }
     }    
   }

   if(pageCount > 1 && currentPage < pageCount){
     j = currentPage + 1; 
     str += "<A href=\"JavaScript:tunePage(" + j + ",'start')\"><strong>&nbsp;Next&nbsp;></strong></A> ";
   }
   document.writeln(str);
 }     

 function tunePage(toPageNo,pageNo) {
   try {
     var topage = 1;
     if(typeof(toPageNo) == "number" && toPageNo < 0) topage = 0;
     else topage = toPageNo;

     var olds = window.location.search;
     if(typeof(pageNo) == "undefined" || pageNo == "") pageNo = "pageNo";

     var news = "";
     if(olds.length > 1) {
       olds = olds.substring(1,olds.length);
       var arrays = olds.split("&");
       for (var i = 0; i < arrays.length ; i++){
         if(arrays[i].indexOf(pageNo + "=") < 0 && arrays[i].length > 1) {
           news += "&" + arrays[i];
         }
       }
       if(news.length > 1) {
         news = "?" + news.substring(1,news.length) + "&" + pageNo + "=" + topage;
       }else {
         news = "?" + pageNo + "=" + topage;
       }
     }else {
       news = "?" + pageNo + "=" + topage;
     }
     window.location = window.location.pathname + news;
   }catch(e) {
     window.location = window.location.pathname + window.location.search;
   }
 }             

 function getQueryString( sProp ) {
   var re = new RegExp( sProp + "=([^\\&]*)", "i" );
   var a = re.exec( document.location.search );
   if( a == null )return "";
   return a[1];
 }  

function sAll(obj) {
    if(obj.length == null){
	    obj.checked = true;
	    tab.style.background = "#FEF3DF";
    }else{
      for(var i = 0;i < obj.length;i++){
	      obj[i].checked = true;
	      tab[i].style.background = "#FEF3DF";
	    }
    }     
}

function sReverse(obj) {
   if(obj.length == null){
	  obj.checked = false;
	  tab.style.background = "";
   }else{
     for(var i = 0;i < obj.length;i++){
	    obj[i].checked = !obj[i].checked;
	    if(obj[i].checked){
	      tab[i].style.background = "#FEF3DF";
	    }else{
		   if(i%2 != 0){
	        tab[i].style.background = "#EDEDED";
	      }else{
		    tab[i].style.background = "";
	      }
	    }				
	  }
   }     
} 

function clearSelect(obj){
   if(obj.length == null){
	  obj.checked = false;
	  tab.style.background = "";
   }else{
     for(var i = 0;i < obj.length;i++){
	    obj[i].checked = false;
	    if(i%2 != 0){
	      tab[i].style.background = "#EDEDED";
	    }else{
		  tab[i].style.background = "";
		    }				
		  }
	   }
}
 
function replaceAll(str){
  var tmp = "";
  var position = 0;
  for(var i = 0;i < str.length;i++){
    if(str.charAt(i) == 's' && i + 1 < str.length && str.charAt(i + 1) == 'u' && i + 2 < str.length &&
       str.charAt(i + 2) == 'r' && i + 3 < str.length && str.charAt(i + 3) == 'l' && i + 4 < str.length &&
       str.charAt(i + 4) == '='){
    	position = i + 4;
    	break; 
    }	
  }
  tmp = str.substring(position + 1, str.length);
  str = str.substring(0, position + 1);
  for(var j = 0;j < tmp.length;j++){
  	if(tmp.charAt(j) == '&'){
  		tmp = tmp.substring(0, j) + "@" + tmp.substring(j + 1, tmp.length);
  	}
  }
  str = str + tmp;
  return str;
}

function isInt( d_int)
{
	var checkOK = "0123456789,";
	var checkStr = d_int;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		  if (ch == checkOK.charAt(j))break;
		if (j == checkOK.length){
			return false;
		}
	}
	return true;
}	

function qtychecking(qtyobj,language){
	if(isNull(qtyobj.value) || !isInt(qtyobj.value) || new Number(qtyobj.value)<=0){
		if(language == 0){
	       alert("Qantity must be a number!");
	    }else{
	       alert("购买数量必须是整数!");
	    }			
		qtyobj.focus();
		return false;
	}
	return true;
}

function bulkqtycheck(qtyobj,language){
	if(isNull(qtyobj.value) || !isInt(qtyobj.value) || new Number(qtyobj.value)<=0){
		if(language == 0){
	       alert("Qantity must be a number!");
	    }else{
	       alert("购买数量必须是整数!");
	    }			
		qtyobj.focus();
		return false;
	}
	return true;
}

function goDoing(dowhat,language)//商品对比&&//收藏夹
{
	var str = "";
	var count = 0;
	var m_sIds = document.all.m_sId; 
	//alert(favoritesCookie);

	for(var i = 0;i < m_sIds.length;i++){
		if(m_sIds[i].checked){
				count++;
				if(str == "")
				  str = m_sIds[i].value;
				else
				  str = str + "," + m_sIds[i].value;
		}
		//m_sIds[i].checked = false;
	}

	if(((count < 2 ) || (count > 7)) && (dowhat == 1) ){
		if(language == 0)alert("Please choose 2-6 products!");
		else alert("商品对比请控制在2-6种商品!");
		return;
	}	

	if(str == ""){
		if(dowhat == 1){
	        if(language == 0){
	        	alert("Please choose products!");
	        }else{
	        	alert("请选择商品进行比较!");
	        }
	    }else if(dowhat == 2){
	        if(language == 0){
	        	alert("Please choose products!");
	        }else{
	        	alert("请选择要收藏的商品!");
	        }
	    }
	}else{
	  if(dowhat == 1){
	      //alert(str);
	      document.form_compare.ids.value = str;
	      document.form_compare.submit();
	  }else if(dowhat == 2){
	      v_pid = str;
	      myhome.getIdsfromcookie(calbackfavorites);
	  }
	}
} 

function addtofavorites(pid){
	v_pid = pid;
	myhome.getIdsfromcookie(calbackfavorites);
}	

function calbackfavorites(favoritesCookie){
	if(favoritesCookie != ''){
	    favoritesCookie = favoritesCookie + "," + v_pid;
        myhome.setIdstocookie(favoritesCookie);
    }else{
  	    myhome.setIdstocookie(v_pid);
    }
    window.location = hostname + "/myhome.html?method=showMyFavorites";		
}	

function sortsearchbyarea(s_name){
   var keyword = "Sort3:" + s_name;
   changekeyword(keyword);
}  

function brandsearchbyarea(f_name){
   var keyword = "Brand:" + f_name;
   changekeyword(keyword);
}   

function sortbrandsearch(f_name){
   var keyword = " Brand:" + f_name;
   changekeyword(keyword);
}        

function sortkeysearch(f_name){
   var keyword = "C3ID:" + f_name;
   changekeyword(keyword);
} 

function pricerange1search(f_name){
   var keyword = "PriceRange1:" + f_name;
   changekeyword(keyword);
}  

function pricerange2search(f_name){
   var keyword = "PriceRange2:" + f_name;
   changekeyword(keyword);
}    

//switch test style and picture style
function goswitch(pattern){
   //alert(window.location);
   try {   
     var olds = window.location.search;   
     var news = "";

     if(olds.length > 1) {
       olds = olds.substring(1,olds.length);
       var arrays = olds.split("&");
       for (var i = 0; i < arrays.length ; i++){
         if(arrays[i].indexOf("pattern=") < 0 && arrays[i].length > 1) {
           news += "&" + arrays[i];
         }
       }

       if(news.length > 1) {
         news = "?" + news.substring(1,news.length) + "&" + "pattern=" + pattern;
       }else {
         news = "?pattern=" + pattern;
       }
     }else {
       news = "?pattern=" + pattern;
     }
     window.location = window.location.pathname + news;
   }catch(e) {
     window.location = window.location.pathname + window.location.search;
   }
}

//Searching base on current results
function research(keyword){
   changekeyword(keyword);
} 

function changekeyword(keyword){
   try {   
     var olds = window.location.search;   
     var news = "";

     if(olds.length > 1) {
       olds = olds.substring(1,olds.length);
       var arrays = olds.split("&");
       for (var i = 0; i < arrays.length ; i++){
         if(arrays[i].indexOf("q=") < 0 && arrays[i].length > 1) {
           news += "&" + arrays[i];
         }
       }

       if(news.length > 1) {
         news = "?" + news.substring(1,news.length) + "&" + "q=" + getQueryString("q") + " " + keyword;
       }else {
         news = "?q=" + getQueryString("q") + " " + keyword;
       }
     }else {
       news = "?q=" + getQueryString("q") + " " + keyword;
     }
     window.location = window.location.pathname + news;
   }catch(e) {
     window.location = window.location.pathname + window.location.search;
   }
} 

function goDetails(promotionid){
	var v_url = "";
	v_url = hostname + "/promotions.html?promotion=" + promotionid;
	window.open(v_url);
}      

function gokeyword(keyword) {
   changekeyword(keyword);
}   

function getPidValue(counter){
	var fieldid="pid"+counter;
	return document.getElementById(fieldid).value;
} 

function getSKUValue(counter){
	var fieldid="sku"+counter;
	return document.getElementById(fieldid).value;
} 
  
function getImageUrl(pid,sku) {
	var img = new Image();
	var defaultimg = "http://platform.asiaec.com/procurement/default.gif";
		
	img.src = "http://productimg.asiaec.com/p"+(parseInt(pid/1000)+1)*1000+"/s_"+sku+".jpg";
	if(img.width != 0) return img.src;
	else return defaultimg;
}  

function todetailed(id,lang){
    var form = document.getElementById("productForm");
    form.id.value = id;
    form.locale.value=lang;
    //alert(lang);
    form.submit();
}       

function sortby(sequence){
   try {   
     var olds = window.location.search;   
     var news = "";

     if(olds.length > 1) {
       olds = olds.substring(1,olds.length);
       var arrays = olds.split("&");
       for (var i = 0; i < arrays.length ; i++){
         if(arrays[i].indexOf("dir=") < 0 && arrays[i].length > 1 &&
            arrays[i].indexOf("orderStyle=") < 0) {
             news += "&" + arrays[i];
         }
       }

       if(news.length > 1) {
           if(sequence == "supermk"){
               news = "?" + news.substring(1,news.length) + "&dir=" + sequence + "&orderStyle=Custom";
           }else{
               news = "?" + news.substring(1,news.length) + "&dir=" + sequence + "&orderStyle=Custom";
           }
           
       }else {
           if(sequence == "supermk"){
               news = "?dir=" + sequence + "&orderStyle=Custom";
           }else{
               news = "?dir=" + sequence + "&orderStyle=Custom";
           }
       }
     }else {
       if(sequence == "supermk"){
           news = "?dir=" + sequence + "&orderStyle=Custom";
       }else{
           news = "?dir=" + sequence + "&orderStyle=Custom";
       }
     }

     window.location = window.location.pathname + news;
   }catch(e) {
     window.location = window.location.pathname + window.location.search;
   }
}
 
//Use for search results list.
function bulkAdd(qrycount,area,language){
	var qty = "",pids = "";
	var map = {};
	var counter = 0;

    for(var i = 1;i <= qrycount;i++) {
	    qty = $("qty"+i).value;
	    pid = $("product"+i).value;
	     
	    if(qty != ""){	
	    	if(qty != "0" && !isNaN(qty) && isInt(qty)){	
	    	    map[pid]=qty;
	    	    counter++;
	    	}else{
	    		if(language == 0){
	    		    alert("The purchase quantity must be an integer number.");
	    		}else{
	    			alert("购买数量必须是正整数!!!");
	    		}
	    		document.getElementById("qty"+i).focus();
	    		return;
	    	}
	    }
	}
    if(counter == 0){
		if(language == 0){
		    alert("The purchase quantity is required.");
		}else{
			alert("请输入购买数量!!!");
		}
		return;
    }
	
	addProductsToCart(map,area);	
	showMessage();
}

function showMessage(){
	window.open("iframe/message.jsp",null,"height=220,width=320,status=no,toolbar=no,menubar=no,location=no,titlebar=no,scrollbars=no");
}
// =========================================================================
//                          Cookie functions 
// =========================================================================
/* This function is used to delete cookies */
function deleteCookie(name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    document.cookie = name + "=;expires="+ exp.toGMTString();
} 

/* This function is used to set cookies */
function writeCookie(name,value) {
    var argv = writeCookie.arguments;
    var argc = writeCookie.arguments.length;
    var expires = (argc  > 2) ? argv[2] : null;
    var path    = (argc  > 3) ? argv[3] : null;
    var domain  = (argc  > 4) ? argv[4] : null;
    var secure  = (argc  > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) 
        +((expires == null) ? "" : ("; expires="+ expires.toGMTString()))
        +((path    == null) ? "" : ("; path="   + path))
        +((domain  == null) ? "" : ("; domain=" + domain))
        +((secure  == true) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
    var pos = document.cookie.indexOf(name + "=");
    if(pos<0)
        return "";
    else{
        var endstr = document.cookie.indexOf(";", pos);
        if(endstr<0)
            return unescape(document.cookie.substr(pos+name.length+1));
        else
            return unescape(document.cookie.substring(pos+name.length+1,endstr));
    }
}	

function setCookie(name, value)
{
    var expires = new Date()
    expires.setFullYear(expires.getFullYear() + 1);
    writeCookie(name, value, expires, "/") ;
}

