/*
cosimage.js
cart.js
im.js
*/

function delById(id){
	if(confirm("Are you sure to delete it ?")){
		location.replace("?op=delById&id="+id+"&ref="+escape(location.href));
	}
}

/* cosimage.js */
var theDiv,t1,t2;
var imgOn=false;
function initDiv(){
	if(!theDiv){
		theDiv=document.getElementById("imgDiv");
		if(!theDiv){
		theDiv=document.createElement('div');
		theDiv=document.body.appendChild(theDiv);
		theDiv.id="imgDiv";
		}
		with(theDiv.style){
			position="absolute"; visibility="hidden"; padding = "5px";
			backgroundColor = "#ffffff"; borderWidth = "1px"; borderStyle = "ridge";
		}
	}
	document.onmousemove=trackMouse;
}
function showImg(evt,theImg){
	if(!theDiv)return;
	if(t1)clearTimeout(t1);
	if(t2)clearTimeout(t2);
	if(evt){
		t1=setTimeout("showImg(null,'"+theImg+"')",1000);
		return;
	}
	imgOn=true;
	theDiv.innerHTML='<img src="'+theImg+'" border="1">';
	t1=setTimeout("theDiv.style.visibility='visible'",100);
}
function trackMouse(evt) {
	if(!imgOn)return;
	if(!theDiv)return;
	var pageXY = evt && typeof(evt.pageX)!='undefined';
	var mouseX = pageXY ? evt.pageX: window.event.clientX + document.body.scrollLeft;
	var mouseY = pageXY ? evt.pageY: window.event.clientY + document.body.scrollTop;
	var tpWd = (theDiv.clientWidth || theDiv.offsetWidth);
	var tpHt = (theDiv.clientHeight || theDiv.offsetHeight);
	var winWd = window.innerWidth ? window.innerWidth-20+window.pageXOffset: document.body.clientWidth+document.body.scrollLeft;
	var winHt = window.innerHeight ? window.innerHeight-20+window.pageYOffset: document.body.clientHeight+document.body.scrollTop;
	theDiv.style.left = ((mouseX+20+tpWd)>winWd ? mouseX-(tpWd+20) : mouseX+20)+"px";
	theDiv.style.top = ((mouseY+12+tpHt)>winHt ? winHt-(tpHt+12) : mouseY+12)+"px";
}
function hideImg(){
	if(!theDiv)return;
	if(t1)clearTimeout(t1);
	if(t2)clearTimeout(t2);
	t2=setTimeout("theDiv.style.visibility='hidden'",100);
	imgOn=false;
}

