﻿//-------------------------------
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
// this function gets the cookie, if it exists
// don't use this, it's weak and does not handle some cases
// correctly, this is just to maintain legacy information
function Get_Cookie( name ) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
//-------------------------------

// --- 存储当前顶部菜单的id
function saveToptypeCookie(toptype_id) {
	Set_Cookie( 'toptype_id', toptype_id, 365, '/', '', '' );
	if ( Get_Cookie( 'subtype_id' ) ){Set_Cookie( 'subtype_id', '', '', '/', '', '' );}
	if ( Get_Cookie( 'job_id' ) ){Set_Cookie( 'job_id', '', '', '/', '', '' );}
}
// --- 存储当前左侧菜单的id
function saveSubtypeCookie(subtype_id) {
	Set_Cookie( 'subtype_id', subtype_id, 365, '/', '', '' );
	if ( Get_Cookie( 'job_id' ) ){Set_Cookie( 'job_id', '', '', '/', '', '' );}
}
//--- 存储当前工作职位的id
function saveJobCookie(job_id){
	Set_Cookie( 'job_id',job_id, 365, '/', '', '' );
	if ( Get_Cookie( 'subtype_id' ) ){Set_Cookie( 'subtype_id', '', '', '/', '', '' );}
}
//页面加载时设置当前菜单的背景色为#ff5f00
function getMyCookies(){	
    var toptype_id,subtype_id,job_id;
    if ( Get_Cookie( 'toptype_id' ) ){
    	toptype_id = Get_Cookie( 'toptype_id' );
		var top = document.getElementsByName(toptype_id);
		if(top.length>0){
			top[0].style.backgroundColor = "#ff5f00";
		}  	
    }
    if ( Get_Cookie( 'subtype_id' ) ){
    	subtype_id = Get_Cookie( 'subtype_id' );
		var sub = document.getElementsByName(subtype_id);
		if(sub.length>0){
			sub[0].focus();
			sub[0].style.backgroundColor = "#ff5f00";
		}
    }
    if ( Get_Cookie( 'job_id' ) ){
    	job_id = Get_Cookie( 'job_id' );
		var job = document.getElementsByName(job_id);
		if(job.length>0){
			job[0].focus();
			job[0].style.backgroundColor = "#ff5f00";	
		}
    }
}
//重置所有菜单cookies
function clearingCookie(){
	if ( Get_Cookie( 'toptype_id' ) ){Set_Cookie( 'toptype_id', '', '', '/', '', '' );}
	if ( Get_Cookie( 'subtype_id' ) ){Set_Cookie( 'subtype_id', '', '', '/', '', '' );}
	if ( Get_Cookie( 'job_id' ) ){Set_Cookie( 'job_id', '', '', '/', '', '' );}
}

function setOnloadEvent(func){
 var oldonload = window.onload;
 if(typeof window.onload != "function"){
  window.onload = func;
 }
 else{
    window.onload = function(){
     oldonload();
     func();
    }
 }
}

function AddFavorite(sURL, sTitle){
	try
	{
		window.external.addFavorite(sURL, sTitle);
	}
	catch (e){
		try{
			window.sidebar.addPanel(sTitle, sURL, "");
		}
		catch (e){
			alert("加入收藏失败，有劳您手动添加。");
		}
	}
}

//function AddFavorite(sURL, sTitle)
//{
//   if (document.all)
//   {
//      window.external.addFavorite('http://www.skycn.com','天空软件站');
//   }
//   else if (window.sidebar)
//   {
//      window.sidebar.addPanel('天空软件站', 'http://www.skycn.com', "");
//   }
//}

setOnloadEvent(getMyCookies);

//刷新验证码--------------------------------------------------------------
function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

////////////////////////////////////////////////////////////////////////////////
/*
 *--------------- 客户端表单通用验证CheckForm(oForm) -----------------
 * 功能:通用验证所有的表单元素.
 * 使用:
 *    <form name="form1" onsubmit="return CheckForm(this)">
 *    <input type="text" name="id" check="^\S+$" warning="id不能为空,且不能含有空格"/>
 *    <input type="submit"/>
 *    </form>
 * author:wanghr100(灰豆宝宝.net)
 * email:wanghr100@126.com
 * update:19:28 2004-8-23
 * 注意:写正则表达式时一定要小心.不要让"有心人"有空子钻.
 * 已实现功能:
 * 对text,password,hidden,file,textarea,select,radio,checkbox进行合法性验证
 * 待实现功能:把正则表式写成个库.
 *--------------- 客户端表单通用验证CheckForm(oForm) -----------------
 */
////////////////////////////////////////////////////////////////////////////////
 
//主函数
function CheckForm(oForm)
{
    var els = oForm.elements;
    //遍历所有表元素
    for(var i=0;i<els.length;i++)
    {
        //是否需要验证
        if(els[i].getAttribute("check"))
        {
            //取得验证的正则字符串
            var sReg = els[i].getAttribute("check");
            //取得表单的值,用通用取值函数
            var sVal = GetValue(els[i]);
            //字符串->正则表达式,不区分大小写
            var reg = new RegExp(sReg,"i");
            if(!reg.test(sVal))
            {
                //验证不通过,弹出提示warning
                alert(els[i].getAttribute("warning"));
                //该表单元素取得焦点,用通用返回函数
                GoBack(els[i])  
                return false;
            }
        }
    }
    return true;
}
 
//通用取值函数分三类进行取值
//文本输入框,直接取值el.value
//单多选,遍历所有选项取得被选中的个数返回结果"00"表示选中两个
//单多下拉菜单,遍历所有选项取得被选中的个数返回结果"0"表示选中一个
function GetValue(el)
{
    //取得表单元素的类型
    var sType = el.type;
    switch(sType)
    {
        case "text":
        case "hidden":
        case "password":
        case "file":
        case "textarea": return el.value;
        case "checkbox":
        case "radio": return GetValueChoose(el);
        case "select-one":
        case "select-multiple": return GetValueSel(el);
    }
    //取得radio,checkbox的选中数,用"0"来表示选中的个数,我们写正则的时候就可以通过0{1,}来表示选中个数
    function GetValueChoose(el)
    {
        var sValue = "";
        //取得第一个元素的name,搜索这个元素组
        var tmpels = document.getElementsByName(el.name);
        for(var i=0;i<tmpels .length;i++)
        {
            if(tmpels[i].checked)
            {
                sValue += "0";
            }
        }
        return sValue;
    }
    //取得select的选中数,用"0"来表示选中的个数,我们写正则的时候就可以通过0{1,}来表示选中个数
    function GetValueSel(el)
    {
        var sValue = "";
        for(var i=0;i<el.options.length;i++)
        {
            //单选下拉框提示选项设置为value=""
            if(el.options[i].selected && el.options[i].value!="")
            {
                sValue += "0";
            }
        }
        return sValue;
    }
}
 
//通用返回函数,验证没通过返回的效果.分三类进行取值
//文本输入框,光标定位在文本输入框的末尾
//单多选,第一选项取得焦点
//单多下拉菜单,取得焦点
function GoBack(el)
{
    //取得表单元素的类型
    var sType = el.type;
    switch(sType)
    {
        case "text":
        case "hidden":
        case "password":
        case "file":
        case "textarea": el.focus();//var rng = el.createTextRange(); rng.collapse(false); rng.select();
        case "checkbox":
        case "radio": var els = document.getElementsByName(el.name);els[0].focus();
        case "select-one":
        case "select-multiple":el.focus();
    }
}
////////////////////////////////////////////////////////////////////////////////
