//----------------------------------------------弹出DIV仿模态窗口开始----------------------------------------------
var divW;       //DIV宽度
var divH;       //DIV高度
var clientH;       //浏览器高度
var clientW;       //浏览器宽度
var pageUrl;       //DIV中加载的页面
var div_X;       //DIV横坐标
var div_Y;       //DIV纵坐标

function DivWindowOpen(divWidth,divHeight,url){
	 $("body").css({"overflow-y":"hidden","overflow-x":"hidden"});
   divW = divWidth;       //DIV宽度
   divH = divHeight;      //DIV高度

   pageUrl = url;       //DIV中加载的页面UR
   lockScreen();       //锁定背景
      
   divOpen();     
   $("#divContent").load(pageUrl);   
	
}
//关闭DIV窗口
function close_w() {
   clearDivWindow();
   clearLockScreen();
   $("body").css({"overflow-y":"","overflow-x":""});
}

//返回弹出的DIV的坐标
function divOpen(){
   var minTop = 80;       //弹出的DIV记顶部的最小距离
   if($("#divWindow").length == 0){
   clientH = $(window).height();       //浏览器高度
   clientW = $(window).width();       //浏览器宽度
      
    div_X = (clientW - divW)/2;       //DIV横坐标
    div_Y = (clientH - divH)/2;       //DIV纵坐标    
    div_X += window.document.documentElement.scrollLeft;       //DIV显示的实际横坐标
    div_Y += window.document.documentElement.scrollTop;       //DIV显示的实际纵坐标
    if(div_Y < minTop){
           div_Y = minTop;
    }
    $("body").append("<div id='divWindow'><div id='divContent'><div class='float-window float-prompt' id='pop_load'>加载中...</div></div></div>");       //增加DIV
    //divWindow的样式
    $("#divWindow").css("position","absolute");
    $("#divWindow").css("z-index","200");
    $("#divWindow").css("left",(div_X + "px"));       //定位DIV的横坐标
    $("#divWindow").css("top",(div_Y + "px"));       //定位DIV的纵坐标
    $("#divWindow").css("opacity","0.9");
   // $("#divWindow").width(divW);
    //$("#divWindow").height(divH);
    $("#divWindow").css("background-color","#FFFFFF");
    //$("#divWindow").css("border","solid 1px #333333");  
    
    //divContent的样式
    $("#divContent").css("padding","0px");
   }
   else{
      clientH = $(window).height();       //浏览器高度
      clientW = $(window).width();       //浏览器宽度
      div_X = (clientW - divW)/2;       //DIV横坐标
      div_Y = (clientH - divH)/2;       //DIV纵坐标
      div_X += window.document.documentElement.scrollLeft;       //DIV显示的实际横坐标
      div_Y += window.document.documentElement.scrollTop;       //DIV显示的实际纵坐标
      if(div_Y < minTop){
             div_Y = minTop;
      }
      $("#divWindow").css("left",(div_X + "px"));       //定位DIV的横坐标
      $("#divWindow").css("top",(div_Y + "px"));       //定位DIV的纵坐标
   }
}

//锁定背景屏幕
function lockScreen(){
	$.cover.show();  
}

//清除背景锁定
function clearLockScreen(){
  $.cover.hide();
}

//清除DIV窗口
function clearDivWindow(){
       $("#divWindow").remove();
}

//窗口大小改变时
$(window).resize(
    function(){
           if($("#divLock").length != 0){
                  lockScreen();
           }
           if($("#divWindow").length != 0){
                  divOpen();
           }
    }
);
//----------------------------------------------弹出DIV仿模态窗口结束----------------------------------------------

/*
*遮罩层api
*@author finder.hyj
*2009.08.27
*/
$.extend({
   cover:{
       show:function(){
           $.cover.hide();
           var str="<div id='cover' style='position:absolute;background-color: #666;" ;
               str+="width:"+100+"%;";
               str+="height:"+$(document).height()+"px;";
               str+="opacity:0.5;filter:alpha(opacity=50);" ;
               str+="top:0;left:0;z-index=10'></div>";
           $(document.body).append(str);
       },
       hide:function(){
           $("#cover").remove();
          
       }
    }
});


