﻿var Cookie = {
	set: function(name,value,seconds){
		if(seconds){
			var d = new Date();
			d.setTime(d.getTime() + (seconds * 1000));
			var expiry = '; expires=' + d.toGMTString();
		}else
			var expiry = '';
		document.cookie = name + "=" + value + expiry + "; path=/";
	},
	get: function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++){
			var c = ca[i];
			while(c.charAt(0) == ' ')
				c = c.substring(1,c.length);
			if(c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	unset: function(name){
		Cookie.set(name,'',-1);
	},
	show: function(){
		alert(unescape(document.cookie));
	}
};

//dimensions of Array
Array.prototype.dimensions  =  function(){  
  var	result = 0;  
  for (var i=0;i<this.length; i++)
  	if(this[i].length)  
  		result = Math.max(result,this[i].dimensions()); 
  return   ++result;  
}

function DoVesselAjax(AURL, AVessel){
	var myAjax = new Ajax.Updater(
		AVessel, 
		AURL, {
			method: 'get' 
		});
}	
function DoFunctionAjax(AURL, AFunction){
	var myAjax = new Ajax.Request(
		AURL, {
			method: 'get', 
			onComplete: AFunction
		});		
}	
function DoPostAjax(AURL, AFormID, AFunction){ 
  var myAjax = new Ajax.Request(
      AURL,{
				method: 'post', 
				parameters: $(AFormID).serialize(), 
				onComplete: AFunction}
  );
}
function RandomParameter(AOnlyAParameter){
	if (AOnlyAParameter)
		return 'tag=' + Math.random()* 1000000000;
	else
		return '&tag=' + Math.random()* 1000000000;
}
//recommend
function recommend_item(ItemNos){
	var ARootPath = arguments.length == 1 ? '../' : arguments[1];
	DoFunctionAjax(
		ARootPath + 'Products/UpdateRecommended.php?ItemNo=' + ItemNos + RandomParameter(),
		function(A){
			alert(A.responseText);
		}
	)
}

//Search for item
function search_item(AKeywords, AURL, ARootPath){
	if(!AURL.blank()){
		if(AKeywords.blank()){
			alert('Please enter keywords what you would like to search.');
			return false;
		}
		//清理用户的关键字		
		AKeywords = AKeywords.gsub(/\s{2,}/, ' ');
		location.href = ARootPath + AURL + AKeywords + RandomParameter();
	}else{
		if(arguments.length == 1)
			ARootPath = '../';
		DoFunctionAjax(ARootPath + "Products/ExistProductByItemNo.php?ItemNo=" + AKeywords + RandomParameter(),
			function(A){
				if (A.responseText == 'Failed')
					alert("Sorry, the ItemNo is not exist.");
				else {
					sShowPath = ARootPath + "Products/ProductShow.php?ItemNo=" + AKeywords;
					location.href = sShowPath;					
				}
			})
	}
}

function show_sub(i){
	var oEID = $('sub_' + i);
	if (oEID.visible()) {
  	oEID.hide();
		return;
  }
	$$('.submenu').each(function(A){
		if (A.visible())
			A.hide();
	});
	$('sub_' + i).show();
	return;
}

/*
JS截取字符串N个字符
标准函数：
Mid( 字符串表达式 ,start[,length])
取字符串中的几个字。 start 为从第几个字取起， length 为取几个字， 若略 length 则从 start 取到最右底。由Len 函数可得知字符串的长度。
例子： Mid("abc123",2,3) 
结果： bc1*/
//截取字符串N个字符,自定义函数
function GetStringNum(str,len){
	 var strlen = 0; 
	 var s = "";
	 for(var i = 0;i < str.length;i++){
			if(str.charCodeAt(i) > 128)
			  strlen += 2;
			else 
			  strlen++;
			s += str.charAt(i);
			if(strlen >= len) 
			return s + "......";
	 }
     return s;
}
//
//下列两个函数实现,单击表单文本框,弹出提示信息
function MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
//————end
//解决函数toFixed（）在IE低于V5.5版本下失效的情况
	Number.prototype.toFixed = function(Num)
	{
		if (Num == null) Num = 0;
		var powValue = Math.pow(10,Num);
		var xValue = this * powValue + 0.5;
		return (Math.floor(xValue) / powValue);
	}

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: block;}\n')
document.write('</style>\n')
}
function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("div"); //DynamicDrive.com change
		if(el.style.display != "none"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				ar[i].style.display = "block";
			}
			el.style.display = "none";
		}else{
			el.style.display = "block";
		}
	}
}

