﻿var make="<button onclick='winClose();');' class='button' onmouseover=this.className='buttonH' onmouseout=this.className='button'>OK</button>"
var cancel="<button onclick='javascript:breaked=true;winClose();');' class='button' onmouseover=this.className='buttonH' onmouseout=this.className='button'>Cancel</button>"

function $(id){return document.getElementById(id);}

//validator
String.prototype.trim = function(){
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}

//-----check number-----------
function isNumber(num){
    if( /^[0-9.]+$/.test( num ))
		return true;
	else return false;
}

//------check email------------
function isEmail(email){
    return ( /^[^@]+@[^@]+\.[^@]+$/.test(email) && email.length<128 );
}

//-------check value-----------
function isValue(val){
	if(val==null || val=="")
		return false;
	else
		return true;
}

//--------format number---------
function formatnumber(value,num){
	var a_int=parseInt(value*Math.pow(10,(num+1)));
	if (a_int==0)
	return "0.00";
	
	var a_str=a_int.toString();
	
	if (a_str.substring(0,1)!="-") // positive number
	{
		var b_int=parseInt(a_str.substring(a_str.length-1,a_str.length));
		if (b_int>=5)
			a_int=a_int+10;
	}
	else// negative number
	{
		var b_int=parseInt(a_str.substring(a_str.length-1,a_str.length));
		if (b_int>=5)
			a_int=a_int-10;
	}
	
	a_str=a_int.toString();
	
	var l_str = a_str.substring(0, a_str.length-(num+1));
	var r_str = a_str.substring(a_str.length-num-1, a_str.length);
	r_str = r_str.substring(0,num);
	var c_str = l_str + "." + r_str;
	
	
	if (value.toString().substring(0,3)=="-0.")
		c_str="-0."+r_str;
	if(value.toString().substring(0,2)=="0.")
		c_str="0."+r_str;
	
	return c_str;
}

//------direct url----------
function goURL(url){
	window.location.href = url;	
}

//-----open url-------------
function openWin(url){
	window.open(url,'','width=480,height=300,toolbar=no, status=no, menubar=no, resizable=yes, scrollbars=yes, left=20,top=20');	
}

//------history back---------
function goBack(){
	history.back();	
}


//--------ajax window---------------
function operate(url, form){
	getPage(url,'content',0,form);
	win_loading();
}

//----------wait-------------
function win_loading(){
	winNew('Note','<p class=iLoad>Loading, please wait.</p>',0,400,94);
}
	
//check select
function chSelect(name,value,note){
	var oProm = $(note);
	if( value == null || value == "" || value.trim() == "" || value == 0) {  //null value
		showNote(oProm, "Please select a ["+name+"]",1);
    	return false;
	}else{
		showNote(oProm, "done",0);
		return true;
	}
}

//(name, value, note, null(0-not null, 1-null), type(0-all, 1-num, 2-letter, 3-letter and num, 4-begig with letter,5-email, 6-mobile phone), min length, max length, min value, max value)
function check(name,value,note,isnull,type,minL,maxL,minN,maxN){
	var oProm = $(note);
	if( value == null || value == "" || value.trim() == "") {  //null value
    	if( isnull==0 ){
			showNote(oProm, "["+name+"] is required.",1);
    		return false;
		}else{
			return true;
		}
    }else{  //value not empty
		if( type==1 && !isNumber(value) ){  //numeral
			showNote(oProm,"["+name+"] must be numercial",1);
			return false;
		}else if( type==2 && ! /^[a-zA-Z]+$/.test(value) ){  //numeral
			showNote(oProm,"["+name+"] must be letter",1);
			return false;
		}else if( type==3 && ! /^[\w]+$/.test(value) ){ //letters and numbers
			showNote(oProm,"["+name+"], only letters (a-z), numbers (0-9), and ( - ), ( _ ), ( . ) are allowed.",1);
    		return false;
		}else if( type==4 && ( ! /^[a-zA-Z].*$/.test(value) || ! /^[\w]+$/.test(value) ) ){  //
			showNote(oProm,"["+name+"], only letters (a-z), numbers (0-9), and ( - ), ( _ ), ( . ) are allowed.",1);
    		return false;
		}else if( type==5){  //more email
			var arr=value.split(","); 
			for (var i=0; i<arr.length; i++) { 
				if (!isEmail(arr[i])){
					showNote(oProm,"Invalid ["+name+"]",1);
					return false;
				}
			}			
		}else if( type==6 && !isMobile(value) ){  //mobile phone
			showNote(oProm,"Invalid ["+name+"]",1);
			return false;
		}else if( type==7){  //more user
			var arr=value.split(","); 
			for (var i=0; i<arr.length; i++) { 
				if (! /^[a-zA-Z].*$/.test(arr[i]) || ! /^[\w]+$/.test(arr[i])){
					showNote(oProm,"Invalid ["+name+"]",1);
					return false;
				}
			}	
		}
		
    	if( minL!=null && maxL!=null && minL==maxL && value.length!=minL ){
    		showNote(oProm, name+"is invalid, sholud be "+minL+" characters",1);
    		return false;
		}
		if( minL!=null && value.length < minL ){  //min length
    		showNote(oProm, name+" can't less then "+minL+" characters",1);
    		return false;
		}
    	if( maxL!=null && value.length > maxL ){  //max lenght
    		showNote(oProm, name+" can't more then "+maxL+" characters",1);
    		return false;
		}

    	if( type==1 && minN!=null && value < minN ){  //max
    		showNote(oProm, "Invalid "+name+", must not less than "+minN,1);
    		return false;
		}
    	if( type==1 && maxN!=null && value > maxN ){  //max
    		showNote(oProm, "Invalid "+name+", must not large than "+maxN,1);
    		return false;
		}
/*
    	if( value.indexOf( "%" ) > -1 || value.indexOf( "&" ) > -1 ) {  //filter letter
    		showNote(oProm, "Invalid "+name+", can not contain '%' or '&'",1);
    		return false;
    	}
*/
		showNote(oProm, "done",0);
		return true;
	}	
}