function $obj(id)
{
   return document.getElementById(id);
}
function sub_order(type)
{
   if(!Ulogin) 
   pop_login();
  // Lgn_tab();
   else
   {
      if(type=="dg")
	  {
	     var goods_name = $("#goods_name").text();
         var goods_url = $("#goods_url").text();
         var goods_price = $("#goods_price").text();
         goods_name = encodeURI(goods_name);
         goods_url = escape(goods_url);
	     var e_md = getCookie('e_md');
         var tourl = 'http://home.youdai.net/cp.php?ac=order&type=dg&op=add&g_name='+goods_name+'&g_url='+goods_url+'&g_price='+goods_price;
	     if(e_md!=null)
		    tourl += '&md='+e_md;
	  }
	  else if(type=="dp")
	  {
	    var firm_name = $("#firm_title").text();
		var firm_url = $("#firm_url").text();
		firm_name = encodeURI(firm_name);
		firm_url = escape(firm_url);
		var tourl = "http://home.youdai.net/cp.php?ac=order&op=add&type=dx&firm_name="+firm_name+"&firm_url="+firm_url;
	  }
	  location.href=tourl;
   }
   return false;
}

//登录提示框1
function Lgn_tab()
{
	
   var mask = document.createElement('div');
   var  _scrollwidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
   var _scrollheight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
   mask.style.position = 'absolute';
   mask.style.zIndex = "1";
   mask.style.width = _scrollwidth+'px';
   mask.style.height = _scrollheight+'px';
   mask.style.top = 0;
   mask.style.left = 0;
   mask.style.background = "#33393C";
   mask.style.filter = "alpha(opacity=40)";
   mask.style.opacity = "0.40";
   mask.style.display = "";
   mask.id = 'mask';
   $obj("append_parent").appendChild(mask);
   
   var lgn_div = document.createElement('div');
   lgn_div.style.position='absolute';
   lgn_div.style.zIndex= '2';
   lgn_div.style.backgroundColor="#ffffff";
   lgn_div.id='lgn_div';
   lgn_div.style.width="500px";
   lgn_div.style.height="250px";
   lgn_div_w_h = getobjwh(lgn_div);
   lgn_div.style.left = lgn_div_w_h.split("|")[0]+"px";
   lgn_div.style.top = lgn_div_w_h.split("|")[1]+"px";
   $obj("append_parent").appendChild(lgn_div);   
   $("#lgn_div").load("/login.php?ac=login1&ajax=1");  
   if(document.all)
   {
     window.attachEvent('onresize',changemask);
     window.attachEvent('onscroll',changelform);
   }
   else
   {
      window.addEventListener('resize',changemask,false);
      window.addEventListener('scroll',changelform,false);
   }
     
 
}

function changemask()
{
  var mask = document.getElementById('mask');
  var _scrollwidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
  var _scrollheight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
  mask.style.width = _scrollwidth+'px';
  mask.style.height = _scrollheight+'px';
  var login_form = document.getElementById('lgn_div');
  var lf_w_h = getobjwh(login_form);
  login_form.style.left = lf_w_h.split('|')[0]+'px';
  login_form.style.top = lf_w_h.split('|')[1]+'px';
}

function changelform()
{
  var login_form = document.getElementById('lgn_div');
  var lf_w_h = getobjwh(login_form);
  login_form.style.left = lf_w_h.split('|')[0]+'px';
  login_form.style.top = lf_w_h.split('|')[1]+'px';
}

function getobjwh(obj)
{
  var cw = document.documentElement.clientWidth;
  var ch = document.documentElement.clientHeight;
  var st = document.documentElement.scrollTop;
  var sl = document.documentElement.scrollLeft;
  var obj_w = parseInt(obj.style.width);
  var obj_h = parseInt(obj.style.height);
  var res_w = Number(sl)+(Number(cw)-Number(obj_w))/2;
  var res_h = Number(st)+(Number(ch)-Number(obj_h))/2
  return  res_w+'|'+res_h;
}

function ajaxpost()
{
  var username = $obj("lgn_u").value;
  var pwd = $obj("lgn_pwd").value;
  $.ajax({
  	type:"POST",
  	url:"/login.php?ac=login2&ajax=1&func1=repage&func2=slogin",
  	data:"user="+username+"&pwd="+pwd,
  	success:function(html){
  		if(html.indexOf("ajaxok")!=-1)
  		{
  			html = html.replace('<ajaxok></ajaxok>','');
  			setTimeout('repage()',2000);
  		}
  		else 
  		{
  			html = html.replace('<ajaxerror></ajaxerror>','');
  			setTimeout('slogin()',2000);
  		}
  		$("#lgn_div").html(html);
  	},
  	dataType:"html"
  	})
}

function repage()
{
  document.location.reload();
}

