/*



Custom Functions

Uses http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
*/
Event.onDOMReady(function(){
	init();
});
/*
	


This is the function that attached the Javascript functions to the page
*/
function init(){


	/*
	
	
	
	Makes Internet Explorer render 32 bit PNGs properly
	*/
	IE_PNG_Hack("#head img");




	/*
	
	
	
	Forms
	*/
		/*
		
		
		
		Focus on an input
		*/
		$$("form input").each(function(node, i){
			node = $(node);
			node.onfocus = node.onblur = function(){
	
				if (node.hasClassName("focus")) {
					node.removeClassName("focus");
				} else {
					node.addClassName("focus");
				}
	
			};
			
			node.onclick = function(){
				node.value = "";
			};
	
		});
		/*
		
		
		Simple form validation
			onsubmit, feed the ValidateForm() function an array containing id's of form inputs you wish to validate
			
		*/
		$$(".CustomerForm").each(function(node, i){
			node = $(node);
			
			/*
			
			clean white space text node elements!
			node.cleanWhitespace(); // breaks in ie
			*/
			node.onsubmit = function(){
				/*
				
				an array of id's that will always be validated
				*/
				FormIDArray = new Array(
					"customer_name_first",
					"customer_name_last",
					"customer_phone_daytime",
					"customer_email",
					"address_1_address",
					"address_1_move_in_date",
					"address_1_city",
					"address_1_state",
					"address_1_zip",
					"address_2_address",
					"address_2_city",
					"address_2_state",
					"address_2_zip"
				);
				/*
				
				validate form, cancel submission if it vails validation
				*/
				if(ValidateForm(FormIDArray) == false){
					this.blur;
					return false;
				}
			}
		});

	
}

/*

This function makes 32 bit PNG's with transparency work
*/	
function IE_PNG_Hack(selector){

	if (navigator.userAgent.indexOf("MSIE ") == -1){
		
		return false;
		
	} else {

		$$(selector).each(function(node, i){
					
			var node = $(node);
			
			if (node.src.indexOf("png") == -1) {
				
				/*node.runtimeStyle.filter = "";*/
				
				return;
			}
			
			var oldSrc = node.src;
			
			node.src = "http://www.frogmanclothing.com/images/site/transparent.gif";
			
			node.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldSrc + "',sizingMethod='scale')";
			
		});
		
	}
	
}

/*


A Form Validation Function
That decorates the page with feedback
	<div>
		<label for="asdf">asdf</label>
		<input id="asdf" name="asdf" /><br />
	</div>
*/
function ValidateForm(vf_FormIDArray){
	ValidationError = $A();
	$A(vf_FormIDArray).each(function(node, i){
		/*
		
		If it's an actual node
		*/
		if($(node)){
			node = $(node);
			parent = $(node.parentNode);
			/*
			
			if it's empty
			*/
			if((node.value == null) || (node.value == "")){
				ValidationError[i] = true;
				/*
				
				if the parent div doesn't already have the "ValidationError" class, add it
				*/
				if(!parent.hasClassName("ValidationError")){
					parent.addClassName("ValidationError");
				}
			/*

			else it's not empty 
			*/
			} else {
				ValidationError[i] = false;
			}
			
			/*
			
			when the input is blurred, remove the ValidationError class if ...
			*/
			node.onblur = function(){
				/*
				
				if the input has something in it and parent div has the "ValidationError" class, remove it
				*/
				parent = $(node.parentNode);
				if (node.value.length > 0){
					if(parent.hasClassName("ValidationError")){
						parent.removeClassName("ValidationError");
					}
				}
				
				/*
				
				Toggles the "focusing" class
				*/
				if(node.hasClassName("focus")){
					node.removeClassName("focus");
				} else {
					node.addClassName("focus");
				}
			}
		}
	});
	
	/*
	
	if validation failed
	*/
	if(ValidationError.any()){
		alert("Please fill out the required form fields.");
		this.blur();
		location.href = "#totop";
		return false;
	}

}