function checkCode(name,value,note){  //get verify code
	var oProm = $(note);
	if( value == null || value == "" || value.trim() == ""){
		showNote(oProm, "["+name+"] is required.",1);
    	return false;
	}
	var xmlhttp;
	if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	
	if (xmlhttp) {
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				if(xmlhttp.status==200){
					var passcode=unescape(xmlhttp.responseText);
					if( passcode==value ){
						showNote(oProm, "done",0);
    					return true;
					}else{
						showNote(oProm, "error",1);
    					return false;
					}
				}else{
					showNote(oProm,"Verification don't work, but you can continue.",0);
				}
			}else{
				showNote(oProm,"verifing, please wait.........",0);
			}
		}
    	xmlhttp.open("get","/ajax.asp?action=vcode",true);
		xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		xmlhttp.setRequestHeader("If-Modified-Since","0"); 
		xmlhttp.send(null);
	}
	return true;
}


function showNote(obj,iMsg,status){  //show meg
	if (typeof obj == "string") {
		obj = $(obj);
	}
	if(status==1){
		obj.style.color = "#ff0000";
		obj.innerHTML = "<img src='/images/window/err.gif' align='absmiddle' /> "+iMsg;
	}else{
	    obj.style.color = "#0e9e26";
		obj.innerHTML = "<img src='/images/window/suc.gif' align='absmiddle' /> "+iMsg;
	}
}

//ajax form
function getPage(url,id,iswin,form){
	var xmlhttp;
	if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	
	if (xmlhttp) {
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				if(xmlhttp.status==200){
					var pageContent=unescape(xmlhttp.responseText);
					if (pageContent.indexOf("iNote")!=-1){
						winNew('Note Information',pageContent,0,400);
					}else if(pageContent.indexOf("iSuc")!=-1){
						winNew('Success Information',pageContent,0,400);
					}else if(pageContent.indexOf("iErr")!=-1){
						winNew('Error Information',pageContent,1,400);
					}else{
						winClose();
						$(id).innerHTML=pageContent;
					}
				}else{
					var loadErr="<p class='iNote'>Loaded failed, please try again after a moment.</p><p class='tRight'>"+make+"</p>";
					winNew('Loaded Failed',loadErr,1,400);
				}
			}else{
				if (iswin!=null && iswin==1){
					var loading="<p class='iLoad'>Data loading, Please wariting...</p><p class='tRight'>"+make+"</p>";
					winNew('Data loading',loading,0,400,94);
				}
			}
		}
		if (form!=null){
			var method="post";
			var data=getForm(form);
		}else{
			var method="get";
			var data=null;
		}
    	xmlhttp.open(method,url,true);
		xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		xmlhttp.setRequestHeader("If-Modified-Since","0"); 
		xmlhttp.send(data);
	}
}

//get form data
function getForm(form) {      
	returnString = "";      
	formElements = document.forms[form].elements;      
	var first = true;     
//	alert(formElements.length);
	for (var i=0;i<formElements.length;i++) {      
		var e = formElements[i];
	//	alert(e.name);
		if(e.name == null || e.name==""){
			continue;
		}
	  
		if(e.type=="checkbox" || e.type == "radio"){      
			if(e.checked){//checkbox     
				if(first == true){
					first = false;
					returnString += e.name + "=" + encodeURIComponent(e.value);      
				}else{      
					returnString += "&" + e.name + "=" + encodeURIComponent(e.value);      
				}
			}
		}else if(e.type=="select-one"){
			e.value=e.options[e.selectedIndex].value;
			if(first == true){
				first = false;
				returnString += e.name + "=" + encodeURIComponent(e.value);      
			}else{      
				returnString += "&" + e.name + "=" + encodeURIComponent(e.value);      
			}//select-one
		}else if (e.type=="select-multiple"){
			for (var n=0;n<e.length;n++) {
				var op=e.options[n];
				if (op.selected) {
					if(first == true){
						first = false;
						returnString += e.name + "=" + encodeURIComponent(op.value);      
					}else{      
						returnString += "&" + e.name + "=" + encodeURIComponent(op.value);      
					}
				}
			}
		}else{
			if(first == true){
				first = false;
				returnString += e.name + "=" + encodeURIComponent(e.value);      
			}else{
				returnString += "&" + e.name + "=" + encodeURIComponent(e.value);      
			}
		}
		
    }
    return returnString;
}

