//Update 2009-04-24 By Seika
//公共JS库（常用的JS库）
//Start


//合并多个复选项的Value值
function join_checkbox(id)
{
	var idarray = new Array();//定义一个数组
	if($('#'+id).length>0)
	{
		var cv = getid(id).getElementsByTagName("input");
		//var cv = $('#'+id).children('input');
	}
	else
	{
		var cv = $("input");
	}
	var m = 0;
	for(var i=0; i<cv.length; i++)
	{
		if(cv[i].type.toLowerCase() == "checkbox")
		{
			if(cv[i].checked)
			{
				idarray[m] = cv[i].value;
				m++;
			}
		}
	}
	var id = idarray.join(",");
	return id;
}

//对 document.getElementById 进行简写
//为兼容 jQuery 这里不使用 $而是使用 getid
function getid(val)
{
	return document.getElementById(val);
}

//多长时间执行一个脚本
function evaljs(js,time)
{
	if(!time || time == "undefined")
	{
		time = 0;
	}
	time = parseInt(time);
	if(time < 1)
	{
		eval(js);
	}
	else
	{
		if(time < 5)
		{
			time = time*1000;
		}
		window.setTimeout(js,time);
	}
}

function totobay_right()
{
	return "<img src='includes/templates/totobay/totobayimages/check_com.gif' border='0' align='absmiddle'>";
}
function totobay_wrong()
{
	return "<img src='includes/templates/totobay/totobayimages/check_not.gif' border='0' align='absmiddle'>";
}

//插入提示语言 /wenda
function totobay_wrong_text(str,width,message){
	var wrong_message_img="<br><img ";
	
	if (width==true){wrong_message_img+="style=\"margin-left:115px\"";}
	wrong_message_img+=" src='includes/templates/totobay/totobayimages/register_er.gif' border='0' align='absmiddle'> ";
	
	if (str=='password'){
		wrong_message_img+= "Sorry, Password information is required.";	
	} else if(str=='mail'){
		wrong_message_img+= "Sorry, E-mail address information is required.";
	} else if (str=='firstname'){
		wrong_message_img+= "Please kindly provide English Name.";
	} else if (str=='lastname'){
		wrong_message_img+= "Sorry, lastname information is required.";
	} else if (str=='password_chk'){
				if (message!='undefined' && message!=''){
			      wrong_message_img+= "Please enter a value greater than four.";
			}else{
				  wrong_message_img+= "Sorry, Password information is required.";
				}			
	} else if (str=='passwordc_chk'){
		wrong_message_img+= "The two passwords you entered don’t match.";
	} else if (str=='email_address'){
		wrong_message_img+= "Please provide a valid e-mail addresses.";
	} else if (str=='telephone'){
		wrong_message_img+= "Please provide a valid telephone.";
	} else if (str=='exists'){
		wrong_message_img+= "Sorry, Our system already has a record of email.";
	} else {
		wrong_message_img+= str;
	}
	return wrong_message_img;			
}

//按需加载JS文件以加快浏览速度
function jsload(url)
{
	var this_script = document.getElementsByTagName("script");
	for(i=0;i<this_script.length;i++)
	{
		if(this_script[i].src && this_script[i].src.indexOf(url)!=-1)
		{
			return true;
		}
	}
	var load_script = document.createElement("script");
	load_script.type = "text/javascript";
	load_script.src = url;
	var head=document.getElementsByTagName("head")[0];
	head.appendChild(load_script);
}

/*取得当前网址所在的目录，使用UrlRewrite后的网址可能支持会有错误*/
function GetSiteUrl()
{
	var siteurl = window.location.href;
	//去除?后面的相关参数
	siteurl = siteurl.substring(7,siteurl.indexOf("?"));
	//切分 / 符号
	var sitearray = siteurl.split("/");
	var newurl = "http://";
	for(var i=0;i<(sitearray.length-1);i++)
	{
		newurl += sitearray[i]+"/";
	}
	return newurl;
}

