/*============================================================================================'
'==============================   STANDARD INCLUDE OBJECTS   ================================='
'============================================================================================='
'== Installation:                                                                           =='
' Place this file in an INCLUDE directory, then copy the code below (on line 10) and paste    '
' it in the header of your HTML page. Remember to have it directing to the appropriate        '
' directory, i.e. the directory where you placed this script.                                 '
'                                                                                             '
'<script language="javascript" src="includes/fitObjects.js"></script>                         '
'                                                                                             '
'============================================================================================='
'== Versioning Notes:                                                                       =='
' v1.0.0: 12/01/2007                                                                          '
'   * Created object fileLoader.                                                              '
' v1.0.1: 15/01/2007                                                                          '
'   * Created object brower.                                                                  '
' v1.0.2: 16/01/2007                                                                          '
'   * Created object validator.                                                               '
' v1.0.2: 17/01/2007                                                                          '
'   * Created object varType.                                                                 '
'============================================================================================='
'============================================================================================*/



/*============================================================================================'
'===================================  fileLoader Object  ====================================='
'============================================================================================='
'== Description:                                                                            =='
'     This Object loads files by writing out thier HTML tags via scripted JavaScript.         '
'============================================================================================='
'== Creating Object:                                                                        =='
'     var fl=new fileLoader();                                                                '
'============================================================================================='
'== Methods:                                                                                =='
'    fileLoader.css(href[[[,media],rel],title])                                               '
'    + Description                                                                            '
'       Loads a CSS file to the HTML page by writing out its HTML tags.                       '
'                                                                                             '
'    + Parameters                                                                             '
'       ~ href:String [Required] - A valid CSS file location and file name.                   '
'       ~ media:String [Optional] - What media the CSS file relates to (valid HTML only).     '
'       ~ rel:String [Optional] - The relationship the StyleSheet has to the page (valid HTML '
'         only).                                                                              '
'       ~ title:String [Optional] - The title of the CSS Include.                             '
'                                                                                             '
'    + Returns                                                                                '
'       Returns true if it was successfully created, false if it was not.                     '
'                                                                                             '
'    + Example                                                                                '
'       var fl=new fileLoader();                                                              '
'       if(fl.css("style.css","all","stylesheet","Default Styles")){                          '
'          alert("CSS File Successfully Loaded.");                                            '
'       }                                                                                     '
'============================================================================================*/
function fileLoader(){
	var _t=this;
	_t.css=function(href,media,rel,title){
		if(href==null){
			return(false);
		}else{
			media='media="'+((media==null)?"all":media)+'"';
			rel='rel="'+((rel==null)?"stylesheet":rel)+'"';
			title=(title==null)?'':' title="'+title+'"';
			document.write('<link type="text/css" href="'+href+'" '+media+' '+rel+title+' />');
			return(true);
		}
	}
}



/*============================================================================================'
'=====================================  browser Object  ======================================'
'============================================================================================='
'== Description:                                                                            =='
'     This Object contains a bunch of Browser related properties and methods.                 '
'============================================================================================='
'== Creating Object:                                                                        =='
'     var _b=new browser();                                                                   '
'============================================================================================='
'== Methods:                                                                                =='
'    browser.popUp(href[,name[,options[,center]]])                                            '
'    + Description                                                                            '
'       Pops open a window, with additional options to the basic window.open method.          '
'                                                                                             '
'    + Parameters                                                                             '
'       ~ href:String [Required] - The URL of the page to be loaded in the PopUp.             '
'       ~ name:String [Optional] - The reference/name of the popup window.                    '
'       ~ options:String [Optional] - Standard window.open options with added options centerX '
'         and centerY. These are Boolean values (1,0,true,false) to indicate if centering is  '
'         required. If it is, a Width and Height option (respectively) must also be included. '
'                                                                                             '
'    + Returns                                                                                '
'       Returns true if it was successfully poped up, false if it was not.                    '
'                                                                                             '
'    + Example                                                                                '
'       var _b=new browser();                                                                 '
'       if(_b.popUp('myPopUp.php','myPopUp','width=450,height=350,centerX=1,centerY=1')){     '
'          alert("Popup window successfully opened.");                                        '
'       }                                                                                     '
'============================================================================================*/
function browser(){
	var _t=this;
	_t.popUp=function(href,name,options){
		var width=0,height=0,centerX=0,centerY=0,topC=-1,leftC=-1,cXc=0,cYc=0,opts="";
		var ar=[];
		if(href==null||href==''){
			return(false);
		}else{
			if(options!=null&&options!=''){
				ar=options.toLowerCase().split(",");
				for(var c=0;c<ar.length;c++){
					ar[c]=ar[c].split("=");
					if(ar[c][0]=="width") width=ar[c][1];
					if(ar[c][0]=="height") height=ar[c][1];
					if(ar[c][0]=="centerx") centerX=ar[c][1];
					if(ar[c][0]=="centery") centerY=ar[c][1];
					if(ar[c][0]=="top") topC=c;
					if(ar[c][0]=="left") leftC=c;
				}
				if(topC==-1){
					ar.push(new Array("top",0));
					topC=ar.length-1;
				}
				if(leftC==-1){
					ar.push(new Array("left",0));
					leftC=ar.length-1;
				}
				if(centerY&&height) ar[topC][1]=((screen.height-height)/2);
				if(centerX&&width) ar[leftC][1]=((screen.width-width)/2);
				for(var c=0;c<ar.length;c++){
					opts+=(c>0)?",":"";
					opts+=ar[c][0]+"="+ar[c][1];
				}
				opts=opts.replace(/(center[xy]{1}=[\da-z]+)/gi,'');
				opts=opts.replace(/,{2,}/gi,',');
				opts=opts.replace(/(,$)|(^,)/gi,'');
			}else{
				opts=null;
			}
			window.open(href,name,opts);
			return(true);
		}
	}
}



