// JavaScript Document
function $(id){
	return document.getElementById(id);
}

function addCookie(url,title) {
	if (document.all) {
		window.external.addFavorite(url, title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	}
}

function setHomepage(url) {
	if (document.all) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(url);

	}
	else if (window.sidebar) {
		if (window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			} catch (e) {
				alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', url);
	}
} 


//获取一个元素（通过 id 属性指定）在页面中的绝对位置
function getElementPos(elementId) {
	var ua = navigator.userAgent.toLowerCase();
	var isOpera = (ua.indexOf('opera') != -1);
	// not opera spoof
	var isIE = (ua.indexOf('msie') != -1 && !isOpera);
	var el = document.getElementById(elementId);
	if(el.parentNode === null || el.style.display == 'none') {
		return false;
	}
	var parent = null;
	var pos = [];    
	var box;
    //IE
	if(el.getBoundingClientRect) {         
		box = el.getBoundingClientRect();
		var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
		return {x:box.left + scrollLeft, y:box.top + scrollTop};
	}
	// gecko   
	else if(document.getBoxObjectFor) {
		box = document.getBoxObjectFor(el);
		var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
		var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;
		pos = [box.x - borderLeft, box.y - borderTop];
	}
   // safari & opera   
	else {
		pos = [el.offsetLeft, el.offsetTop]; 
		parent = el.offsetParent;    
		if (parent != el) {
			while (parent) { 
				pos[0] += parent.offsetLeft;
				pos[1] += parent.offsetTop;
				parent = parent.offsetParent;
			}
		}
		if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) {
			pos[0] -= document.body.offsetLeft;
			pos[1] -= document.body.offsetTop;        
		}
	}
	if (el.parentNode) {
		parent = el.parentNode;
	} else {
		parent = null;
	}
	// account for any scrolled ancestors
	while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { 
		pos[0] -= parent.scrollLeft;
		pos[1] -= parent.scrollTop;
		if (parent.parentNode) {
			parent = parent.parentNode;
		} else {
			parent = null;
		}
	}
	return {x:pos[0], y:pos[1]};
}

/**
 * 添加优惠券到收藏夹，或从收藏夹中删除优惠券
 */
Array.prototype.Contains=function(obj){
   if(null==obj){
	   return;
	}
   for(var i=0,n=0;i<this.length;i++) {
    if(this[i] ==obj ) {
     return true;
    }
   }
   return false;
}
var existCouponIds = new Array();
var timeCounter4Panel = 0;
var startPanelTimeCount = true;
var panelInterval = null;


function addToFavo (event,resourceId,id,show,index){
	timeCounter4Panel = 0;
	startPanelTimeCount = true;
	if(panelInterval != null)
		clearInterval(panelInterval);
	
	var body = document.documentElement;
	var width = 294,height=164;
	var sheight = body.scrollTop+body.clientHeight-height;
	
	var e = event.srcElement||event.target;
	var act = "add";
	if(e.checked == false)
		act = "remove";
	var url = "http://"+location.host+"/addToFavorite.do?id="+id+"&resourceId="+resourceId+"&action="+act+"&ran="+Math.random();
	var x = doGet(url);
	//添加收藏才出现提示面板
	//if(act == "add"){
	if(show){
		var p = getElementPos(e.id);
		
		var div = document.getElementById("paenlBoxc") ;
		if(typeof(div) == 'undefined' || div == null){
			div = document.createElement("DIV");
			div.id = "paenlBoxc";
			div.onmouseover = function(){
				startPanelTimeCount = false;
			}
			div.onmouseout = function () {
				startPanelTimeCount = true;
			}
			document.body.appendChild(div);
		}
		div.innerHTML = x;
		div.style.position = "absolute";
		div.className = "paenlBoxc";
		div.style.right = "0px";
		div.style.top = sheight+"px";
		div.style.display = '';	
		panelInterval = setInterval(function(){computePos();},50);
	}
	
	var spanId_xx = "span_addFavo_" + id;
	if (index){
		spanId_xx = spanId_xx + "_" + index;
	}
	if(act === 'add')
		document.getElementById(spanId_xx).className = "add on";
	else
		document.getElementById(spanId_xx).className = "add";
	//当添加临时收藏夹失败，如达到上限，系统错误等，则需要取消勾选框
	var addFavoCheckboxId = document.getElementById("addFavoCheckboxId");
	//alert(addFavoCheckboxId.value);
	if(addFavoCheckboxId){
		var ckbox = addFavoCheckboxId.value;
		if(ckbox){
			e.checked = false;
			document.getElementById(spanId_xx).className = "add";
		}
	}
}