/*通过Ajax来获取城市菜单，如果城市下垃菜单为空，则使用input框供其填写*/
/*相关参数说明
 *countryid: 国家ID号
 *selectid: 下拉菜单的省ID号
 *inputid: 如果selectid对应的select没有可选择项，则使用该项补充
 *innerid: 插入指定的ID位置
 *extmsg: 数据补充信息
*/
function update_state(countryid,selectid,inputid,innerid)
{
	var url = "index.php?main_page=ajax&action=getstate&returnstring=true";
	if(!countryid || countryid == "undefined")
	{
		return false;
	}
	url += "&countryid="+countryid;
	if(!selectid || selectid == "undefined")
	{
		selectid = "zone_id";
	}
	url += "&selectid="+selectid;
	//判断是否有默认值
	var get_selectid = getid(selectid);
	if(get_selectid)
	{
		var selectid_val = get_selectid.value;
		if(selectid_val)
		{
			url += "&selectid_val="+selectid_val;
		}
	}
	if(!inputid || inputid == "undefined")
	{
		inputid = "state";
	}
	url += "&inputid="+inputid;
	var get_inputid = getid(inputid);
	if(get_inputid)
	{
		var inputid_val = get_inputid.value;
		if(inputid_val)
		{
			url += "&inputid_val="+inputid_val;
		}
	}
	var msg = get_ajax(url);
	if(!msg)
	{
		alert("Error: Get city failed!");
		return false;
	}
	var chkerror = msg.toLowerCase();
	if(chkerror.substring(0,5) == "error")
	{
		alert(msg);
		return false;
	}
	if(!innerid || innerid == "undefined")
	{
		innerid = "state_innerHTML";
	}
	getid(innerid).innerHTML = msg;
	return true;
}
//utf8 encode
function EncodeUtf8(s1)
{
	var s = escape(s1);
	var sa = s.split("%");
	var retV ="";
	if(sa[0] != "")
	{
		retV = sa[0];
	}
	for(var i = 1; i < sa.length; i ++)
	{
		if(sa[i].substring(0,1) == "u")
		{
			retV += Hex2Utf8(Str2Hex(sa[i].substring(1,5)));
			if(sa[i].length>5)
			{
				retV += sa[i].substring(5);
			}
		}
		else
		{
			retV += "%" + sa[i];
		}
	}
	return retV;
}
function Str2Hex(s)
{
	var c = "";
	var n;
	var ss = "0123456789ABCDEF";
	var digS = "";
	for(var i = 0; i < s.length; i ++)
	{
		c = s.charAt(i);
		n = ss.indexOf(c);
		digS += Dec2Dig(eval(n));
	}
	return digS;
}
function Dec2Dig(n1)
{
	var s = "";
	var n2 = 0;
	for(var i = 0; i < 4; i++)
	{
		n2 = Math.pow(2,3 - i);
		if(n1 >= n2)
		{
			s += '1';
			n1 = n1 - n2;
		}
		else
		{
			s += '0';
		}
	}
	return s;
}
function Dig2Dec(s)
{
	var retV = 0;
	if(s.length == 4)
	{
		for(var i = 0; i < 4; i ++)
		{
			retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
		}
		return retV;
	}
	return -1;
}
function Hex2Utf8(s)
{
	var retS = "";
	var tempS = "";
	var ss = "";
	if(s.length == 16)
	{
		tempS = "1110" + s.substring(0, 4);
		tempS += "10" +  s.substring(4, 10);
		tempS += "10" + s.substring(10,16);
		var sss = "0123456789ABCDEF";
		for(var i = 0; i < 3; i ++)
		{
			retS += "%";
			ss = tempS.substring(i * 8, (eval(i)+1)*8);
			retS += sss.charAt(Dig2Dec(ss.substring(0,4)));
			retS += sss.charAt(Dig2Dec(ss.substring(4,8)));
		}
		return retS;
	}
	return "";
}
//Create Ajax
function add_ajax()
{
	var xmlhttp=null;
	try
	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			xmlhttp=null;
		}
	}
	if(!xmlhttp && typeof XMLHttpRequest != "undefined")
	{
		xmlhttp=new XMLHttpRequest();
	}
    return xmlhttp;
}

//Using Ajax
function get_ajax(url)
{
	var xmlhttp = add_ajax();
	var randname = "rand"+Math.floor(Math.random()*10000);
	var randnum = Math.floor(Math.random()*10000);
	if(url.indexOf("?") != -1)
	{
		url += "&"+randname+"="+randnum;
	}
	else
	{
		url += "?"+randname+"="+randnum;
	}
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	if(xmlhttp.readyState==3)
	{
		alert("Loading...");
	}
	else if(xmlhttp.readyState == 4)
	{
		var result = xmlhttp.responseText;
		if(result)
		{
			return result;
		}
		else
		{
			alert("Please refresh later!");
		}
	}
}
function get_cookie(name)
{
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
			{
				end = document.cookie.length;
			}
			cookieValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}

