function OpenQQLink(QQArray){
	var n = Math.floor(Math.random() * QQArray.length);
	var link = "http://wpa.qq.com/msgrd?v=3&Site=美丽加芬商城&menu=no&Uin="+QQArray[n];
	//document.getElementById('wpaqq').src = link;
	window.open(link);
}

$(document).ready(function(){
	SetDef("input.keyword");
	SetDef("input.pediaSearch");

	$("input.GoSearch").click(function(){
		Search();
	});

	$("a.ClearHistory").click(function(){
		$.cookie('history', '',{domain:location.hostname});
		$("ul.prdrecdlist").empty();
	});
});


function SetDef(box){
	$(box).click(function(){
		if( $(this).attr("tag")!="true" ){
			this.value="";
			$(this).attr("tag","true");
		}
	});
	$(box).blur(function(){
		if( $(this).val().length<=0 ){
			this.value = this.defaultValue;
			$(this).attr("tag","false");
		}
	});
}

function SearchArray(url,items){
	location.href = url +"?"+ allPrpos(items);
}

function Search(){
		var keyword=$("input.keyword").val();
		keyword=keyword.replace("商品货号或关键字","");
    var search = {"act":"search"
        ,"brand":$("select.SearchBrand").val()
        ,"category":$("select.SearchCategory").val()
        ,"key":keyword};

    window.location.href = "/shop.php" + "?" + allPrpos(search);
}

$(document).ready(function(){
	var name = $('#quickmenu');

	function scrollAd(){
		var offset = 93 + $(document).scrollTop();
		$(name).animate({top:offset,right:0},{duration:800,queue:false});
	}   
	scrollAd();
	$(window).scroll(scrollAd);
	if($("ul.CarList").length>0)
		LoadCarItems();
});



function LoadCarItems(){
	$.ajax({
		 type:"POST",
		 url:"/application/actions/ajax.car.php?action=CarMenu",
		 data: "",
		 dataType:"json",
		 success:function(object){
			 if( Number(object.Code)>0)
				 return ;
			 $("ul.CarList").empty();

			 var cars = eval("({})");
			 if(object.Rows!=undefined &&  object.Rows.length>0){
				 for(var key in object.Rows ){
					 if(object.Rows[key].html != undefined){
						 var sel = object.Rows[key];
						 if(sel.SalesID>0){
							 var buf = $(object.Rows[key].html);
							 $("ul.CarList").append(buf);
							 cars[sel.SalesID] = eval("({SId:"+sel.SalesID+",Count:"+sel.Count+",Price:"+sel.Price+",GId:"+sel.GID+"})");
						 }
					 }
				 }
			 }
			 if(object.Points!=undefined && object.Points.length>0){
				 for(var key in object.Points ){
					 if(object.Points[key].html != undefined){
						 var buf = $(object.Points[key].html);
						 $("ul.CarList").append(buf);							 
					 }
				 }
			 }

			 var value = JSON.Object2JSON(cars);
			 $.cookie('Cars',value);

			 $("span.CarListCount").html(object.Rows.length);
			 $("span.CarListPrice").html(object.Price);
		 }
	 });
}


function SetCar(GId,Count){
	var cars;
	var cookieVal = $.cookie('Cars');
	if( cookieVal == null || cookieVal == ""){
		cars = eval("({})");
	}else{
		cars = eval("("+cookieVal+")");
	}

	if(typeof cars[GId] == 'undefined'){
		cars[GId] = eval("({SId:"+GId+",Count:0,Price:0})");
	}
	var count = Number((isNaN(cars[GId].Count)?0:cars[GId].Count));

	cars[GId].Count = count + Number(Count);
	SetCarToCookie(cars);
}

function SetActToCookie(Acts){
	var value = JSON.Object2JSON(Acts);
	SetCookie('Act',value);
}
function SetCarToCookie(Cars){
	var value = JSON.Object2JSON(Cars);
	SetCookie('Cars',value);
}
function SetCookie(Key,Value){
	$.cookie(Key,Value, {"expires":30,path:"/"});
}
function GetCookie(Key){
		return $.cookie(Key);
}