function addToPrintFavo (event,resourceId,id,show){
	timeCounter4Panel = 0;
	startPanelTimeCount = true;
	if(panelInterval != null)
		clearInterval(panelInterval);
	
	var body = document.documentElement;
	var width = 294,height=164;
	var sheight = body.scrollTop+body.clientHeight-height;
	
	var e = event.srcElement||event.target;
	var act = "add";
	if(e.checked == false)
		act = "remove";
	var url = "http://"+location.host+"/addToPrintFavorite.do?id="+id+"&resourceId="+resourceId+"&action="+act+"&ran="+Math.random();
	var x = doGet(url);
	//添加收藏才出现提示面板
	//if(act == "add"){
	if(show){
		var p = getElementPos(e.id);
		
		var div = document.getElementById("paenlBoxc") ;
		if(typeof(div) == 'undefined' || div == null){
			div = document.createElement("DIV");
			div.id = "paenlBoxc";
			div.onmouseover = function(){
				startPanelTimeCount = false;
			}
			div.onmouseout = function () {
				startPanelTimeCount = true;
			}
			document.body.appendChild(div);
		}
		div.innerHTML = x;
		div.style.position = "absolute";
		div.className = "paenlBoxc";
		div.style.right = "0px";
		div.style.top = sheight+"px";
		div.style.display = '';	
		panelInterval = setInterval(function(){computePos();},50);
	}
	
	if(act === 'add')
		document.getElementById("span_addPrintFavo_"+id).className = "download don";
	else
		document.getElementById("span_addPrintFavo_"+id).className = "download";
	//当添加临时收藏夹失败，如达到上限，系统错误等，则需要取消勾选框
	var addFavoCheckboxId = document.getElementById("addFavoCheckboxId");
	//alert(addFavoCheckboxId.value);
	if(addFavoCheckboxId){
		var ckbox = addFavoCheckboxId.value;
		if(ckbox){
			e.checked = false;
			document.getElementById("span_addPrintFavo_"+id).className = "download don";
		}
	}
}
function addToPrintFavo2(event,resourceId,id,show){
	timeCounter4Panel = 0;
	startPanelTimeCount = true;
	if(panelInterval != null)
		clearInterval(panelInterval);
	
	var body = document.documentElement;
	var width = 294,height=164;
	var sheight = body.scrollTop+body.clientHeight-height;
	
	var e = event.srcElement||event.target;
	var act = "add";
	if(e.checked == false)
		act = "remove";
	var url = "http://"+location.host+"/addToPrintFavorite.do?id="+id+"&resourceId="+resourceId+"&action="+act+"&ran="+Math.random();
	var x = doGet(url);
	//添加收藏才出现提示面板
	//if(act == "add"){
	if(show){
		var p = getElementPos(e.id);
		
		var div = document.getElementById("paenlBoxc") ;
		if(typeof(div) == 'undefined' || div == null){
			div = document.createElement("DIV");
			div.id = "paenlBoxc";
			div.onmouseover = function(){
				startPanelTimeCount = false;
			}
			div.onmouseout = function () {
				startPanelTimeCount = true;
			}
			document.body.appendChild(div);
		}
		div.innerHTML = x;
		div.style.position = "absolute";
		div.className = "paenlBoxc";
		div.style.right = "0px";
		div.style.top = sheight+"px";
		div.style.display = '';	
		panelInterval = setInterval(function(){computePos();},50);
	}
	
	if(act === 'add')
		document.getElementById("span_addPrintFavo_"+id).className = "add on";
	else
		document.getElementById("span_addPrintFavo_"+id).className = "add";
	//当添加临时收藏夹失败，如达到上限，系统错误等，则需要取消勾选框
	var addFavoCheckboxId = document.getElementById("addFavoCheckboxId");
	//alert(addFavoCheckboxId.value);
	if(addFavoCheckboxId){
		var ckbox = addFavoCheckboxId.value;
		if(ckbox){
			e.checked = false;
			document.getElementById("span_addPrintFavo_"+id).className = "add on";
		}
	}
}

function computePos () {
	var width = 294,height=164;
	var body = document.documentElement;
	var sheight = body.scrollTop+body.clientHeight-height;
	
	var div = document.getElementById("paenlBoxc") ;
	if(div != null && typeof(div) != 'undefined' && div.style.display != 'none'){
		timeCounter4Panel += 50;
		if(timeCounter4Panel >= 5000 && startPanelTimeCount){
			div.style.display = 'none';	
			timeCounter4Panel = 0;
			if(panelInterval != null)
				clearInterval(panelInterval);
		}
		div.style.top = sheight+"px";
	}
}
/**
 * 关闭收藏夹提示面板
 */
function closePanel () {
	var div = document.getElementById("paenlBoxc") ;
	if(div != null && typeof(div) != 'undefined')
		div.style.display='none';
}

/**
 * 检查券，商家等的收藏状态
 * 如果已经收藏了，则自动将选择框选中
 */