function set_cookie(cookieName,cookieValue,DayValue)
{
	var expire = "";
	var day_value=1;
	if(DayValue!=null)
	{
		day_value=DayValue;
	}
    expire = new Date((new Date()).getTime() + day_value * 86400000);
    expire = "; expires=" + expire.toGMTString();
	document.cookie = cookieName + "=" + escape(cookieValue) +";path=/"+ expire;
}

function del_cookie(cookieName)
{ 
	var expire = "";
    expire = new Date((new Date()).getTime() - 1 );
    expire = "; expires=" + expire.toGMTString();
	document.cookie = cookieName + "=" + escape("") +";path=/"+ expire;
}

// init 

var baseURL="http://www.totobay.com/";
var lang = 
{
	error_required_fields: 'You did not complete all of the required fields or input data is incorrect.',
	error_no_items_selected: 'No items selected! At least one checkbox must be ticked off to perform this action.',
	delete_confirmation: 'Are you sure you want to delete the selected items?',
	text_out_of_stock: 'Out of stock',
	items: 'item(s)',
	text_required_group_product: 'Please select a product for the required group [group_name]',
	error_email_invalid: 'The e-mail address is invalid. Please check.',
	phone_number_invalid: 'The phone number is invalid. Please check.'
};
function $id(a){if(document.getElementById(a));return document.getElementById(a);}

//删除个人中心 shipping address
function delete_shipping_address(addressbookid)
{
	var delurl = "index.php?main_page=ajax&action=delete_shipping_address&address_book_id="+addressbookid;
	
	var msg = get_ajax(delurl);
	if(msg == "ok")
	{
		window.location.href = window.location.href;
		return true;
	}
	else
	{
		alert("Delete Failed.");
		return false;
	}
}


//Start Edit by the Lisa
//Add To Wishlist
//单个产品添加到帐户收藏页操作


function addtowishlist(pid)
{
	if(!pid || pid == "undefined")
	{
		alert("Error: It's forbidden.");
		return false;
	}
	
	var ajaxurl = "index.php?main_page=ajax&action=addwishlist";
	ajaxurl += "&products_id="+pid;
	
	fn_ajax_http_request(ajaxurl);
}




//删除购物车里的某个产品信息
function delwishlist(productid)
{
	if(!productid || productid == "undefined")
	{
		refreshurl = false;
	}
	var delurl = "index.php?main_page=ajax&action=delwishlist&products_id="+productid;
	var msg = get_ajax(delurl);
	if(msg == "ok")
	{
		window.location.href = window.location.href;
	}
	else
	{
		if(!msg) msg == "Error!";
		alert(msg);
	}
}

//End Edit by the Lisa

function popupwin(url,name,width,height,options){if($.browser.msie&&arguments[5]!=1){var win=window.showModelessDialog(url,window,"status:false;dialogWidth:"+(width)+"px;dialogHeight:"+(height+30)+"px;edge:Raised; help: 0; resizable: 0; status: 0;scroll:0;");return false;}
xposition=0;yposition=0;if((parseInt(navigator.appVersion)>=4)){xposition=(screen.width-width)/2;yposition=(screen.height-height)/2;
theproperty="width="+width+","+"height="+height+","+"screenx="+xposition+","+"screeny="+yposition+","+"left="+xposition+","+"top="+yposition+","+options;
var win=window.open(url,name,theproperty);
win.focus();}
return false;}

//event

var vmenu = $("#vmenu");
if(vmenu.length>0){
	$("a",vmenu).focus(function(){
	  this.blur();
	}); 
}
var logo_a = $("#logo a");
if(logo_a.length>0){
	logo_a.focus(function(){
		if(this.blur)
	  this.blur();
	}); 
}

var input_key = $("input[name='keyword']");
if(input_key.length>0){
	input_key.focus(function(){
		if (this.value == 'Enter search keywords here') this.value = '';
	}); 
	input_key.blur(function(){
		if (this.value == '') this.value = 'Enter search keywords here';
	});
}
var all = $("#all");
	if(all.length>0){
	all.hover(
	  function () {
		$(this).removeClass("menu2");
		$(this).addClass("menu1");
	  },
	  function () {
		$(this).removeClass("menu1");
		$(this).addClass("menu2");
	  }
	); 
}
