//////////////////////////////////////////////
//  0 Author:mooring                       
//////////////////////////////////////////////
function $(id)
{
	try
	{
		if(document.getElementById)
		{
			return document.getElementById(id);
		}
	}
	catch (e)
	{
		try
		{
			if(document.all)
			{
				return document.all[id];
			}
			else if(document.layers)
			{
				return document.layers[id];
			}
		}
		catch(e)
		{
			alert(e.message);
			return false;
		}
	}
}

function addevent(obj,evt,func,boo)
{
	try
	{
		obj.addEventListener(evt,func,boo);	
	}
	catch (e)
	{
		try
		{
			obj.attachEvent("on"+evt,func);
		}
		catch (e)
		{
			try
			{
				eval(obj+".on"+evt)=func;	
			}
			catch (e)
			{
				alert(e.message);
			}
		}
	}
}

function Ajax()
{
	var xmlhttp = null;
	try
	{
		xmlhttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					xmlhttp = window.createRequest();
				}
				catch (e)
				{
					alert(e.message);
				}
			}
		}

	}
	if(xmlhttp != null || xmlhttp != undefined)
	{
		return xmlhttp;
	}
	else
	{
		alert("Your Browser is too old to support Ajax");
		return false;
	}
	return false;
}

/*----------===============cookie functions start===============----------*/
var cookies = new Object();
cookies.add = setcookie;
cookies.get = getcookie;

function setcookie(nam,val,day)
{
	var d = new Date();
	d.setDate(d.getDate()+day);
	document.cookie = nam + "=" + val + ";path=/;expires="+d.toGMTString();
}

function getcookie(nam)
{
	var ck = document.cookie.toString().split("; ");
	for(var i=0; i<ck.length;i++)
	{
		if(ck[i].split("=")[0]==nam)
		{
			return ck[i].split("=")[1];
		}
	}
	return false;
}
/*----------===============cookie functions end===============----------*/


/*----------===============load image start ===============----------*/
function getsolution() /* get maxwidth and maxheigh of current document */
{
	var t = document.documentElement.scrollHeight;
	var s = document.documentElement.clientHeight;
	var w1 = document.documentElement.scrollWidth;
	var w2 = document.documentElement.offsetWidth;
	var h = parseInt((t>s)?t:s);
	var w = parseInt((w1>w2)?w1:w2);
	return(w + "=" + h);
}

function showpic(str)
{
	if ($("picbg") == null)
	{
		var picbgs = document.createElement("div");
		var picdiv = picbgs.cloneNode(false);
		var img = document.createElement("img");
		picbgs.id = "picbg";
		picdiv.id = "picsdiv";
		img.id = "picimg";
		picdiv.appendChild(img);
		document.body.appendChild(picbgs);
		document.body.appendChild(picdiv);

	}

	var bgs = $("picbg");
	var pic = $("picsdiv");
	var pimg = $("picimg");
	bgs.style.display = "block";
	pic.style.display = "block";
	resize();
	window.onresize = resize;
	if(window.addEventListener)
	{
		pimg.addEventListener("DOMMouseScroll",wheel,false);
	}
	else
	{
		pimg.onmousewheel = wheel;
	}
	pimg.src = str;
	pimg.onclick = function()
	{
		$("picbg").style.display = "none";
		$("picsdiv").style.display = "none";
		pimg.src = "";
	}
}

function resize()
{
	var bgs = $("picbg");
	var pic = $("picsdiv");
	document.body.style.margin = "0px";
	bgs.style.position = "absolute";
	bgs.style.zIndex = "1";
	pic.style.position = "absolute";
	pic.style.zIndex = "10";
	bgs.style.left = "0px";
	bgs.style.top = "0px";
	bgs.style.width = "100%";//getsolution().split("=")[0] + "px";
	bgs.style.height = getsolution().split("=")[1] + "px";
	bgs.style.border = "none";
	bgs.style.backgroundColor = "gray";
	bgs.style.opacity = "0.4";
	bgs.style.filter = "alpha(opacity=40)";
	pic.style.width = "80%";
	pic.style.left = "10%";
	pic.style.top = "15%";
	pic.style.border = "1px solid white";
	pic.style.textAlign = "center";
}

function wheel(evt)
{
	evt = evt||event;
	var delta = 0;
	if (evt.wheelDelta) /* IE/Opera. */
	{	
		delta = evt.wheelDelta;
	} 
	else if (evt.detail)
	{ /** Mozilla case. */
		delta = -evt.detail;
	}
	
	if (delta) handle(delta);

	if (evt.preventDefault)	evt.preventDefault();
	evt.returnValue = false;
}
function handle(boo)
{
	if(parseInt(boo)>0)
	{
		$("picimg").width *= 1+0.2; 
	}
	else
	{
		$("picimg").width *= 1-0.2; 
	}
}
/*----------===============load image end ===============----------*/