/////
function Check_Key_Data()
{
	if((document.getElementById("keywords").value=="") || (document.getElementById("keywords").value=="Input keywords"))
	{
		window.alert("Please Input Keywords");
		document.getElementById("keywords").value="";
		document.getElementById("keywords").focus();
		return false;
	}
	return true;
}
function Click_Key()
{
   document.getElementById("keywords").value="";
}

//chzy 2009年4月20日18时46分37秒
function chk_chg(){
	if (arguments.length === 0)
		return;
	val = arguments[0];
	if (arguments.length < 2)
		ARootPath = '../';
	else
		ARootPath = arguments[1];
	
	oCheckes = $$('input[name="chk_pro"]');
	aItemNo = new Array();
	oCheckes.each(function(AE){
		if (AE.checked)
			aItemNo.push(AE.readAttribute('title'));
	});
	oCheckes = null;
	if (aItemNo.length === 0) {
  	alert('Please select one or more product(s)!');
  	return;
  }
	if(val == 1){//Inquiry
		j = 0;
		proid = 0;			
   	if(aItemNo.length > 20){
			alert('Forbid to: Inquiry more than 20 items a time.');
			return;
		}else{
			var request_txt = "";
			request_txt = aItemNo.join(',');
			location.href = ARootPath + 'Inquiries/index.php?ItemNo='+request_txt;
   }
	}else if(val == 2){ //InquiryBasket.php
		var InquiriyItemNos = Cookie.get('InquiriyItemNos');
		if(InquiriyItemNos != null){
			var tmp_cookie_arr = $A(InquiriyItemNos.split(','));
			var arr = aItemNo.concat(tmp_cookie_arr);
			arr = arr.uniq();
		}else{
			var arr= aItemNo;
		}
		Cookie.set('InquiriyItemNos', arr.join(","));
		$("inquiry_basket_number").update(arr.length);
		if(confirm('Success, Would you like to inquiry basket'))
			location.href= ARootPath + 'Inquiries/InquiryBasket.php';
		else
			$("inquiry_basket_number").scrollTo();
	}else if (val == 3){
		DoFunctionAjax(ARootPath + 'Products/UpdateRecommended.php?ItemNo=' + aItemNo.join(","),
			function(A){
				alert(A.responseText);
			});	
	}
}

function chk_all(type){
	oCheckes = $$('input[name="chk_pro"]');
	switch (type){
		case 1://Unselect
			oCheckes.each(function(AE){
				AE.checked = false;
			});
			break;
		case 2: //Invert Select
			oCheckes.each(function(AE){
				AE.checked = ! AE.checked;
			});		
		default: //Select All
			oCheckes.each(function(AE){
				AE.checked = true;
			});
	}
}


//pagination 
function chg_order(order,sort,url){
	if(order && sort && url){
		location.href = url + "?order="+order+"&sort="+sort;
	}
}

function disp_accordian(){
	try{
			new Accordian('basic-accordian',8,'header_highlight');
		}catch(e) {
			
		}
}

Event.observe(window, "load", function(event){
	//按下回车，激发按纽的点击事件　chzy　2009年4月28日11时55分17秒
	if($('PageGoText') != null)
		Event.observe('PageGoText', 'keyup', function(event){
			if (event.keyCode == 13) {
				$('PageGoButton').click();
			}
		});
	//给左侧菜单挂接事件
	$oMainClasses = $$('div.MainClass');
	if($oMainClasses != null){
		$oMainClasses.invoke('observe', 'mouseover', function(event){			
			var oE = event.element();
			if (oE.hasClassName('MainClass'))
				oE.removeClassName('MainClass');
			else
				return false;
			oE.addClassName('MainClassFocus');
		});
		$oMainClasses.invoke('observe', 'mouseout', function(event){
			var oE = event.element();
			if (oE.hasClassName('MainClassFocus'))
				oE.removeClassName('MainClassFocus');
			else
				return false;
			oE.addClassName('MainClass');
		});
	}
	//给查询蓝赋值　chzy 2009年5月4日21时43分39秒
	var InquiriyItemNos = Cookie.get('InquiriyItemNos');
	if (InquiriyItemNos != null) {
  	var aItemNo = $A(InquiriyItemNos.split(','));
		var oIBN = $("inquiry_basket_number");
		if((aItemNo != null) && (oIBN != null))
  		oIBN.update(aItemNo.length);
  }
});