///用户中心翻页用代码
function ReLoadSelect(count,page,RowCount){
	$("select.Page").each(function(){
		$(this).empty();
		for(i=0;i<count;i++){
			$(this).append('<option value='+i+'>第'+(i+1)+'页</option>');
		}
		$(this).val(page);
	});
	$("a.RowCount").html(RowCount);
}
///对象转参数字符串
function allPrpos(obj) { 
	var props = "";
	for(var p in obj){
		if(obj[p] == undefined){}
		else if(typeof(obj[p])=="function"){ 
			obj[p]();
		}else{ 
			props+= p + "=" + obj[p] + "&";
		} 
	} 
	return (props);
}



$.cookie = 
	function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options);
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



//商品分类树
function CategroysTree(){
	var cate = $.query.get("cate");
	if(cate=="")
		return;
	var target = jQuery("li[Categroys='"+cate+"']");
	if(target.length>0)
	{
		target.children().attr("style","color:#FFFFFF;background:url(../images/pkbg.png) left center no-repeat;");
		cate = target.attr("Parents");
	}
	var img = jQuery("div[Categroys='"+cate+"']").attr("style").replace(".png","_.png");
	jQuery("div[Categroys='"+cate+"']").attr("style", img);
	jQuery("ul[Categroys='"+cate+"']").attr("style", "display:display");
}


function json(obj){
    var a=new Array();
    for (i in obj){
        a[i] = '"'+i.replace(/\"/g,"\\\"")+'":"'+obj[i].replace(/\"/g,"\\\"")+'"';
    }
    var b = a.join(",");
    return "{"+b+"}";
}

String.prototype.format = function() {
    var formatted = this;
    for (arg in arguments) {
        formatted = formatted.replace("{" + arg + "}", arguments[arg]);
    }
    return formatted;
};


//正則

//检查特殊字符，包括@
function check_special_char(str)	{
	var regex=/[\!\@\#\$\%\^\&\*\+\`\/\[\]\'\"\\]+/g;
	return regex.test(str);
	}
	
//检查特殊字符，不包括@
function check_special_char_whitout_at(str)	{
	var regex=/[\!\#\$\%\^\&\*\+\`\/\[\]\'\"\\]+/g;
	return regex.test(str);
	}

//检查EMAIL
function check_email(str)	{
	var regex=/[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
	return regex.test(str);
	}

//检查全数字
function check_number(str)	{
	var regex=/^[\d.]+$/;
	return regex.test(str);
	}
//检查手机号码
function CheckMobile(mobile)
{
    var regex = /^0*(13|14|15|18)\d{9}$/;
    return regex.test(mobile);
}
//判断字符串长度，中文等于2个字符
function Len(str)	{
	var i,sum;
	sum=0;
	for(i=0;i<str.length;i++)		{
		if ((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255))
			sum=sum+1;
		else
			sum=sum+2;
	}
	return sum;
}

var JSON = {
		Object2JSON:function(obj){
		        var res = [];
		        if (obj instanceof Array) {
		            for(var i=0;i<obj.length;i++){
		                var e = obj[i];
		                var str = '';
		                switch (typeof e) {
		                    case "boolean":
		                        str = Boolean(e);break;
		                    case "number":
		                        str = Number(e);break;
		                    case "string":
		                        str = '"' + e + '"';break;
		                    case "object":
		                        str = JSON.Object2JSON(e);break;
		                    default:
		                        str = e;
		                }
		                res.push(str);
		            }
		            res = res.join(",");
		            res = '['+res+']';
		        } else {
		            for (var i in obj) {
		                var e = obj[i];
		                var str = '';
		                switch (typeof e) {
		                    case "boolean":
		                        str = '"' + i + '":' + Boolean(e);break;
		                    case "number":
		                        str = '"' + i + '":' + Number(e);break;
		                    case "string":
		                        str = '"' + i + '":"' + e + '"';break;
		                    case "object":
		                        str = '"' + i + '":' + JSON.Object2JSON(e);break;
		                    default:
		                        str = e;
		                }
		                res.push(str);
		            }
		            res = res.join(",");
		            res = '{'+res+'}';
		        }
		        return res;
		    }
}