/*----------===============popup start ===============----------*/
var _pop_obj = {"Title":"","Content":"","OK_Text":"","Cancel_Text":"","OK_Function":"","Cancle_Function":"","Styles":[1,1,1,1,1,1]};
var _pop_img = {"info":"info.png","question":"question.png","warning":"warning.png","error":"error.png"};
var _pop_select = new Array();
var inited = 0;

function Popup()
{
	this.Title = "Hello World" ;
	this.Content = "Initialization of Popup !";
	this.MsgType = "info";
	this.OK_Text = "OK" ;
	this.Cancel_Text = "";
	this.OK_Function = "";
	this.Cancel_Function = "";
	this.Display = setVisible ;
	this.setStyle = setStyles;
	this.Keep = keepContent;
	createBgDiv();
	remSelect();
}

function createBgDiv()
{
	if($("popbg") != null || $("popbg") != undefined) return;
	var _pop_bg = document.createElement("div");
	var _pop_bdr = document.createElement("div");
	var _pop_tle = _pop_bdr.cloneNode(false);	// document.createElement("div");
	var _pop_con = _pop_bdr.cloneNode(false);	// document.createElement("div");
	var _pop_con_text = _pop_con.cloneNode(false);	// document.createElement("div");
	var _pop_con_btn = _pop_con.cloneNode(false);	// document.createElement("div");
	
	_pop_bg.id = "popbg";
	_pop_bdr.id = "popborder";
	_pop_tle.id = "poptitle";
	_pop_con.id = "popcontain";
	_pop_con_text.id = "popcontent";
	_pop_con_btn.id = "popbtns";

	_pop_bg.className = "bg_0";
	_pop_bdr.className = "border_0";
	_pop_tle.className = "title_0";
	_pop_con.className = "contain_0";
	_pop_con_text.className = "content_0";
	_pop_con_btn.className ="btn_0";

	_pop_con.appendChild(_pop_con_text);
	_pop_con.appendChild(_pop_con_btn);
	_pop_bdr.appendChild(_pop_tle);
	_pop_bdr.appendChild(_pop_con);
 
	document.body.appendChild(_pop_bg);
	document.body.appendChild(_pop_bdr);

	$("popbg").style.display = "none";
	$("popborder").style.display = "none";
}


function setVisible(boo)
{
	if(inited == 0)	this.Keep();
	var ori = parseInt(boo);
	if($("popbg") == null || $("popbg") == undefined) createBgDiv();
	var visible = (ori==1)?"block":"none";
	var selectState = (ori ==1)?"hidden":"visible";
	for(var i=0; i<_pop_select.length;i++)
	{
		$(_pop_select[i]).style.visibility = selectState;
	}
	var btns = '<button type="button" onclick="javascript:'+_pop_obj.OK_Function + ';setVisible(0);void(0);">'+_pop_obj.OK_Text + '</button>';
	if(_pop_obj.Cancel_Text != "")	btns += '<button type="button" onclick="javascript:'+_pop_obj.Cancel_Function + ';setVisible(0);void(0);">'+_pop_obj.Cancel_Text + '</button>';
	var img = (this.MsgType==undefined)?"info":this.MsgType;
	var tleimg = img;
	img = '<img src="images/' + _pop_img[img] + '" align="left" width="45" height="45" />';

	if(/MSIE (5|6)\.\d?/.test(window.navigator.userAgent))
	{
		var t = document.documentElement.scrollHeight;
		var s = document.documentElement.clientHeight;
		$("popbg").style.height = parseInt((t>s)?t:s) + "px";
	}
	 
	$("poptitle").innerHTML = _pop_obj.Title;
	$("popcontent").innerHTML = img + _pop_obj.Content;
	$("popbtns").innerHTML = btns;
	$("popbg").style.display = visible;
	$("popborder").style.display = visible;
	if(ori == 1)
	{
		addevent($("poptitle"),"dblclick",closeit);
		addevent($("popborder"),"contextmenu",rfalse);
		addevent($("popbg"),"contextmenu",rfalse);
		addevent($("popbtns"),"contextmenu",rfalse);
		addevent($("popcontent"),"contextmenu",rfalse);
		addevent($("popcontain"),"contextmenu",rfalse);
		addevent($("poptitle"),"selectstart",rfalse);
		addevent($("popborder"),"selectstart",rfalse);
		addevent($("popbg"),"selectstart",rfalse);
		addevent($("popbtns"),"selectstart",rfalse);
		addevent($("popcontent"),"selectstart",rfalse);
		addevent($("popcontain"),"selectstart",rfalse);
		addevent($("poptitle"),"mousedown",drag);
		addevent($("poptitle"),"mouseup",dragover);
		addevent(document,"mousemove",loc);
		addevent($("popbg"),"click",btnfocus);
	}
	pngfix();
}