/*============================================================================================'
'=====================================  varType Object  ======================================'
'============================================================================================='
'== Description:                                                                            =='
'     This Object performs various variable checking functions/methods.                       '
'============================================================================================='
'== Creating Object:                                                                        =='
'     var _v=new v();                                                                 '
'============================================================================================='
'== Methods:                                                                                =='
'    browser.popUp(href[,name[,options[,center]]])                                            '
'    + Description                                                                            '
'       Pops open a window, with additional options to the basic window.open method.          '
'                                                                                             '
'    + Parameters                                                                             '
'       ~ href:String [Required] - The URL of the page to be loaded in the PopUp.             '
'       ~ name:String [Optional] - The reference/name of the popup window.                    '
'       ~ options:String [Optional] - Standard window.open options with added options centerX '
'         and centerY. These are Boolean values (1,0,true,false) to indicate if centering is  '
'         required. If it is, a Width and Height option (respectively) must also be included. '
'                                                                                             '
'    + Returns                                                                                '
'       Returns true if it was successfully poped up, false if it was not.                    '
'                                                                                             '
'    + Example                                                                                '
'       var _b=new validator();                                                               '
'       if(_b.popUp('myPopUp.php','myPopUp','width=450,height=350,centerX=1,centerY=1')){     '
'          alert("Popup window successfully opened.");                                        '
'       }                                                                                     '
'============================================================================================*/
function varType(variant){
	this.isFunction=function(a){return(typeof a=='function');}
	this.isObject=function(a){return(a&&typeof a=='object')||this.isFunction(a);}
	this.isArray=function(a){return(this.isObject(a)&&a.constructor==Array);}
	this.isString=function(a){return(typeof a=='string');}
	this.isBoolean=function(a){return(typeof a=='boolean');}
	this.isUndefined=function(a){return(typeof a=='undefined');}
	this.isNull=function(a){return(typeof a=='object'&&!a);}
	this.isEmpty=function(o){
		var i,v;
	    if(this.isObject(o)){
		   for(i in o){
			  v=o[i];
			  if(this.isUndefined(v)&&this.isFunction(v)){return(false);}
		   }
	    }
	    return(true);
	}
	if(variant!=null){
	}
}


