// JavaScript Document

/********** BASE FUNCTIONS ************/
function $(objid)
{
	var obj = document.getElementById(objid);
	return obj;
}
function $_$(objname)
{
	var obj = document.getElementsByTagName(objname);
	return obj;
}
function $_value(objid)
{
	var obj = $(objid);
	return obj.value;
}
/********** BASE FUNCTIONS ************/



/********** Validate Login Details ************/
//Main Validate Class ( PROCESS FORM SUBMIT )
function ObjAttribute()
{
	this.format_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;	//Format of Email
	
	this.format_hotmail = /^\w+([-+.]\w+)*@hotmail\.com$/;	//Format of Email
	
	this.format_phone = /^\d{3}-\d{3}-\d{4}(|-\d{1,6})$/;	//Format of USA Tel
	
	this.format_date = /^(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d$/;
	
	this.format_phoneB = /^\d{11}$/;	//Format of USA Tel
	
	this.format_phone2 = /^\d{1,6}$/;	//Format of USA Tel
	
	this.format_zip = /^\d{5}$/;	//zip format
	
	this.format_fax = /^\d{11}$/;
	
	this.format_year = /^\d{4}$/;	//year format
	
	this.username = "";
	
	this.password = "";
	
	this.errmsg = new Array();
	
	this.errorcolor = "#BB0000";
	this.font_color = "#FFFFFF";
	
	/***** main function validate start *****/
	this.validate = function(Objfrm,Objerr){
		if(Objfrm==""){
			return false;
		}
		
		this.errmsg.push('Sorry, we cannot complete your request.<br />Kindly provide us the missing or incorrect information enclosed below.<br />');	//default error message
		
		var len=Objfrm.elements.length;
		
		for(var i=0,errorTag=0;i<len;i++){ //cycle for all the elements
			var ObjEach = Objfrm.elements[i];
			with(Objfrm.elements[i]){
				
				var _require = getAttribute("require");
				var _datatype = getAttribute("datatype");
				
				if( this.CheckAttributeTrue(_datatype) ){ //check if this item needs validation
					
					switch(_datatype){
						case "Email":
						case "E-Mail":
							this.CheckEmail(Objfrm,_require,value,_datatype);
							continue;
						case "MSN":
							this.CheckEmail2(Objfrm,_require,value,_datatype);
							continue;
						case "Phone":
						case "Telephone":
						case "Cell Phone":
						case "Home Phone":
						case "Work Phone":
							this.CheckPhoneB(Objfrm,_require,value,_datatype);
							continue;
						case "Work Phone Ext.":
							this.CheckPhone2(Objfrm,_require,value,_datatype);
							continue;
						case "Zip":
						case "Property Zip":
						case "Business Zip":
							this.CheckZip(Objfrm,_require,value,_datatype);
							continue;
						case "Image":
							this.CheckURL(Objfrm,_require,value,_datatype);
							continue;
						case "Price":
							this.CheckPrice(Objfrm,_require,value,_datatype);
							continue;
						case "Quantity":
						case "D1 DL Number":
						case "D3 DL Number":
						case "Years Licensed":
						case "D1 Years Licensed":
						case "D2 Years Licensed":
						case "D3 Years Licensed":
							this.CheckNumeric(Objfrm,_require,value,_datatype);
							continue;
						case "Date of Birth":
						case "D1 Date of Birth":
						case "D2 Date of Birth":
						case "D3 Date of Birth":
						case "Expiration Date":
							this.CheckDate(Objfrm,_require,value,_datatype);
							continue;
						case "Fax":
							this.CheckFax(Objfrm,_require,value,_datatype);
							continue;
						case "V1 Year":
							this.CheckYear(Objfrm,_require,value,_datatype);
							continue;
						default:
							this.CheckNormalField(Objfrm,_require,value,_datatype);
							continue;
					}
					
				}else{ //did not validate
					continue;
				}
				
			}			
		}		
		
		if(!this.DisplayError(Objerr)){
			return false;
		}else{
			return true;
		}
		
	}
	/***** main function end *****/
	
	this.CheckAttributeTrue = function (Obj){
		if(Obj!=null && Obj!=false){
			return true;
		}else{
			return false;
		}
	}
	
	this.CheckValue = function (ObjValue){
		if(ObjValue==''){
			return false;
		}else{
			return true;
		}
	}
	
	/***** validate if required start *****/
	this.CheckNull = function (Objfrm,ObjRequire,ObjValue,DataType){
		if( this.CheckAttributeTrue(ObjRequire) ){
			if(ObjValue==''){
				this.errmsg.push("- <strong>"+DataType+"</strong> is required");
				return false;
			}else{
				return true;
			}
		}else{
			return true;
		}
	}
	/***** validate if required start *****/
	
	this.CheckNormalField = function (Objfrm,ObjRequire,ObjValue,DataType){
		this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType);
	}
		
	/***** validate email start *****/
	this.CheckEmail = function (Objfrm,ObjRequire,ObjValue,DataType){
		
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_email)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate email end *****/
	
	/***** validate email start *****/
	this.CheckEmail2 = function (Objfrm,ObjRequire,ObjValue,DataType){
		
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_hotmail)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate email end *****/
		
	/***** validate phone start *****/
	this.CheckPhone = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_phone)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	
	this.CheckPhoneB = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_phoneB)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	
	this.CheckPhone2 = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_phone2)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate phone end *****/
	
	/***** validate zip start *****/
	this.CheckZip = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_zip)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate zip end *****/
	
	/***** validate Fax start *****/
	this.CheckFax = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_fax)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate Fax end *****/
	
	/***** validate Year start *****/
	this.CheckYear = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_year)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}else if( parseInt(ObjValue) <1900 ){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}		
	}
	/***** validate Year end *****/
	
	/***** validate img extension ******/
	this.CheckEx = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			var imgArr = new Array( 'jpg', 'jpeg', 'gif', 'png' );
			var cmpD = imgArr.join(",");
			var cmpS = ObjValue.split(".");
			var extensionN = cmpS[cmpS.length-1];
			if( cmpD.match(extensionN) == null ){
				this.errmsg.push("- Upload <strong>"+ DataType +"</strong> Failed, the format of Image should be .jpg .jpeg .gif .png");
			}
		}
	}
	/***** validate img extension ******/
	
	/***** validate URL ******/
	this.CheckURL = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			var strUrl = /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i;
			if( !ObjValue.match(strUrl) ){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate img extension ******/
	
	/***** validate Price ******/
	this.CheckPrice = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			var strPrice = /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
			if( !ObjValue.match(strPrice) ){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate img extension ******/
	
	/***** validate date ******/
	this.CheckDate = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			if(!ObjValue.match(this.format_date)){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate date ******/
	
	/***** validate Number ******/
	this.CheckNumeric = function (Objfrm,ObjRequire,ObjValue,DataType){
		if(!this.CheckNull(Objfrm,ObjRequire,ObjValue,DataType)){
			return;
		}
		if(  this.CheckValue(ObjValue) )
		{
			var strNumeric = /^[1-9]{1}[0-9]*$/;
			if( !ObjValue.match(strNumeric) ){
				this.errmsg.push("- Please input the correct format of <strong>"+ DataType +"</strong>");
			}
		}
	}
	/***** validate Number ******/
	
	/******** Display Error Message *********/
	this.DisplayError = function (Objerr){
		
		if(this.errmsg.length!=1){
			Objerr.style.display="";
			$("errorMSG").innerHTML=this.errmsg.join("<br />");
			return false;
		}else{
			Objerr.style.display="none";
			return true;
		}
	}
	
}


function ValidateForm(objfrm)
{
	var obj=new ObjAttribute();
	var objErr=$("errorPosition");
	if(!obj.validate(objfrm,objErr)){
		return false;
	}else{
		return true;
	}
}
/********** Validate Login Details ************/






/************ Other Functions ************/


//check all
function checkall(objForm)
{
	var len = objForm.elements.length;
	for(var i=0;i<len;i++){
		if (objForm.elements[i].type=='checkbox'){
			objForm.elements[i].checked=objForm.check_all.checked;
		}
	}
}

//clear all the object in the form
function searchFormClear(objForm, selectStr)
{
	var len = objForm.elements.length;
	
	if( selectStr!="" ){
		selectArr = selectStr.split(",");
	}
	
	for(var i=0;i<len;i++){
		if(objForm.elements[i].type=='checkbox'){
			objForm.elements[i].checked = false;
		}
		if(objForm.elements[i].type=='text'){
			objForm.elements[i].value = "";
		}
		if(objForm.elements[i].type=='radio'){
			objForm.elements[i].checked = false;
		}
		if( selectStr!="" ){
			for(var q=0; q< selectArr.length; q++){
				if(objForm.elements[i].id == selectArr[q]){
					objForm.elements[i].value = "";
				}
			}
		}
	}
}

//if it is checked
function ifChecked(objForm)
{
	var len = objForm.elements.length;
	var checked_target=0;
	for(var i=0;i<len;i++){
		if((objForm.elements[i].type=='checkbox')&&(objForm.elements[i].disabled==false)&&(objForm.elements[i].checked==true)&&(objForm.elements[i].name!='check_all')){
			checked_target++;
		}
	}
	if(checked_target==0){
		return false;
	}else{
		return true;
	}
}

//delete form submit
function deleteFormSubmit(objfrm)
{
	var Objerr = $("errorPosition");	
	if(ifChecked(objfrm)){
		Objerr.style.display = "none";
		objfrm.submit();
		return true;
	}else{
		Objerr.style.display = "";
		var CellPosition = Objerr.cells[0];
		CellPosition.innerHTML="Please select the checkbox first";
		return false;
	}
}

//submit into popupwindow
function popFormSubmit(objfrm, submitPage, popup)
{
	var Objerr = $("errorPosition");
	if(ifChecked(objfrm)){
		Objerr.style.display = "none";
		objfrm.action = submitPage;
		if( popup == 'yes' ){
			objfrm.target="popup";
			objfrm.method="post";
			var height = 480;
			var width = 600;
			var left = (document.body.clientWidth - width)/2;
			var top = (document.body.clientHeight - height)/2;
			var win=window.open("","popup","height="+height+",width="+width+",top="+top+",left="+left);
			win.focus();
		}else{
			objfrm.target="_self";
			objfrm.method="post";
		}
		objfrm.submit();
		return true;
	}else{
		Objerr.style.display = "";
		Objerr.innerHTML = "Please select the checkbox first";
		return false;
	}
}


//fck
function addAFck(objID)
{
	var objName = $(objID).name;
		
	var FCK = new FCKeditor(objName);
	FCK.BasePath = "../fckeditor/";
	FCK.Height = 350;
	FCK.ToolbarSet = "Default"; 
	FCK.ReplaceTextarea();
}


//add options
function addOption(selectObj, optionText, optionValue)
{
	var moPtion=document.createElement("OPTION");
	moPtion.innerText=optionText;
	moPtion.text=optionText;
	moPtion.value=optionValue;
	selectObj.appendChild(moPtion);
}

//clear all the form fields
function resetForm(objfrm,string)
{
	var length=objfrm.elements.length;
	for(var i=0;i<length;i++){
		if(objfrm.elements[i].type=="text"){
				objfrm.elements[i].value="";
		}
		if(string!=""){
			var array_str=string.split(",");
			if(array_str.length>0){
				for(var j=0;j<array_str.length;j++){
					if(objfrm.elements[i].name==array_str[j]){
						objfrm.elements[i].value="";
					}
				}
			}
		}
	}
}

//change line's color automaticlly
/*
colorOne: the first color which is ready to change
colorTwo: the second color which is ready to change
tableID: the ID of table
startLine: the real line count in table where we start change color
*/
function changeTrColor(colorOne, colorTwo, tableID, startLine)
{
	var tableOBJ = $(tableID);
	var trArr = tableOBJ.getElementsByTagName("tr");
	for( var i = (parseInt(startLine)-1); i<trArr.length; i ++ )
	{
		var trOBJ = trArr[i];
		var tdArr = trOBJ.getElementsByTagName("td");
		if(trOBJ.style.display == 'none')
		{
			continue;
		}
		var bgColorDefault = (bgColorDefault == colorOne?colorTwo:colorOne);
		for( var j=0; j<tdArr.length; j++ )
		{
			tdArr[j].bgColor = bgColorDefault;
		}
	}
}

/************ Other Functions ************/