/* common configuration */
var charset = 'gbk'; /* 系统编码 */
var is_ie = window.ActiveXObject ? true : false; /* 是否为IE浏览器 */
var is_chrome = window.navigator.userAgent.indexOf('Chrome') >= 0 ? true : false; /* 是否为Chrome浏览器 */ 
var ie_ie6 = window.navigator.userAgent.indexOf('MSIE 6') >= 0 ? true : false; /*　是否为ie6 */
var DOMAIN = '/'; /*由原来的domain改成web目录 */
var domain = 'http://www.ahzp.com';
var _funcdict = {}; /* 全局  函数盒子 */

/* common function */
function mb_strlen(str) {
	var len = 0;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (charset == 'utf-8' ? 3 : 2) : 1;
	}
	return len;
}

function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}

function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds);
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function loadCssFile(path){
    var doc   = document;
    var body  = doc.body;
    var link  = doc.createElement("LINK");
    if(link && body){
        link.href = path;
        link.type = "text/css";
        link.rel  = "Stylesheet";
        body.insertBefore(link, body.firstChild);
    }
}

function doane(event) {
	e = event ? event : window.event;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}


/* pmwin相关 begin */
/*
 *显示窗体: function win(action,  url, param, canmove, width, heightpercent, id)
 */

function win(action, dict) {
	
	var url = dict.url;
	var param = dict.param;
	var canmove = dict.canmove;
	var width = dict.width;
	var heightpercent = dict.height;
	var id = dict.id;
	var closeend = dict.closeend;
	var openend = dict.openend;
	var onlyone = dict.onlyone;
	
	if (url) {
		if (url[0] == '/' || url.substr(0,7) == 'http://') {
			var pre = /^(?:http:\/\/[^\/]+[\/]+|\/)?(.*)/;
			url = url.replace(pre, DOMAIN + '$1');
		} else {
			var pre = /^(.*?)[^\/]+$/;
			var pn = self.location.pathname.replace(pre, "$1");
			url = pn + url;
		}
	}
	
	if (typeof onlyone == 'undefined') {
		onlyone = true;
	}
	
	if (id == "" || typeof id == "undefined") {
        id = "pmlayer" + Date.parse(new Date()).toString();
    }

    if (param == "" || typeof param == "undefined") {
        param == undefined;
    }
	
	var TOTALWIDTH = "";
    if (width == 0 || width == "" || typeof width == "undefined") {
        TOTALWIDTH = 800;
    } else {
        TOTALWIDTH = width;
    }

    if (heightpercent == 0 || heightpercent == "" || typeof heightpercent == "undefined") {
        var TOTALHEIGHTPERCENT = 0.9;
    } else {
        var TOTALHEIGHTPERCENT = heightpercent;
    }

    if (typeof canmove == "undefined" || canmove == 0 || canmove == false) {
        canmove = false;
    } else {
        canmove = true;
    }

	var objs = document.getElementsByTagName("OBJECT");
	var objs2 = document.getElementsByTagName("SELECT");
	if(action == 'open') {
		/* 同一个窗口只能打开一次 */
		if (onlyone && typeof top._urliframehash != 'undefined') {
			if (typeof top._urliframehash[url] != 'undefined') return false;
		}
		
		for(i = 0;i < objs.length; i ++) {
			if(objs[i].style.visibility != 'hidden') {
				objs[i].setAttribute("oldvisibility", objs[i].style.visibility);
				objs[i].style.visibility = 'hidden';
            }
		}
		if (ie_ie6) {
			for(i = 0;i < objs2.length; i ++) {
				if(objs2[i].style.visibility != 'hidden') {
					objs2[i].setAttribute("oldvisibility", objs2[i].style.visibility);
					objs2[i].style.visibility = 'hidden';
				}
			}
		}
        var clientWidth = document.body.clientWidth;
		var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		var pmwidth = parseInt(TOTALWIDTH > 1 ? TOTALWIDTH : clientWidth * TOTALWIDTH);
		var pmheight = parseInt(TOTALHEIGHTPERCENT > 1 ? TOTALHEIGHTPERCENT : clientHeight * TOTALHEIGHTPERCENT);
		if($('#'+id).length == 0) {
			div = document.createElement('div');
			div.id = id;
			div.innerHTML = "";
			div.style.position = 'absolute';
			//div.style.zIndex = '999';/* ztzt3 style原来有个  margin-bottom: -30px; pmframe 原来有 height:100% */
			div.innerHTML = '<div id="ztzt1" style="width: ' + pmwidth + 'px; background: #666666; margin: 5px auto; text-align: left">' +
			'<div class="ztzt2" style="width: ' + pmwidth + 'px; height: ' + pmheight + 'px; padding: 1px; background: #FFFFFF; border: 1px solid #7597B8; position: relative; left: -6px; top: -3px">' +
			'<div class="ztzt3" style="position: relative; left: 0px; top: 0px; width: ' + pmwidth + 'px; height: 34px;line-height:34px;"></div>' +
			'<a class="ztzt4" href="###" onclick="return win(\'close\',{id:\''+id+'\'});"><img style="position: absolute; right: 20px; top: 13px; border:none;" src="'+DOMAIN+'template/default/close.gif" title="关闭" /></a>' +
			'<div class="pmwinmask" style="margin-top: 30px; position: absolute; width: 100%; height: 100%; display: none"></div><iframe class="pmframe" name="pmframe" style="width:' + pmwidth + 'px;" allowTransparency="true" frameborder="0"></iframe></div></div>';
			$('#append_parent')[0].appendChild(div);
			div = $(div);
		}else{
			div = $('#'+id)[0];
			div.find('.ztzt1').css('width', TOTALWIDTH + 'px');
			div.find('.ztzt2').css('width', TOTALWIDTH + 'px').css('height', pmheight + 'px');
			div.find('.ztzt3').css('width', TOTALWIDTH + 'px');
			div.find(".pmframe").css('width', pmwidth + 'px');
		}
		
		var pmframe = div.find(".pmframe")[0].contentWindow;
		
		div.find(".pmframe").height(div.find(".pmwinmask").height()-36);
		if (closeend) {
			window._funcdict['oncloseend_'+div.attr('id')] = closeend;
		}
		
		if (canmove) {
			div[0].rel = new Array();
			div.find('.ztzt3').mouseover(function(){pmwindrag(this, id);}).css('cursor', 'move');
		} else {
			div.find('.ztzt3').unbind("mouseover").css('cursor', 'auto');
		}
		
		div.width(pmwidth);
		div.height(pmheight);
        div.css("left", ((clientWidth - pmwidth) / 2) + 'px');
		div.css("top", ((clientHeight - pmheight) / 2 + scrollTop) + 'px');
		
		var tmpfunc = function(){
			div.find('.ztzt3').html("<span>&nbsp;&nbsp;</span>" + pmframe.document.title);
			//pmframe.document.body.rel = id;
			if (typeof openend == 'function') openend();
			if (pmframe.document.activeElement.tagName.toLowerCase() == 'body') {
				pmframe.document.getElementsByTagName('BODY')[0].focus();
				pmframe.document.getElementsByTagName('BODY')[0].blur();
			}
		};
		
		if (is_ie) {
			div.find('.pmframe')[0].onreadystatechange = function(){
				if (this.readyState && this.readyState == 'complete') {
					tmpfunc();
				}
			};
		} else if (is_chrome) {
			div.find('.pmframe')[0].onload = function() {
				tmpfunc();
			}
		} else {			
			pmframe.onload = function() {
				tmpfunc();
			};
		}
	
		if(param) {
			url += '&' + param;
		}
		
		if (typeof top._urliframehash == 'undefined') {
			top._urliframehash = {};
			
		} else {
			/*for(var key in top._urliframehash) {
				var __id = top._urliframehash[key];
				$('#'+__id).find('.ztzt3').addClass('ztzt3_focus');
				top._lastFocus = id;
			}*/
		}
		top._urliframehash[url] = id; /* 赋值 hash */
		
		div.find('.pmframe')[0].setAttribute('src', url);
			
		div.css("display", "");
		
		
	/* close */
	} else if(action == 'close') {
		for(i = 0;i < objs.length; i ++) {
			if(objs[i].attributes['oldvisibility']) {
				objs[i].style.visibility = objs[i].attributes['oldvisibility'].nodeValue;
				objs[i].removeAttribute('oldvisibility');
			}
		}
		if (ie_ie6) {
			for(i = 0;i < objs2.length; i ++) {
				if(objs2[i].attributes['oldvisibility']) {
					objs2[i].style.visibility = objs2[i].attributes['oldvisibility'].nodeValue;
					objs2[i].removeAttribute('oldvisibility');
				}
			}
		}
		/* 在此处可以插入事件 */
		if (window._funcdict['oncloseend_' + id]) {
			window._funcdict['oncloseend_' + id]();
		}
		//hiddenobj = new Array();
		/*$('#'+id).fadeOut(200, function(){
			$('#append_parent')[0].removeChild($('#'+id)[0]);
		});*/
		$('#'+id).hide();
		$('#append_parent')[0].removeChild($('#'+id)[0]);
		for(var key in top._urliframehash) {
			if (top._urliframehash[key] == id) {
				top._urliframehash[key] = undefined;
			}
		}
	}
	return false;
}