/* cart.js */
function sizerowEvt(){
	var evt = getEvent();
	var evtType = evt.type;
	var td = evt.srcElement;
	var size=td.getAttribute("i");
	if(evtType=="mouseover" && td.className=="sizeblur"){
		td.className="sizehover";
		if(td.setCapture){
			td.setCapture();
		}else if(window.captureEvents){
			//window.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		}
	}
	if(evtType=="mouseout"){
		if(td.className=="sizehover"){
			td.className="sizeblur";
		}
		if(td.releaseCapture){
			td.releaseCapture();
		}
	}
	if(evtType=="click" && td.className=="sizehover"){
		var tds = document.getElementById("sizerows").rows;
		for(var i=0;i<tds.length;i++){
			tds[i].cells[0].className = "sizeblur";
		}
		td.className="sizefocus";
		var prices = dicSizePrice[size] || [0,0,0];
		document.getElementById("priceA").value=prices[0];
		document.getElementById("priceB").value=prices[1];
		document.getElementById("priceC").value=prices[2];
	}
}
function addToCartEvt(){
	var tds = document.getElementById("sizerows").rows;
	var size;
	for(var i=0;i<tds.length;i++){
		if(tds[i].cells[0].className == "sizefocus"){
			size=tds[i].cells[0].getAttribute("i");
			break;
		}
	}
	if(!size)return false;
	var f=document.forms["formCart"];
	var es=f.elements;
	es["size"].value=size;
	var earr = [es["quantityA"],es["quantityB"],es["quantityC"]];
	var varr = [parseInt(earr[0].value), parseInt(earr[1].value), parseInt(earr[2].value)];
	earr[0].value = (isNaN(varr[0]) || varr[0]<0) ? "0" : varr[0];
	earr[1].value = (isNaN(varr[1]) || varr[1]<0) ? "0" : varr[1];
	earr[2].value = (isNaN(varr[2]) || varr[2]<0) ? "0" : varr[2];
	if(earr[0].value=="0" && earr[1].value=="0" && earr[2].value=="0"){
		alert("Please input quantity.");
		return false;
	}
	return true;
}
function getEvent(){     //获取事件对象，同时兼容ie和ff的写法
	if(window.event)    event.target=event.srcElement;
	if(window.event)    return window.event;
	var func=getEvent.caller;
	while(func!=null){
		var arg0=func.arguments[0];
		if(arg0){
			if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
				|| (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){
				arg0.srcElement=arg0.target;
				return arg0;
			 }
		 }
		 func=func.caller;
	}
	return null;
}
function addToCartEvt2(){ //wholesale
  try{
	var f=document.forms["formCart"];
	var es=f.elements;
	var $=function(id){return document.getElementById(id);}
	if(!es["quantity"].value){
		alert("Please input quantity.");
		return false;
	}
	if(es["quantity"].value.search(/^[1-9]\d?\d?$/)<0){
		alert("Quantity is invalid.");
		return false;
	}
	if($("sizeB").checked){
		if(!$("sizeW").value || !$("sizeH").value){
			alert("Please input size.");
			return false;
		}
		if($("sizeW").value.search(/^[1-9]\d?\d?$/)<0 || $("sizeH").value.search(/^[1-9]\d?\d?$/)<0) {
			alert("Size is invalid.");
			return false;
		}
	}
	es["size"].value = $("sizeA").checked ? $("sizeSelect").value : ($("sizeW").value +"X"+ $("sizeH").value +" "+ $("sizeUnit").value);
	var iQuantity=parseInt(es["quantity"].value);
	es["quantityA"].value=$("QualityA").checked?iQuantity:'0';
	es["quantityB"].value=$("QualityB").checked?iQuantity:'0';
	es["quantityC"].value=$("QualityC").checked?iQuantity:'0';
	return true;
  }catch(js_err){
	alert(js_err.description);
	return false;
  }
}

/* im.js */
/* for msn */
function cxp_MSN_startConversation(MSN_IM_ID)
{
	var conversationUrl="http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=" +MSN_IM_ID+ "@apps.messenger.live.com&mkt=en-GB";
	window.open(conversationUrl, '_blank', 'height=400,width=400');
}
/* for skype */
/* http://download.skype.com/share/skypebuttons/js/skypeCheck.js */
function skypeCheck() {
	var activex = typeof(ActiveXObject)=='object';
	var CantDetect = ((navigator.userAgent.indexOf('Safari')!=-1) || (navigator.userAgent.indexOf('Opera')!=-1));
	if(typeof(detected) == "undefined" && activex && window.execScript) {
		window.execScript('Function isSkypeInstalled() : on error resume next : Set oSkype = CreateObject("Skype.Detection") : isSkypeInstalled = IsObject(oSkype) : Set oSkype = nothing : End Function','vbscript');
	}
	window.detected = true;
	if(CantDetect) {
		return true;
	} else if(!activex) {
		var skypeMime = navigator.mimeTypes["application/x-skype"];
		return typeof(skypeMime)=="object";
	} else {
		if((typeof isSkypeInstalled)=='unknown' && isSkypeInstalled()) {
			return true;
		}
	}
	return false;
}
/* for tq */
function checkTQ(){
	var online=(new Date()).getHours()>=8;
	/* var q=escape("http://"+location.host+"/");
	var q=escape(document.referrer);
	'http://float2006.tq.cn/static_card.js?ltype=100&name=U&uin=8469916&utf8=1&onlinepic=page/livechat_online2.gif&offlinepic=page/livechat_offline2.gif&where='+q+' */
	var div=document.getElementById("im_tq_div");
	var o=document.getElementById("tqImgElement");
	if(div){
		if(!o)div.innerHTML='<IMG id="tqImgElement" src="page/MsSpacer.gif" width="162" height="48" border="0" />';
		o=document.getElementById("tqImgElement");
		o.onclick=function(){window.open((online?'http://webchat.tq.cn/sendmain.jsp':'http://qtt.tq.cn/leavemsg.do')+'?uin=8469916&ltype=100&ispaymoney=1&page=http%3A//'+location.host+'/','tqmsg','width=595,height=437,location=no,resizable=1,scrollbars=0,status=no,toolbar=no,menu=no,top=100,left=200');return false;}
		o.src=online?"page/livechat_online2.gif":"page/livechat_offline2.gif";
		o.title="Click here to give me a note";
		o.style.cursor='pointer';
	}
}


/* all execute here */
if(document.addEventListener){
	document.addEventListener("DOMContentLoaded", checkTQ, false);
	document.addEventListener("DOMContentLoaded", initDiv, false);
}else if(document.attachEvent){
	document.attachEvent('onreadystatechange',function(){if(document.readyState=="complete"){
		checkTQ();
		initDiv();
		}}
	);
}