function keepContent()
{
		_pop_obj.Title = (this.Title == undefined || this.Title == "")? _pop_obj.Title:this.Title;
		_pop_obj.Content = (this.Content == undefined || this.Content == "")?_pop_obj.Content:this.Content;
		_pop_obj.OK_Text = (this.OK_Text == undefined || this.OK_Text == "")?_pop_obj.OK_Text:this.OK_Text;
		_pop_obj.OK_Function =(this.OK_Function == undefined || this.OK_Function == "")? _pop_obj.OK_Function:this.OK_Function;
		_pop_obj.Cancel_Text =(this.Cancel_Text == undefined || this.Cancel_Text == "")?_pop_obj.Cancel_Text:this.Cancel_Text;
		_pop_obj.Cancel_Function =(this.Cancel_Function == undefined || this.Cancel_Function == "")? _pop_obj.Cancel_Function:this.Cancel_Function;
		inited = 1;
}

function setStyles()
{
	var args = new Array();
	var laststyle = 1;
	for(var i=0;i<6; i++)
	{
		if(arguments[i] != undefined)
		{
			args[i] = arguments[i];
			laststyle = args[i];
		}else
		{
			args[i] = laststyle;
		}
	}
	//alert(args);
	$("popbg").className = "bg_" + args[0] ;
	$("popborder").className = "border_" + args[1] ;
	$("poptitle").className = "title_" + args[2] ;
	$("popcontain").className = "contain_" + args[3];
	$("popcontent").className = "content_" + args[4];
	$("popbtns").className = "btn_" + args[5];
}


function remSelect()
{
	if(/MSIE (5|6)\.\d?/.test(window.navigator.userAgent))
	{
		var d = document.getElementsByTagName("select");
		var j = 0;
		for(var i=0; i<d.length; i++)
		{
			if(d[i].style.visibility == "hidden" || d[i].style.display == "none")
			{
				continue;
			}else
			{
				if(d[i].id == undefined || d[i].id==null || d[i].id =="") d[i].id = "_pop_hidden_" + j;
				_pop_select.push(d[i].id);
				j++;
			}
		}
	}
}

function pngfix()
{
	if(document.images && /MSIE (5|6)\.\d?/.test(window.navigator.userAgent))
	{
		var d = document.images;
		for(var i=0;i<d.length;i++)
		{
			if(/.+\.png$/.test(d[i].src))
			{
				d[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + d[i].src + "',sizingMethod='scale')";
				d[i].src="images/blank.gif";
			}
		}
	}
}

function rfalse()
{
	return false;
}

function closeit()
{
	setVisible(0);
}

function btnfocus()
{
	$("popbtns").getElementsByTagName("button")[0].focus();
}

var obj=0; 
var mx=0; 
var my=0;
var ly = 10;
var draging = 0;

function drag(evt)
{
	draging = 1;
}
function dragover()
{
	draging = 0;
}
function loc(evt)
{
	evt = evt || event;
	mx = evt.clientX;
	my = evt.clientY;
	if(/MSIE (5|6)\.\d?/.test(window.navigator.userAgent))
	{
		var sh = $("popborder").scrollTop;
		var sw = $("popborder").clientLeft;
	}
	else
	{
		var sh = $("popborder").offsetTop;
		var sw = $("popborder").offsetLeft;
	}
	
	if(draging == 1)
	{
		$("popborder").style.left = mx -15 +  "px";
		$("popborder").style.top = my - 15 + "px";
	}
}

/*Usage of Popup-----
	var p = new Popup();
	p.Title = "Title Content here"
	p.Content = "Content is here";
	p.MsgType = tp=(info/warning/question/error);
	p.OK_Text = "½T©w";
	p.OK_Function  = "yourokfunction()";
	p.Cancel_Text = "¨ú®ø";
	p.Cancel_Function = "yourcancelfunction()";
	p.setStyle(0);
	p.Display(1);

	*to hidden the popup ,using setVisible(0);
	*keep the popup.css in css/popup.css and let the images in images folder
*/
/*----------=============== popup end ===============----------*/

function _import(tp,file)
{
	if((tp == "js"||tp=="css") &&file.length>0)
	{
		if(document.createElement)
		{
			switch(tp)
			{
				case "js":
					var d = document.createElement("script");
					d.type = "text/javascript";
					d.language = "javascript";
					d.src = file + ".js";
					document.appendChild(d);
					loaded = true;
					break;
				case "css":
					var d = document.createElement("link");
					d.type = "text/css";
					d.href = file + ".css";
					document.appendChild(d);
					break;
				default:
					break;
			}
		}
		else
		{
			/*alert("no scripts file is imported!");*/
			return;
		}
	}
}