//var pmwindragstart = new Array();
function pmwindrag(obj, id) {
	var divpmplay = $("#"+id);
	obj.onmousedown = function(e) {
		if(is_ie) {
			document.body.onselectstart = function() {
				return false;
			}
		}
		divpmplay[0].rel = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		divpmplay[0].rel[2] = parseInt(divpmplay[0].style.left);
		divpmplay[0].rel[3] = parseInt(divpmplay[0].style.top);
		divpmplay.find('.pmwinmask')[0].style.display = '';
		doane(e);
	}
	_attachEvent(document.body, 'mousemove', function(e) {
		if(divpmplay[0].rel[0]) {
			var pmwindragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
			with(divpmplay[0]) {
				style.left = (divpmplay[0].rel[2] + pmwindragnow[0] - divpmplay[0].rel[0]) + 'px';
				style.top = (divpmplay[0].rel[3] + pmwindragnow[1] - divpmplay[0].rel[1]) + 'px';
			}
			doane(e);
		}
	});
	obj.onmouseup = function(e) {
		if(is_ie) {
			document.body.onselectstart = function() {
				return true;
			}
		}
		divpmplay[0].rel = [];
		divpmplay.find('.pmwinmask')[0].style.display = 'none';
		doane(e);
	}
	obj.onmouseover = null;
}