/*============================================================================================'
'====================================  validator Object  ====================================='
'============================================================================================='
'== Description:                                                                            =='
'     This Object contains a bunch of properties and methods to aid in Form Validation.       '
'============================================================================================='
'== Creating Object:                                                                        =='
'     var _v=new v();                                                                 '
'============================================================================================='
'== Methods:                                                                                =='
'    browser.popUp(href[,name[,options[,center]]])                                            '
'    + Description                                                                            '
'       Pops open a window, with additional options to the basic window.open method.          '
'                                                                                             '
'    + Parameters                                                                             '
'       ~ href:String [Required] - The URL of the page to be loaded in the PopUp.             '
'       ~ name:String [Optional] - The reference/name of the popup window.                    '
'       ~ options:String [Optional] - Standard window.open options with added options centerX '
'         and centerY. These are Boolean values (1,0,true,false) to indicate if centering is  '
'         required. If it is, a Width and Height option (respectively) must also be included. '
'                                                                                             '
'    + Returns                                                                                '
'       Returns true if it was successfully poped up, false if it was not.                    '
'                                                                                             '
'    + Example                                                                                '
'       var _b=new validator();                                                               '
'       if(_b.popUp('myPopUp.php','myPopUp','width=450,height=350,centerX=1,centerY=1')){     '
'          alert("Popup window successfully opened.");                                        '
'       }                                                                                     '
'============================================================================================*/
function validator(){
	status='validator initiating...'
	this.fields=0;
	this.errors=0;
	this.init='The following errors must be corrected before continuing:\n\n';
	this.msg='';
	this.vt=new varType();
	this.output = function (){
		status=''
		return(this.init+this.msg);
	}

	this.clean = function (){
		this.fields=0;
		this.errors=0;
		this.init='The following errors must be corrected before continuing:\n\n';
		this.msg='';
		status=''
	}

	this.addError = function (erMsg){
		this.errors++;
		this.msg+=' '+this.errors+'. '+erMsg+'.\n';
	}

	//Number Validation
	this.round=function (num,dec){
	}
	
	//String Validation
	this.str = function (field,fieldName,required,minLen,maxLen,altVal) {
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			minLen=(minLen==null)?-1:minLen;
			maxLen=(maxLen==null)?-1:maxLen;
			altVal=(altVal==null)?'':altVal;
			var isErr=false;	
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				if(required&&field.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please enter a value in the field: '+fieldName+'.\n';
					isErr=true;
				}else if(required&&field.value.length<minLen&&minLen>-1){
					this.errors++;
					this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" must be at least '+minLen+' characters.\n';
					isErr=true;
				}else if(required&&field.value.length>maxLen&&maxLen>-1){
					this.errors++;
					this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" must not exceed '+maxLen+' characters.\n';
					isErr=true;
				}
				if(isErr&&this.errors==1){
					field.focus();
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}

	
	//Number Validation
	this.num = function (field,fieldName,required,isIn,format,altVal,minNum,maxNum){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			isIn=(isIn==null)?'[^01234567890\.\-]':isIn;
			format=(format==null)?'':format;
			altVal=(altVal==null)?'':altVal;
			var isErr=false;	
			var re=new RegExp().compile(isIn,'gi');
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				if(required&&field.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please enter a valid Number in the field: '+fieldName+'.\n';
					isErr=true;
				}else if(re.test(field.value)){
					if(required){
						this.errors++;
						this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" must only contain numbers.\n';
						isErr=true;
					}else{
						field.value=field.value.replace(re,'');
					}
				}
				if(Number(field.value)<Number(minNum)&&required&&minNum!=null){
					this.errors++;
					this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" is not valid (must be greater than '+(minNum-1)+').\n';
					isErr=true;
				}
				if(Number(field.value)>Number(maxNum)&&required&&maxNum!=null){
					this.errors++;
					this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" is not valid (must be less than '+(maxNum+1)+').\n';
					isErr=true;
				}
				if(!isErr&&format.length>0){
					//get the DP's requested if there are any.
					var dps=parseInt(format.substr(format.indexOf(".")+1));
					if(!isNaN(dps)&&dps>0){
						
					}
					for(c=0;c<format.length;c++){
						
					}
				}
				if(isErr&&this.errors==1&&field.type!='hidden'){
					field.focus();
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}

	
	//Mobile Number Validation
	this.mob = function (field,fieldName,required,format,altVal){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			format=(format==null)?'0000 000 000':format;
			altVal=(altVal==null)?'':altVal;
			var isErr=false;	
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				if(required&&field.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please enter a valid Mobile Number in the field: '+fieldName+'.\n';
					isErr=true;
				}else{
					field.value=field.value.replace(/[^0-9]/gi,'');
					if(required&&field.value.length!=10){
						this.errors++;
						this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" was not a valid mobile number.\n';
						isErr=true;
					}else{
						var mC=0,mobile='';
						for(c=0;c<format.length&&mC<field.value.length;c++){
							if(isNaN(format.charAt(c))||format.charAt(c)==' '){
								mobile+=format.charAt(c);
							}else{
								mobile+=field.value.charAt(mC);
								mC++;
							}
						}
						field.value=mobile;
					}
				}
				if(isErr&&this.errors==1){
					field.focus();
				}
				return(true);
			}else{
				return(false);
			}
		}

	
	//Email Address Validation
	this.email = function (field,fieldName,required,altVal){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			altVal=(altVal==null)?'altVal':altVal;
			var isErr=false;	
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				if(required&&field.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please enter a valid Email Address in the field: '+fieldName+'.\n';
					isErr=true;
				}else{
					re=/^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/gi;
					if(required&&!re.test(field.value)){
						this.errors++;
						this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" was not a valid Email Address.\n';
						isErr=true;
					}else if(!re.test(field.value)&&altVal!='altVal'){
						field.value=altVal;
					}
				}
				if(isErr&&this.errors==1){
					field.focus();
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}

	
	//Telephone Number Validation
	this.tel = function (field,fieldName,required,altVal){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			altVal=(altVal==null)?'altVal':altVal;
			var isErr=false;	
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				if(required&&field.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please enter a valid Telephone Number in the field: '+fieldName+'.\n';
					isErr=true;
				}else{
					re=/[^\+\s\d\-\.\(\)]/gi;
					if(required&&re.test(field.value)){
						this.errors++;
						this.msg+=' '+this.errors+'. The value in the field "'+fieldName+'" was not a valid Telephone Number.\n';
						isErr=true;
					}else if(!re.test(field.value)&&altVal!='altVal'){
						field.value=altVal;
					}
				}
				if(isErr&&this.errors==1){
					field.focus();
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}

	
	//Basic Dropmenu Validation
	this.drop = function (field,fieldName,required,nullIndex){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			var isErr=false;	
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				if(required&&(field.value.length<1||field.selectedIndex==nullIndex)){
					this.errors++;
					this.msg+=' '+this.errors+'. Please select an option from the drop-menu: '+fieldName+'.\n';
					isErr=true;
				}
				if(isErr&&this.errors==1){
					field.focus();
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}

	
	//Basic Radio Field Validation
	this.radio = function (field,fieldName,required,defaultIndex){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			defaultIndex=(defaultIndex==null)?-1:defaultIndex;
			var isErr=false;	
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				isRadioOk=false;
				for(var i=0;i<field.length;i++){
					if(field[i].checked){
						isRadioOk=true;
						i=field.length+10;
					}
				}
				if(required&&!isRadioOk){
					this.errors++;
					this.msg+=' '+this.errors+'. Please select an option from the list: '+fieldName+'.\n';
					isErr=true;
				}
				if(defaultIndex>-1&&!isRadioOk){
					field[defaultIndex].checked=true;
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}

	
	//Basic Dropmenu Validation
	this.dateDrop = function (fieldY,fieldM,fieldD,fieldName,required,altVal){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			altVal=(altVal==null)?'altVal':altVal;
			var isErr=false;	
			if(this.vt.isObject(fieldY)&&this.vt.isObject(fieldM)&&this.vt.isObject(fieldD)){
				fieldName=(fieldName==null)?fieldD.name:fieldName;
				this.fields++;
				if(required&&fieldD.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please select a Day from the drop-menu: '+fieldName+'.\n';
					isErr=true;
				}
				if(required&&fieldM.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please select a Month from the drop-menu: '+fieldName+'.\n';
					isErr=true;
				}
				if(required&&fieldY.value.length<1){
					this.errors++;
					this.msg+=' '+this.errors+'. Please select a Year from the drop-menu: '+fieldName+'.\n';
					isErr=true;
				}
				if(!isErr){
					date=fieldD.value+"/"+fieldM.value+"/"+fieldY.value
					test1=(/^\d{1,2}[\-/]\d{1,2}[\-/]\d{4}$/.test(date))
					date=date.replace(/[\-/]/g,',');
					date=date.split(',');
					d=new Date(date[2],date[1]-1,date[0]);
					test2=(1*date[0]==d.getDate()&&1*date[1]==(d.getMonth()+1)&&1*date[2]==d.getFullYear());
					if(test1&&test2){
						return(true);
					}
					else if(required){
						this.errors++;
						this.msg+=' '+this.errors+'. The date you entered in the field: '+fieldName+' was invalid.\n';
						isErr=true;
					}else{
						fieldY.selectedIndex=0;
						fieldM.selectedIndex=0;
						fieldD.selectedIndex=0;
					}
				}
				if(isErr&&this.errors==1){
					fieldD.focus();
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}

	
	//Basic Date Validation
	this.date = function (field,fieldName,required,format,altVal){
			status='Validating '+fieldName+'.';
			required=(required==null)?true:required;
			altVal=(altVal==null)?'altVal':altVal;
			var isErr=false;	
			if(this.vt.isObject(field)){
				fieldName=(fieldName==null)?field.name:fieldName;
				this.fields++;
				if(required&&(field.value.length<1||field.value.indexOf("/")<0)){
					this.errors++;
					this.msg+=' '+this.errors+'. Please enter in a valid date in the field: '+fieldName+'.\n';
					isErr=true;
				}
				if(!isErr){
					date=field.value
					test1=(/^\d{1,2}[\-/]\d{1,2}[\-/]\d{4}$/.test(date))
					date=date.replace(/[\-/]/g,',');
					date=date.split(',');
					d=new Date(date[2],date[1]-1,date[0]);
					test2=(1*date[0]==d.getDate()&&1*date[1]==(d.getMonth()+1)&&1*date[2]==d.getFullYear());
					if(test1&&test2){
						return(true);
					}
					else if(required){
						this.errors++;
						this.msg+=' '+this.errors+'. The date you entered in the field: '+fieldName+' was invalid.\n';
						isErr=true;
					}else if(altVal!='altVal'){
						field.value=altVal;
					}
				}
				if(isErr&&this.errors==1){
					field.focus();
				}
				status='';
				return(true);
			}else{
				status='';
				return(false);
			}
		}
	status='validator initiated.';
}
