var validate = {
	errcss : "errorcontrol",
	noterrcss : "",
	highlightcolor : 'red',
	valEmpty : function(txtValid,spanErr,errMsg) {
 		if(jQuery.trim($('#'+txtValid).val()) == ''){
 			if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 			$('#'+txtValid).attr('class',this.errcss);
 			return false;
 		}else{
 			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 			$('#'+txtValid).attr('class',this.noterrcss);
 			return true;
 		}
	},
	valEmail:function(txtValid,spanErr,errMsg) {
		var isnoterror = true;
		validRegExp = /\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i;
    	if ($('#'+txtValid).val().search(validRegExp) == -1){isnoterror =  false;}
 		if (isnoterror){
			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 			$('#'+txtValid).attr('class',this.noterrcss);
			return true;
		}else{
			if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 			$('#'+txtValid).attr('class',this.errcss);
			return false;
		}					
	},
	valSelect:function(txtValid,spanErr,errMsg) {
 		if(jQuery.trim($('#'+txtValid).val()) == '0'){
 			if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 			$('#'+txtValid).attr('class',this.errcss);
 			return false;
 		}else{
 			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 			$('#'+txtValid).attr('class',this.noterrcss);
 		}
	 },
	 valNumeric : function(txtValid,spanErr,errMsg){
    	var value = $('#'+txtValid).val();
      	if (!(jQuery.trim(value) == '')){
	      	if (value.match(/^\d+$|^\d+\.\d{2}$/ )){
				if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 				$('#'+txtValid).attr('class',this.noterrcss);
			}else{
				if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 				$('#'+txtValid).attr('class',this.errcss);
				return false;
			}
      	}else{
  			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 			$('#'+txtValid).attr('class',this.noterrcss);
      	}
    },
    valDate : function(txtValid,spanErr,errMsg){
		var value = $('#'+txtValid).val();
	 	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
 		var isnoterr = false;
		if (!(jQuery.trim(value) == '')){
			if(!objRegExp.test(value)){isnoterr = false;}
			else {
				var strSeparator = value.substring(2,3); 
			    var arrayDate = value.split(strSeparator); 
			    var arrayLookup = { '01' : 31,'03' : 31, 
			                        '04' : 30,'05' : 31,
			                        '06' : 30,'07' : 31,
			                        '08' : 31,'09' : 30,
			                        '10' : 31,'11' : 30,'12' : 31};
			    var intDay = parseInt(arrayDate[0],10); 
			    if(arrayLookup[arrayDate[1]] != null) {
			    	if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0){isnoterr =  true;}
			    }
			    var intMonth = parseInt(arrayDate[1],10);
			    if (intMonth == 2) { 
			        var intYear = parseInt(arrayDate[2]);
			        if (intDay > 0 && intDay < 29) {isnoterr =  true;}
			        else if (intDay == 29) {
			        	if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0)) {isnoterr = true;}   
			        }
				}
			}  
	      	if (isnoterr){
				if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 				$('#'+txtValid).attr('class',this.noterrcss);
			}else{
				if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 				$('#'+txtValid).attr('class',this.errcss);
				return false;
			}
	   	}else{
  			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 			$('#'+txtValid).attr('class',this.noterrcss);
		}
	},
	valPassword : function(txtValid,spanErr,txtConfirm,spanErrConfirm) {
	 	var passwd = $('#'+txtValid).val();
	 	var errorpass; 
	    var strVerdict = "weak";
	 	var intScore = 0;
	 	//var Log = "";
	    if (passwd.length < 17){
		    if (passwd.length < 1) {}
		    else if (passwd.length < 6) {} 
		    else if (passwd.length > 5 && passwd.length < 10) {intScore = intScore + 6;} 
		    else if (passwd.length > 9 && passwd.length < 17) {intScore = intScore + 16;}
		    
		    if (passwd.match(/[a-z]/)) {intScore = intScore + 2; }
		    if (passwd.match(/[A-Z]/)) {intScore = intScore + 5; }
		    if (passwd.match(/\d+/)) {intScore = intScore + 5; }
			if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/)) {intScore = intScore + 5; }
			if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) {intScore = intScore + 5; }
			if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) {intScore = intScore + 5; }
			if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {intScore = intScore + 2; }
			if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) {intScore = intScore + 2;}
			if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) {intScore += intScore + 2;}
	    }
		if (passwd.length < 6 ){errorpass = "Password should more than 6 characters.";}
		else if(passwd.length > 16 ){errorpass = "Password should less than 16 characters.";}
		else {errorpass = "";}
		
		if (passwd.match(/[\s,\',\"]/)){errorpass = "Illegal Character";}
		
		if (intScore < 15) {
			strVerdict = "weak"; 
			$('#'+spanErr).css({color: "white",backgroundColor: "red"});
		} 
		else if (intScore > 14 && intScore < 35) {strVerdict = "medium"; $('#'+spanErr).css({color: "white",backgroundColor: "yellow"});} 
		else {strVerdict = "strong"; $('#'+spanErr).css({color: "white",backgroundColor: "green"});}
	
	
		if($('#'+spanErr).length > 0){$('#'+spanErr).html(strVerdict + ' ' + errorpass);}
	 	if (!jQuery.trim($('#'+txtConfirm).val()) == ''){this.confirmpass(txtValid,spanErr,txtConfirm,spanErrConfirm);}
	 },
	 valChecked : function(txtValid,spanErr,errMsg){
	 	if(mform.arrChecked(txtValid).length > 0){
 			if($('#'+spanErr).length > 0){$('#'+spanErr).html('');}
 			$('#'+txtValid).attr('class',this.noterrcss);
 		}else{
 			if($('#'+spanErr).length > 0){$('#'+spanErr).html(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 			$('#'+txtValid).attr('class',this.errcss);
 			return false;
 		}
	 },
	 confirmpass : function(txtValid,txtConfirm,spanErr,errMsg) {
	 	var passwd = $('#'+txtValid).val();
	 	var firmpass = $('#'+txtConfirm).val();
	 	var isnoterror = true;
	 	
	 	if (passwd != firmpass){
	 		$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);
	 		$('#'+txtConfirm).attr('class',this.errcss);
	 		isnoterror = false;
	 	}else{
	 		$('#'+spanErr).text();
	 		$('#'+txtConfirm).attr('class',this.noterrcss);
	 	}
	 	return isnoterror;	
	 },
	 compareDate:function(firstDate,SecondDate,spanErr,errMsg){
		var str1 = $('#'+firstDate).val();
		var str2 = $('#'+SecondDate).val();
   		if (!(str1 == '') && !(str2== '')){
	   		var dt1   = parseInt(str1.substring(0,2),10);
	   		var mon1  = parseInt(str1.substring(3,5),10);
	   		var yr1   = parseInt(str1.substring(6,10),10);
	   		var dt2   = parseInt(str2.substring(0,2),10);
	   		var mon2  = parseInt(str2.substring(3,5),10);
	   		var yr2   = parseInt(str2.substring(6,10),10);
	   		var date1 = new Date(yr1, mon1, dt1);
	   		var date2 = new Date(yr2, mon2, dt2); 
			
		    if(date2 < date1){
	   			if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 				$('#'+SecondDate).attr('class',this.errcss);
				return false;
	   		}else{
	   			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 				$('#'+SecondDate).attr('class',this.noterrcss);
			}
   		}else{
   			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 			$('#'+SecondDate).attr('class',this.noterrcss);
   		}
	},
	compareTime : function(firstTime,secondTime,spanErr,errMsg){
   		if (!($('#'+firstTime).val() == '') && !($('#'+secondTime).val() == '')){
	   		var h1   = parseInt(firstTime.substring(0,2),10);
	   		var m1  = parseInt(firstTime.substring(3,5),10);
	   		var h2   = parseInt(secondTime.substring(0,2),10);
	   		var m2  = parseInt(secondTime.substring(3,5),10);
	   		var time1 = new Date(0,0,0,h1,m1);
	   		var time2 = new Date(0,0,0,h2,m2); 
			
		    if(time2 <= time1){
	   			if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
	   			$('#'+firstTime).attr('class',this.errcss);
	   			$('#'+secondTime).attr('class',this.errcss);
				return false;
	   		}else{
	   			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
	   			$('#'+firstTime).attr('class',this.noterrcss);
	   			$('#'+secondTime).attr('class',this.noterrcss);
			}
   		}else{
   			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
   			$('#'+firstTime).attr('class',this.noterrcss);
	   		$('#'+secondTime).attr('class',this.noterrcss);
   		}
	},
	compareNumeric : function(comparetype,firstvalue,secondvalue,spanErr,errMsg){
		var isnoterr = true;
		var str1  = $('#'+firstvalue).val();
   		var str2  = $('#'+secondvalue).val();
		switch (comparetype) {
			case 'lessThan':
				if(str1 < str2){}else{isnoterr = false;}
				break;
			case 'lessThanOrEqual':
				if(str1 <= str2){}else{isnoterr = false;}
				break;
			case 'greaterThan':
				if(str1 > str2){}else{isnoterr = false;}
				break;
			case 'greaterThanOrEqual':
				if(str1 >= str2){}else{isnoterr = false;}
				break;
			case 'equal':
				if(str1 == str2){}else{isnoterr = false;}
				break;
			case 'notEqual':
				if(str1 != str2){}else{isnoterr = false;}
				break;						
			default:
				isnoterr = false;
				break;
		}
		if(isnoterr){
			if($('#'+spanErr).length > 0){$('#'+spanErr).text('');}
 			$('#'+secondvalue).attr('class',this.noterrcss);
		}else{
			if($('#'+spanErr).length > 0){$('#'+spanErr).text(errMsg).effect("highlight", {color: this.highlightcolor}, 1000);}
 			$('#'+secondvalue).attr('class',this.errcss);
 			return false;
		}
	},highlightfield:function(control){
		$('#'+control).effect("highlight", {color: this.highlightcolor}, 1000);
	}
};

var popup = {
	divpopup:'',
	cover:'.popup',
	pagesize:'',
	showCover :function (){
		$(this.cover).css("display","block");
		$(this.cover).css("height",this.pagesize[0]+'px');
		$(this.cover).fadeTo(600,0.7);
	},
	hideCover:function(){
		$(this.cover).fadeTo(600,0.0);
		setTimeout('$("'+this.cover+'").css("display","none")',660);
	},	
	showpopup:function(divpopup){
		this.divpopup = divpopup;
		this.pagesize = this.getPageSize();
		this.showCover();
		var dim = this.windowPosition();
		$(this.divpopup).css("top",dim[0]);
		$(this.divpopup).css("left",dim[1]);
//		$(this.divpopup).css("display","block");
		$(this.divpopup).slideDown("slow");
//		$(this.divpopup).draggable({cursor:"move",handle:".poptop"});
	},
	changePosition:function(){
		this.pagesize = this.getPageSize();
		var dim = this.windowPosition();
		$(this.divpopup).animate({top:dim[0],left:dim[1]},0);
	},
	windowPosition:function(){
		var wtop,wleft;
		wtop = ((this.pagesize[3]  - $(this.divpopup).height())/2) + "px";
		wleft = ((this.pagesize[0] - $(this.divpopup).width())/2) + "px";
		arrayw = new Array(wtop,wleft);
		return arrayw;
	},
	closepopup:function(){
		$(this.divpopup).slideUp("slow");
//		$(this.divpopup).draggable("destroy");
		this.hideCover();
	},
	getPageSize :function(){
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {xScroll = document.body.scrollWidth;yScroll = window.innerHeight + window.scrollMaxY;} 
		else if (document.body.scrollHeight > document.body.offsetHeight){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} 
		else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {windowWidth = self.innerWidth;windowHeight = self.innerHeight;} 
		else if (document.documentElement && document.documentElement.clientHeight) {windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} 
		else if (document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;}
			
		if(yScroll < windowHeight){pageHeight = windowHeight;} else {pageHeight = yScroll;}
		if(xScroll < windowWidth){pageWidth = windowWidth;} else {pageWidth = xScroll;}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 
		return arrayPageSize;
	},
	fileManagerPopup:function(control){
		window.open('/class/filemanager/index.php?id='+control, '', 'status=0,toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,copyhistory=0,resizable=0');
	}
};

var mform = {
	clearForm:function(formIdent){
		$("#"+formIdent+" textarea").val('').attr("class",validate.noterrcss);
		$("#"+formIdent+" :text").val('').attr("class",validate.noterrcss);
		$("#"+formIdent+" :password").val('').attr("class",validate.noterrcss);
		$("#"+formIdent+" :file").val('').attr("class",validate.noterrcss);
		$("#"+formIdent+" :hidden").val('');
		$("#"+formIdent+"  select").attr("selectedIndex",0).attr("class",validate.noterrcss);
		$("#"+formIdent+" :checkbox").attr("checked",'');
		$("#"+formIdent+" :radio").attr("checked",'');
		$("#"+formIdent+" em[id][id^='err']").html('');
	},	
	checkall:function(control,chkGroup){if (control.checked){$(":checkbox[name='"+chkGroup+"']").attr('checked','checked');}else{$(":checkbox[name='"+chkGroup+"']").attr('checked','');}},
	updateTiny:function(strControl){tinyMCE.get(strControl).execCommand('mceSetContent',false,$('#'+strControl).val());},
	arrChecked:function(strControl){
		var arrCheck=new Array();
		$(":checked[name = '"+strControl+"']").each(function(){arrCheck.push($(this).val());});return arrCheck;}	
};

var format = {
	currency:function(num,dec) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') +  num + ((dec)? '.' + cents: ''));
	}
};	

var cookieManager = {
	createCookie : function (name,value,days){
		if (days){
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie:function (name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++){
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	eraseCookie : function (name){createCookie(name,"",-1);}
};
function debug(value){$('#debug').append(value + '<br/>');}