/* pmwin相关 end */

function Confirm(str) {
	var val = confirm(str);
	return val;		
}

/*倒计时跳转*/
/*
 *@selector jquery对象或者selector字符串
 *@str 跳转的时候显示的文字 如 ‘跳转’、‘刷新'等
 *@cttime 倒计时时间 单位：秒  默认 3秒
 */
var _countdownTimeouthandlerid = null;
var countdown = function(selector, str, cttime) {
	if (typeof cttime == 'undefined') cttime = 3; /* 默认三秒跳转 */
	var slt = $(selector);
	slt.click(function(){
		if (_countdownTimeouthandlerid) clearTimeout(_countdownTimeouthandlerid);
	});
	slt.html(cttime.toString() + "秒后" + str);
	if (cttime != 0) {
		cttime -= 1;
		_countdownTimeouthandlerid = setTimeout(function(){countdown(selector, str, cttime)}, 1000);	
	} else {
		slt.click();
	}
}

/*
 *获得弹出窗的hash值
 */
var getframeid = function(Self) {
	return top._urliframehash[Self.location.href.replace(/^http:\/\/[^\/]+/, '')];
};

if (ie_ie6) {
	$(function(){
		$(":text").css("border", "1px solid #68A6BD");
		$(":password").css("border", "1px solid #68A6BD");
		$("textarea").css("border", "1px solid #68A6BD");
	});
}
/*
 * 获得滚动条距离
 */  
var IeTrueBody = function(){  
   return (document.compatMode && document.compatMode!="BackCompat")?document.documentElement : document.body;  
}; 
  
var getScrollTop = function(){  
   return is_ie ? IeTrueBody().scrollTop : window.pageYOffset;  
};
var getScrollLeft = function(){
	return is_ie ? IeTrueBody().scrollLeft : window.pageXOffset;
};

// 查看面试通知
var viewinterview = function(eiid) {
	win('open', {url:domain + '/mod/interview_view/?op=view&eiid='+eiid.toString() ,canmove:true, width:580, height:480, closeend: function(){}});
};

/* 投递简历 */
var deliveryresume = function(url) {
	win('open', {url:url, canmove:true, width:700, height:0.8});
};
/* 修改密码 */
var chanagepsw = function() {
	win('open', {url:domain + "/mod/changepwd", canmove:true, width:300, height:300});
};