function slogin()
{
   $("#lgn_div").load("/login.php?ac=login1&ajax=1");
}
function close_tab()
{
  if(document.all)
  {
    window.detachEvent('onresize',changemask);
	window.detachEvent('onscroll',changelform);
  }
  else
  {
    window.removeEventListener('resize',changemask,false);
	window.removeEventListener('scroll',changelform,false);
  }
  var mask = $obj('mask');
  var lgn_div = $obj('lgn_div');
  if(mask!=null&&mask!=undefined)
  {
     mask.parentNode.removeChild(mask);
  }
  if(lgn_div!=null&&lgn_div!=undefined)
  {
    lgn_div.parentNode.removeChild(lgn_div);
  }
}

//便捷工具
function wop(type)
{
  if(type=='size')
  {
	 window.open('http://home.youdai.net/jtool/tool-size.html','_blank',"width=960,height=550,location=no,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0",false);
  }
  else if(type == 'wgt')
  {
	 window.open('http://home.youdai.net/jtool/tool-freight.html','_blank',"width=960,height=620,location=no,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0",false);
  }
  else if(type == 'price')
  {
	 window.open('http://home.youdai.net/jtool/tool-price.php','_blank',"width=460,height=590,location=no,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0",false);
  }
  else if(type == 'wash')
  {
	 window.open('http://home.youdai.net/jtool/tool-wash.html','_blank',"width=760,height=590,location=no,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0",false);
  }
  else
     return false;
}

function getCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1)
		{
			c_start = c_start + c_name.length + 1;
			c_end   = document.cookie.indexOf(";",c_start);
			if (c_end == -1)
			{
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null
}
function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); //使设置的有效时间正确。增加toGMTString()
}
function save2favo(title,url)
  {
    if(title!=''&&url!='')
	{
	  if(window.sidebar)
	  {
	    window.sidebar.addPanel(title,url,"");
	  }
	  else
	  {
	    window.external.addFavorite(url,title);
	  }
	}
	return true;
}


//去除首尾空白
String.prototype.trim=function() {
	return this.replace(/(^\s*)|(\s*$)/g,"");
}

//验证email
function check_email(str) {
	var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,4}){1,2})$/;
	if(str.search(reg)==-1){
	alert("Email格式错误！");	
	return false;
	} else {
		return true;
	}
}

/*************************
*****添加于2010-5-17******
***以下是应用于head.htm***
**************************/

//判断省份
$(function() {
$.ajax({
	type:"GET",
	url:"/tools/province.php",
	success:function(msg) {	
		$("#province").html(msg);				
	}
});	

//判断是否登录	
$.ajax({
	type:"GET",
	url:"/login.php",
	data:"ac=checklogin&"+parseInt(Math.random()*1000),
	success:function(msg) {								
		$("#unp").html(msg);		
	} 
});
});

//异步载入数据
$(function() {
	$("#title_rec").load('{dede:global.cfg_cmspath /}/tools/ajax_html.php?action=title_rec',{'typeid':'{dede:field.typeid /}','channel':'{dede:field.channel /}'});
	$("#title_sale").load('{dede:global.cfg_cmspath /}/tools/ajax_html.php?action=title_sale',{'typeid':'{dede:field.typeid /}'}); 
}); 

//登录弹出框  DivWindowOpen函数来自 frame.js
function pop_login() {	
	var str = "/login2.htm";	
	DivWindowOpen('550','256',str); 
} 

//注册弹出框  DivWindowOpen函数来自 frame.js
function pop_register() {
	var str = "/register.html"; 
	DivWindowOpen('650','406',str);
}

//判断用户是否登录 应用于订单提交页面article_shangpin.htm
function check_islogin(web_str) {	
	$.ajax({
	type:"GET",
	url:"/login.php",
	data:"ac=is_login&"+parseInt(Math.random()*1000),
	success:function(msg) {								
		if(msg!='') {
			location.href=web_str;
		} else {
			pop_login();
		}		
	} 
});
}

//判断用户是否登录
$.ajax({
type:"GET",
url:"/login.php",
data:"ac=is_login&"+parseInt(Math.random()*1000),
success:function(msg) {						
	if(msg!='') { 
		$("#tips_login").hide();
		$("#online").show();
	} else {
		$("#tips_login").show();
		$("#online").hide();
	}
} 
});	

	//对登录页面login2.htm进行验证
	function check_login() {
	var obj = document.form_login;	
	if(obj.user.value=="") {
		alert("用户名不能为空！");
		obj.user.focus();
		return false;
	} else if(obj.pwd.value=="") {
		alert("密码不能为空！");
		obj.pwd.focus();
		return false;
	}  else if(obj.secode.value=="") {
		alert("验证码不能为空！");
		obj.secode.focus();
		return false;
	}
	return true;
}