/*minwin*/
function winNew(title,content,ismask,width,height){
	$("windows").style.display = "block";
	$("windows").innerHTML="";
	
	var mask=document.createElement("iframe"); 
	mask.setAttribute('id','mask'); 
	mask.style.position="absolute"; 
	mask.style.top="0"; 
	mask.style.left="0"; 
	mask.style.width=document.body.offsetWidth + "px"; 
	mask.style.height=document.body.offsetHeight-14 + "px"; 
	mask.style.zIndex = "10000";

	var minWins = document.createElement("div");
	minWins.setAttribute("id","minWin");
	minWins.style.position = "absolute"; 
	minWins.style.left = "50%";
	minWins.style.top = "50%"; 
	minWins.style.marginLeft = -width/2+"px";
	minWins.style.marginTop = -width/4+document.documentElement.scrollTop+"px";
	minWins.style.width = width + "px";
	minWins.style.zIndex = "10001"; 
	
	var winTitle = document.createElement("ul");
	winTitle.className="winTitle";
	var winLeft = document.createElement("li");
	winLeft.className="winLeft";
	var winMid = document.createElement("li");
	winMid.innerHTML=title;
	winMid.className="winMid";
	winMid.style.width = width-24 + "px";
	var winClose = document.createElement("li");
	winClose.className="winClose";
	var winCloseA = document.createElement("a");
	winCloseA.title = "Close";
	winCloseA.setAttribute("href","javascript:winClose();");
	winCloseA.setAttribute("hideFocus","hideFocus")
	winCloseA.onclick=function(){$("windows").style.display = "none";$("windows").innerHTML="";}
	var winRight = document.createElement("li");	
	winRight.className="winRight";
	
	var winContent = document.createElement("ul");
	winContent.className = "winContent";
	winContent.style.width = width-2 + "px";
	if(height!=null){winContent.style.height = height+ "px";}
	winContent.innerHTML="<li>"+content+"</li>";
	
	winClose.appendChild(winCloseA);
	winTitle.appendChild(winLeft);
	winTitle.appendChild(winMid);
	winTitle.appendChild(winClose);
	winTitle.appendChild(winRight);
	minWins.appendChild(winTitle);
	minWins.appendChild(winContent);
	
	document.getElementById("windows").appendChild(mask);
	document.getElementById("windows").appendChild(minWins);
	if(ismask==0){
		mask.style.background="#EFEFEF"; 
		mask.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=0,finishOpacity=10)"; 
		mask.style.opacity="0";
	}else{
		mask.style.background="#666666"; 
		mask.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=30,finishOpacity=10)"; 
		mask.style.opacity="0.3";
	}
}

function winClose(){
	$("windows").style.display = "none";
	$("windows").innerHTML="";
}

function winBack(){
	$("windows").style.display = "none";
	$("windows").innerHTML="";
	window.history.back();
}
function winRefresh(){
	$("windows").style.display = "none";
	$("windows").innerHTML="";	
	window.self.location.reload();	
}

//--------------------
function fEvent(sType,oInput){
switch (sType){
	case "focus" :
		oInput.isfocus = true;
		oInput.style.backgroundColor='#FFFFD8';
	case "mouseover" :
		oInput.style.borderColor = '#99E300';
		break;
	case "blur" :
		oInput.isfocus = false;
		oInput.style.backgroundColor="";
	case "mouseout" :
		if(!oInput.isfocus){
		oInput.style.borderColor='';
		}
		break;
	}
}

function logout(){
	if(confirm('Really logout?')){
		location.href='/login.asp?action=out';	
	}
}

// change color
function chgColor(e, p){
	var tr = $(p);
	if(e.checked)
		tr.style.backgroundColor='#FDE3F4';
	else
		tr.style.backgroundColor='';
}

function seleAll(f)
{
	var myform = document.getElementById(f);
	for (var i=0;i<myform.elements.length;i++){
    var e = myform.elements[i];
    if(e.name == 'selectID')       
	{
		e.checked = document.getElementById("selectAll").checked; 
		chgColor(e,'tr'+e.value);
	}
   }
}

//copy text
function copyTxt(id){
	var text = $(id).value;
	if(clipboardData.setData("text",text)){
		alert("Copy success!");
	}
	else{
		alert("Copy fail!");
	}
}