function checkFavoStatus (resourceId) {
	//alert(resourceId);
	var url = "http://"+location.host+"/addToFavorite.do?resourceId="+resourceId+"&action=list";
	var x = doGet(url);
	if(x != null && typeof(x) != 'undefined'){
		var arr = x.split(",");
		for(var i = 0 ; i < arr.length;i++){
			var index = arr[i].replace("\r\n","").replace("\n","");
			var spani = document.getElementById("span_addFavo_"+index);
			var chkboxi = document.getElementById("chkbox_addFavo_"+index);
			if(spani != null && typeof(spani) != 'undefined'){
				spani.className = "add on";
			}
			if(chkboxi != null && typeof(chkboxi) != 'undefined'){
				chkboxi.checked = true;
			}
		}
	}
}

/**
 * 改变一个html的文字
 */
function chText (id){
	var v = document.getElementById("chkbox_addFavo_"+id);
	if(typeof(v) == 'undefined' || v == null)
		return;
	var c = document.getElementById("label_addFavo_"+id);
	if(typeof(c) == 'undefined' || c == null)
		return;
	if(v.checked == true || v.checked == 'checked'){
		c.innerHTML="取消该优惠券";
	}else{
		c.innerHTML="选中该优惠券";
	}
}

String.prototype.replaceAll  = function(s1,s2){   
	return this.replace(new RegExp(s1,"gm"),s2);   
}
String.prototype.startWith = function (s1) {
	return this.indexOf(s1) == 0;
}
String.prototype.endWith = function (s1){
	return this.indexOf(s1) == this.length-s1.length;
}

/**
 * 判断一个字符串是不是手机号码
 */
var MobileRegexp = /^\d+$/;
var ValidMobilePrefix = ['130','131','132','133','134','135','136','137','138','139','150','151','153','155','156','157','158','159','188','189','186'];
function isMobile (m,alerting) {
	if(!MobileRegexp.test(m)){
		if(alerting){
			alert("请输入正确的手机号码");
		}
		return false;
	}
		
	if(m.startWith("1")){
		for(var i = 0 ; i < ValidMobilePrefix.length;i++){
			if(m.startWith(ValidMobilePrefix[i]))
				return true;
		}
		if(alerting){
			alert("请输入正确的手机号码");
		}
		return false;
	}else if (m.startWith("0")){
		if(m.startWith("00")){
			if(alerting){
				alert("请输入正确的手机号码");
			}
			return false;
		}
		return m.length == 11 || m.length == 12;
	}else{
		if(alerting){
			alert("请输入正确的手机号码");
		}
		return false;
	}
}
/**
 * logout
 *
 */
function logout () {
	var divLoginState = $("divLoginState");
	var NotLoggedShoppingListInfo = $("NotLoggedShoppingListInfo");
	var LoggedShoppingListInfo = $("LoggedShoppingListInfo");
	var NotLoggedConsumePlanInfo = $("NotLoggedConsumePlanInfo");
	var LoggedConsumePlanInfo = $("LoggedConsumePlanInfo");
	
	if(typeof Account == 'undefined' || Account.gid <= 0){
		if(divLoginState){
			divLoginState.innerHTML=("<span  id='welcomInfo'>尊敬的用户，您好！\
									 </span><button id='userLoginButton' onclick=\"window.open('http://my.checkoo.com')\">登录</button>\
									 <button id='userRetButton' type=\"button\"  onclick=\"window.open('http://my.checkoo.com/register.jsp')\">注册</button>");
		}
		if(NotLoggedShoppingListInfo){
			NotLoggedShoppingListInfo.style.diaplay = '';
		}
		if(NotLoggedConsumePlanInfo){
			NotLoggedConsumePlanInfo.style.diaplay = '';
		}
		if(LoggedShoppingListInfo){
			LoggedShoppingListInfo.style.diaplay = 'none';
		}
		if(LoggedConsumePlanInfo){
			LoggedConsumePlanInfo.style.diaplay = 'none';
		}
		//document.getElementById("manageMyCoupons").style.display = 'none';
	}else{
		if(divLoginState){
			divLoginState.innerHTML=("<span>"+Account.username+"，您好！</span>\
									 <button onclick=\"window.open('http://my.checkoo.com/Logout.do');logout();\">退出</button>");
		}
		
		if(NotLoggedShoppingListInfo){
			NotLoggedShoppingListInfo.style.diaplay = 'none';
		}
		if(NotLoggedConsumePlanInfo){
			NotLoggedConsumePlanInfo.style.diaplay = 'none';
		}
		if(LoggedShoppingListInfo){
			LoggedShoppingListInfo.style.diaplay = '';
		}
		if(LoggedConsumePlanInfo){
			LoggedConsumePlanInfo.style.diaplay = '';
		}
		//document.getElementById("manageMyCoupons").style.display = '';
	}
}