var checkForms = new function(){
	this.focusItem				= '';			// First focus item in form
	this.errorMessage			= '';			// Error message to display to user
	this.decimalPlaces 			= 2;			// Decimal places allowed for a form
	this.checkResult			= true;			// Varible used to check form for errors
	this.focusInput				= null;			// First missing input item to focus on
	this.reqInput				= Array();		// Array to store required values
	this.allForms				= Array();
	this.resetButton			= Array();		// Button used to reset form
	
	/////////////////////////// AJAX VARIBLES /////////////////////////////////////////////////
	this.callAjax				= Array();		// Array to hold different AJAX object
	this.useAjax				= Array();		// set to True to use Ajax
	this.displayAjaxDiv			= Array();		// document location for ajax response
	this.displayAjaxErrorDiv	= Array();		// Location to display ajax Errors
	
	/////////////////////////// INPUT VARIBLES	////////////////////////////////////////////////
	this.reqInput				= Array();		// Array to store required values
	this.inputClass				= Array();		// Input class for input elements
	this.errorClass				= Array();		// Error class for input elements
	this.outputErrorLocation	= Array();		// Document location of div to put error message
	this.outputClass 			= Array();		// Class to apply to div with error location
	this.outputErrorClass		= Array();		// Class to change the div to show errors
	
	/////////////////////////// ANIMATION VARIBLES //////////////////////////////////////////////
	this.displayAnimDiv			= Array();		// Animation to display when calling ajax
	this.toggle					= Array();		// Hold colour change amount for each animation
	this.r						= Array();		// Red Colour	0-255
	this.g						= Array();		// Green colour 0-255
	this.b						= Array();		// blue colour	0-255
	this.animID					= Array();		// AnimationID = used to start and stop animations
	this.formAnimationCount		= Array();		// Array used to track animations on page
	this.timeout				= '110';		// Amount to do the animation at most
	this.animationCount			= Array();		// Array to store how many times the animation has looped

	///////////////////////////// EMAIL OPTIONS ..////////////////////////////////////////////////
	this.email_domain 			= 'magicdesigns.co.nz';
	this.email_account 			= 'support';
	this.subject 				= "Magic Deisgns Website Enquiry";
	this.bodyText 				= "Magic Designs would love to hear from you...";
	
	this.formatEmail = function(objText){
		var self = checkForms;
		objText.value = objText.value.replace(/ /g,'');
	};
	this.formatDecimal = function(objText){
		var self = checkForms;
		var tempAmount = "";
		var amount = objText.value.replace(/ /g,'');							// Remove spaces
		if (amount.lastIndexOf('.') > amount.indexOf('.')){						// Remove second decimal place
			amount = amount.substring(0,amount.lastIndexOf('.'));
		}
		if (amount.indexOf('.')!= -1){
			amount = amount.substring(0,amount.indexOf('.')+ 1 + parseInt(self.decimalPlaces));
		}
		for(var i =0; i < amount.length; i++){
			if(!isNaN(amount.charAt(i)) || amount.charAt(i)=="."){
				tempAmount += "" + amount.charAt(i);
			}
		}
		objText.value = tempAmount;
	};
	this.formatInteger = function(objText){
		var self = checkForms;
		var cleanedInt = '';
		objText.value = objText.value.replace(/ /g,'');
		for(var i = 0; i < objText.value.length; i++){
			if(!isNaN(objText.value.charAt(i))){
				cleanedInt += objText.value.charAt(i)
			}
		}
		objText.value = cleanedInt;
	};
	this.formatPhone = function(objText){
		var self = checkForms;
		var phoneNumber = '';
		var cleanedAmount = '';
		var countLocation = 0;
		phoneNumber = objText.value.replace(/ /g,'');
		for(var j = 0; j < phoneNumber.length; j++){
			if(!isNaN(phoneNumber.charAt(j))){
				switch(countLocation){
					case 0:
						cleanedAmount += '(';
						break;
					case 2:
						cleanedAmount += ') ';
						break;
					case 5:
						cleanedAmount += '-';
						break;
				}
				cleanedAmount += phoneNumber.charAt(j);
				countLocation++;
			}
		}
		objText.value = cleanedAmount;
	};
	this.setUpInputs = function(formId){
		var self = checkForms;
		self.reqInput[formId] = Array();
		self.reqInput[formId]['email']	=((document.getElementById(formId).emailcheck) ? document.getElementById(formId).emailcheck.value  		:  'email' );
		self.reqInput[formId]['int']	=((document.getElementById(formId).intcheck) ? document.getElementById(formId).intcheck.value 			:  'qty' );
		self.reqInput[formId]['text']	=((document.getElementById(formId).textcheck) ? document.getElementById(formId).textcheck.value  		:  'first_name,last_name,city,street' );
		self.reqInput[formId]['decimal']=((document.getElementById(formId).decimalcheck) ? document.getElementById(formId).decimalcheck.value 	:  'price' );
		self.reqInput[formId]['phone'] 	=((document.getElementById(formId).phonecheck) ? document.getElementById(formId).phonecheck.value  		:  'phone' );
		self.reqInput[formId]['exclude']=((document.getElementById(formId).excludecheck) ? document.getElementById(formId).excludecheck.value + ',submit,reset' :  'submit,button,reset' );
	};
	this.setUpClasses = function(formId){
		var self = checkForms;
		self.inputClass[formId] = ((document.getElementById(formId).inputclass) ? document.getElementById(formId).inputclass.value :'');
		self.errorClass[formId] = ((document.getElementById(formId).inputerrorclass) ? document.getElementById(formId).inputerrorclass.value : self.inputClass[formId]);
	};
	this.setUpErrors = function(formId){
		var self = checkForms;
		self.outputErrorLocation[formId] = ((document.getElementById(formId).errorout) ? document.getElementById(formId).errorout.value :'');
		self.outputClass[formId] = ((document.getElementById(formId).errorclass) ? document.getElementById(formId).errorout.value :'');
		self.outputErrorClass[formId] = ((document.getElementById(formId).showerrorclass) ? document.getElementById(formId).errorout.value :self.outputClass[formId]);
	};
	this.setUpAjax = function(formId){
		var self = checkForms;
		var useAjax =((document.getElementById(formId).ajax) ? document.getElementById(formId).ajax.value : 'no'); 
			self.displayAjaxDiv[formId]	= ((document.getElementById(formId).ajaxout) ? document.getElementById(formId).ajaxout.value : '');
			self.displayAjaxErrorDiv[formId] = ((document.getElementById(formId).ajaxerror) ? document.getElementById(formId).ajaxerror.value : '');
			self.displayAnimDiv[formId] = ((document.getElementById(formId).ajaxanimation) ? document.getElementById(formId).ajaxanimation.value :'');
		if(useAjax=='yes'){	
			self.callAjax[formId] = new Ajax;
			self.useAjax[formId] = true;
		}
	};
	this.resetAll = function(formId){
		var self = checkForms;
		if (self.useAjax[formId]== true){
			self.stopAnim(formId);
			self.callAjax[formId].abort();
			if (typeof document.getElementById(self.displayAjaxDiv[formId]) == 'object' && self.displayAjaxDiv[formId] != ''){
				//document.getElementById(self.displayAjaxDiv[formId]).innerHTML = '';
			}
			if (typeof document.getElementById(self.displayAjaxErrorDiv[formId]) == 'object' && self.displayAjaxErrorDiv[formId] != '' ){
				//document.getElementById(self.displayAjaxErrorDiv[formId]).innerHTML = '';
			}
		}
		for(var j =0; j < document.getElementById(formId).elements.length; j++){
			if (self.reqInput[formId]['exclude'].indexOf(document.getElementById(formId).elements[j].type) == -1){
				document.getElementById(formId).elements[j].className = self.inputClass[formId];
				if (self.inputClass[formId] == ''){
					document.getElementById(formId).elements[j].style.border = 'solid 1px #999999';
					document.getElementById(formId).elements[j].style.backgroundColor = '#FFFFFF';
				}
			}
		}
		if (typeof document.getElementById(self.outputErrorLocation[formId]) == 'object' && self.outputErrorLocation[formId] != ''){
			if(self.outputClass[formId]==''){
				document.getElementById(self.outputErrorLocation[formId]).style.border = 'solid 1px #FFFFFF';
				document.getElementById(self.outputErrorLocation[formId]).style.backgroundColor	='#FFFFFF';
			}else{
				document.getElementById(self.outputErrorLocation[formId]).className = self.outputClass[formId];
			}
			document.getElementById(self.outputErrorLocation[formId]).innerHTML = '';
		}
	};
	this.email_person = function(objID){
		var self = checkForms;
		document.getElementById(objID).href='mailto:' + self.email_account + '@' + self.email_domain + '?subject=' + self.subject + '&body=' + self.bodyText;
	};
	this.init = function(){
		var self = checkForms;
		for(var allLinks = 0; allLinks < document.getElementsByTagName('a').length; allLinks++){
			if(document.getElementsByTagName('a')[allLinks].className=='ajax'){			
				document.getElementsByTagName('a')[allLinks].onclick = function(){var localAjax = new Ajax; localAjax.doAjax(this.href,this.title); return false;}
			}
			if (document.getElementsByTagName('a')[allLinks].className=='email'){
				document.getElementsByTagName('a')[allLinks].onclick = function(){ checkForms.email_person(this.id); }
			}
		}
		for(var i = 0; i < document.forms.length; i++){
			if(typeof document.forms[i].required == 'object' && document.forms[i].required.value=='required' && document.forms[i].id){
				formId = document.forms[i].id;
				self.allForms[formId] = document.forms[i];
				self.allForms[formId].onsubmit = function(){checkForms.procForm(this.id); return false;}
				self.setUpInputs(formId);
				self.setUpAjax(formId);
				self.setUpErrors(formId);
				self.setUpClasses(formId);
				self.resetAll(formId);
				for(var j =0; j < document.forms[i].elements.length; j++){
					if(document.forms[i].elements[j].type == 'reset'){
						document.forms[i].elements[j].onclick = function(){checkForms.resetAll(this.form.id)}
					}
					if (self.reqInput[formId]['exclude'].indexOf(document.forms[i].elements[j].type) == -1){
						document.forms[i].elements[j].className = self.inputClass[document.forms[i].id];
						if(self.focusItem == document.forms[i].elements[j].name){	
							document.forms[i].elements[j].focus();
						}
						if (self.reqInput[formId]['email'].indexOf(document.forms[i].elements[j].name)!= -1){
							document.forms[i].elements[j].onkeyup = function(){checkForms.formatEmail(this)}
						}
						if (self.reqInput[formId]['decimal'].indexOf(document.forms[i].elements[j].name)!= -1){
							document.forms[i].elements[j].onkeyup = function(){checkForms.formatDecimal(this)}
						}
						if (self.reqInput[formId]['int'].indexOf(document.forms[i].elements[j].name)!= -1){
							document.forms[i].elements[j].onkeyup = function(){checkForms.formatInteger(this)}
						}
						if (self.reqInput[formId]['phone'].indexOf(document.forms[i].elements[j].name)!= -1){
							document.forms[i].elements[j].onkeyup = function(){checkForms.formatPhone(this)}
						}	
					}
				}
			}
		}
	};
	this.procForm = function(formId){
		var self = checkForms;
		self.checkResult 	= true;
		self.focusInput  	= null;
		self.errorMessage 	= '';	
		self.resetAll(formId);
		if (typeof document.getElementById(self.outputErrorLocation[formId]) == 'object' && self.outputErrorLocation[formId] != ''){
			document.getElementById(self.outputErrorLocation[formId]).className = self.outputClass[formId];	
			if (self.outputClass[formId] == ''){
				document.getElementById(self.outputErrorLocation[formId]).style.visibility = 'hidden';
			}
		}
		if (typeof document.getElementById(self.displayAjaxDiv[formId]) == 'object' && self.displayAjaxDiv[formId] != ''){
			//document.getElementById(self.displayAjaxDiv[formId]).innerHTML ='';
		}
		if(typeof document.getElementById(self.displayAjaxErrorDiv[formId]) == 'object' && self.displayAjaxErrorDiv[formId] != ''){
			//document.getElementById(self.displayAjaxErrorDiv[formId]).innerHTML ='';
		}
		for(var i = 0;i < self.allForms[formId].elements.length; i++){
			if(self.reqInput[formId]['exclude'].indexOf(self.allForms[formId].elements[i].type) == -1){
				self.allForms[formId].elements[i].className = self.inputClass[formId];
				if(self.inputClass[formId]== ''){
					self.allForms[formId].elements[i].style.border = 'solid 1px #999999';
					self.allForms[formId].elements[i].style.backgroundColor = '#FFFFFF';
				}
				if(self.reqInput[formId]['email'].indexOf(self.allForms[formId].elements[i].name) != -1)	{self.procEmail(i,formId);}
				if(self.reqInput[formId]['text'].indexOf(self.allForms[formId].elements[i].name)!= -1)		{self.procText(i,formId);}
				if(self.reqInput[formId]['decimal'].indexOf(self.allForms[formId].elements[i].name)!= -1)	{self.procDecimal(i,formId);}
				if(self.reqInput[formId]['int'].indexOf(self.allForms[formId].elements[i].name)!= -1)		{self.procInt(i,formId);}
				if(self.reqInput[formId]['phone'].indexOf(self.allForms[formId].elements[i].name) != -1)	{self.procPhone(i,formId);}
			}
		}
		if (self.checkResult == false){
			if (typeof document.getElementById(self.outputErrorLocation[formId]) == 'object' && self.outputErrorLocation[formId] != ''){
				if(self.outputErrorClass[formId] != ''){
					document.getElementById(self.outputErrorLocation[formId]).className = self.outputErrorClass[formId];
				}else{
					document.getElementById(self.outputErrorLocation[formId]).style.visibility = 'visible';
					document.getElementById(self.outputErrorLocation[formId]).style.backgroundColor = '#EEEEFF';
					document.getElementById(self.outputErrorLocation[formId]).style.border = 'solid 1px #0000FF';
					document.getElementById(self.outputErrorLocation[formId]).style.padding = '5px';
				}
				document.getElementById(self.outputErrorLocation[formId]).innerHTML = self.errorMessage;
			}else{
				self.errorMessage = self.errorMessage.replace(/<br \/>/g,'\r\n');
				alert(self.errorMessage);
			}
			if(self.focusInput != null){
				self.focusInput.focus();
			}
			return false;
		}else{
			if(self.useAjax[formId]== true){
				self.callAjax[formId].abort();
				self.stopAnim(formId);
				self.doAnim(formId);
				self.callAjax[formId].doAjax(self.allForms[formId],self.displayAjaxDiv[formId],self.displayAjaxErrorDiv[formId]);
			}else{
				for(var i = 0;i< document.forms.length; i++){
					if(self.useAjax[document.forms[i].id] == true){
						self.callAjax[document.forms[i].id].abort();
						self.stopAnim(document.forms[i].id);
					}
				}
				self.allForms[formId].submit();
			}
		}
		return true;
	};
	this.doAnim = function(formId){
		var self = checkForms;
		if(typeof document.getElementById(self.displayAnimDiv[formId]) =='object' && self.displayAnimDiv[formId] != ''){
			document.getElementById(self.displayAnimDiv[formId]).innerHTML = 'Processing';
			self.r[formId] = self.g[formId] = 0;
			self.b[formId] = 255;
			self.toggle[formId]= 10;	
			self.formAnimationCount[self.formAnimationCount.length+1] = formId;
			var currAnimationCount = self.formAnimationCount.length - 1;
			self.animationCount[formId]=0;
			self.animID[formId] = setInterval("checkForms.animate(" + currAnimationCount + ")",25);
		}
	};
	this.animate = function(currAnimation){
		var self = checkForms;
		formId = self.formAnimationCount[currAnimation];
		self.animationCount[formId]++;
		if(self.r[formId] >= 255){self.toggle[formId] = -10;}
		if(self.r[formId] <= 0){self.toggle[formId] = 10;}
		self.r[formId] = self.g[formId] += self.toggle[formId];
		if(typeof document.getElementById(self.displayAnimDiv[formId]) =='object' && self.displayAnimDiv[formId] != ''){
			document.getElementById(self.displayAnimDiv[formId]).style.display = 'block';
			document.getElementById(self.displayAnimDiv[formId]).style.visibility = 'visible';
			document.getElementById(self.displayAnimDiv[formId]).style.border = 'solid 1px ' + 'rgb('+ self.r[formId] + ','+ self.g[formId] + ','+ self.b[formId] + ')';
			document.getElementById(self.displayAnimDiv[formId]).style.backgroundColor = '#EEEEFF';
			document.getElementById(self.displayAnimDiv[formId]).style.fontSize = '60%';
			document.getElementById(self.displayAnimDiv[formId]).style.textAlign = 'center';
			document.getElementById(self.displayAnimDiv[formId]).style.color = 'rgb('+ self.r[formId] + ','+ self.g[formId] + ','+ self.b[formId] + ')';
			document.getElementById(self.displayAnimDiv[formId]).style.height = '12px';
			document.getElementById(self.displayAnimDiv[formId]).style.padding = '0px';
			if(typeof document.getElementById(self.displayAjaxDiv[formId]) == 'object' && self.displayAjaxDiv[formId] != ''){
				//if(document.getElementById(self.displayAjaxDiv[formId]).innerHTML != '' || document.getElementById(self.displayAjaxErrorDiv[formId]).innerHTML != ''){
				//	self.stopAnim(formId);
				//}
			}else{
				if(self.animationCount[formId] > self.timeout){
					self.stopAnim(formId);
				}
			}
		}
	};
	this.stopAnim = function(formId){
		var self = checkForms;
		clearInterval(self.animID[formId]);
		self.animID[formId] = null;
		self.r[formId] = 0; self.g[formId] = 0; self.b[formId] = 255;
		if(typeof document.getElementById(self.displayAnimDiv[formId]) == 'object' && self.displayAnimDiv[formId] != '' && self.displayAnimDiv[formId]){
			document.getElementById(self.displayAnimDiv[formId]).innerHTML = '';
			document.getElementById(self.displayAnimDiv[formId]).style.border = 'solid 1px #EEEEFF'; 
			document.getElementById(self.displayAnimDiv[formId]).style.backgroundColor = '#FFFFFF'; 
			document.getElementById(self.displayAnimDiv[formId]).style.width = '90px';
			document.getElementById(self.displayAnimDiv[formId]).style.height = '15px';
			document.getElementById(self.displayAnimDiv[formId]).style.visibility='hidden';
			document.getElementById(self.displayAnimDiv[formId]).style.display='none';
		}
		self.animationCount[formId]=0;
	};
	this.procEmail = function(elementNo,formId){
		var self = checkForms;	
		var emailAddress = escape(self.allForms[formId].elements[elementNo].value);
		if(emailAddress.indexOf('%20') != -1 || emailAddress.indexOf('@') == -1 || emailAddress.indexOf('.') == -1){
			self.checkResult = false;
			self.displayErrorStyles(elementNo, formId);
			self.errorMessage += 'Please check the email address in \'' + self.allForms[formId].elements[elementNo].name  +'\' <br />';
		}		
	};
	this.procText = function(elementNo, formId){
		var self = checkForms;
		var inputText = self.allForms[formId].elements[elementNo].value;
		if (inputText.length < 3){
			self.errorMessage += 'Please provide text for \'' + self.allForms[formId].elements[elementNo].name + '\'<br />';
			self.checkResult = false;
			self.displayErrorStyles(elementNo,formId);
		}
	};
	this.procDecimal = function(elementNo, formId){
		var self = checkForms;
		if (isNaN(self.allForms[formId].elements[elementNo].value)|| self.allForms[formId].elements[elementNo].value == '' ){
			self.checkResult = false;
			self.errorMessage += 'Please Input correct values for ' + self.allForms[formId].elements[elementNo].name+ '.<br />';
			self.displayErrorStyles(elementNo, formId);
		}
		self.allForms[formId].elements[elementNo].value = (isNaN(parseFloat(self.allForms[formId].elements[elementNo].value))) ? '' : parseFloat(self.allForms[formId].elements[elementNo].value);
	};
	this.procInt = function(elementNo, formId){
		var self = checkForms;
		if(isNaN(self.allForms[formId].elements[elementNo].value) || self.allForms[formId].elements[elementNo].value ==''){
			self.checkResult = false;
			self.errorMessage += 'Please provide a number for \''+ self.allForms[formId].elements[elementNo].name + '\'.<br />';
			self.displayErrorStyles(elementNo, formId);
		}
		self.allForms[formId].elements[elementNo].value = (isNaN(parseInt(self.allForms[formId].elements[elementNo].value))) ? '' : parseInt(self.allForms[formId].elements[elementNo].value);
	};
	this.procPhone = function(elementNo,formId){
		var self = checkForms;
		var validPhone = true;
		self.formatPhone(self.allForms[formId].elements[elementNo]);
		if (self.allForms[formId].elements[elementNo].value.length < 12){validPhone = false;}
		for(var i = 0; i < self.allForms[formId].elements[elementNo].value.length; i++){
			if(self.allForms[formId].elements[elementNo].value.charAt(i) != '(' && self.allForms[formId].elements[elementNo].value.charAt(i) != ')' && self.allForms[formId].elements[elementNo].value.charAt(i) != '-' && self.allForms[formId].elements[elementNo].value.charAt(i) != ''){
				if(isNaN(self.allForms[formId].elements[elementNo].value.charAt(i))){validPhone = false;}
			}
		}
		if (validPhone == false){
			self.checkResult = false;
			self.errorMessage += 'Please provide a number for the '+ self.allForms[formId].elements[elementNo].name + '.<br />';
			self.allForms[formId].elements[elementNo].className = self.errorClass;
			self.displayErrorStyles(elementNo, formId);
		}	
	};
	this.displayErrorStyles = function(elementNo, formId){
		var self = checkForms;
		if(self.errorClass[formId]==''){
			self.allForms[formId].elements[elementNo].style.border = 'solid 1px #FF0000';
			self.allForms[formId].elements[elementNo].style.backgroundColor = '#FFEEEE';
		}else{
			self.allForms[formId].elements[elementNo].className = self.errorClass[formId];
		}
		if(self.focusInput == null){
			self.focusInput = document.getElementById(formId).elements[elementNo];
		}
	};
	this.cleanUp = function(){
		var self = checkForms;
		for(i=0;i < document.forms.length;i++){
			if(self.useAjax[document.forms[i].id] == true){
				self.callAjax[document.forms[i].id].abort();
			}
			self.stopAnim(document.forms[i].id);
		}
		self.reqInput			= null;
		self.callAjax			= null;
		self.eipAjax			= null;
		self.formAnimationCount = null;
		clearInterval(self.menuTimerID);
		self.menuTimerID = null;
	};
}
window.onload = checkForms.init;
window.onunload = checkForms.cleanUp;