$j(document).ready(function(){
	$j("#step1").show();
	$j("#step2").hide();
	$j("#step3").hide();

	$j("#st1").addClass("active");
	$j("#apply_tabs").show();
	
	var formRules = {
		rules: {
			/* Step 1 */
			firstname: "required",
			lastname: "required",
			phone: {
				required: true,
				digits: true
			},
			/* Step 2 */
			housenumber: "required",
			town: "required",
			county: "required",
			postcode: "required",
			email: {
				required: true,
				email: true
			},
			/* Step 3 */
			ownpur: "required",
			live: "required",
			terms_accept: "required"
		}
	};

//Forwards to step two
	$j(".toStepTwo").click(function(){
		$j("#apply_now").validate(formRules);
		var st1Val = $j("#firstname, #lastname, #phone").valid();
		if(st1Val){
			$j("#st1").removeClass("active");
			$j("#st2").addClass("active");
			showTwo();
			return false;
		} else {
			return false;
		}
	});
//Forwards to step three
	$j(".toStepThree").click(function(){
		$j("#apply_now").validate(formRules);
		var st2Val = $j("#housenumber, #town, #county, #postcode, #email, #emailconfirm").valid();
		if(st2Val){
			$j("#st2").removeClass("active");
			$j("#st3").addClass("active");
			showThree();
			return false;
		} else {
			return false;
		}
	});
//Forwards to Submit
	$j("#apply_submit").click(function(){
		var st3Val = $j("#live, #, #ownpur").valid();
		if(!st3Val){
			return false;
		}
	});
//Back to step one
	$j(".backStepOne").click(function(){
		showOne();
		$j("#st2").removeClass("active");
		$j("#st1").addClass("active");
		return false;
	});
//Back to step two
	$j(".backStepTwo").click(function(){
		showTwo();
		$j("#st3").removeClass("active");
		$j("#st2").addClass("active");
		return false;
	});
	
//Step 1 anchor
	$j("#st1 a").click(function(){
		showOne();
		$j("#st2").removeClass("active");
		$j("#st3").removeClass("active");
		$j("#st1").addClass("active");
		return false;
	});
//Step 2 anchor
	$j("#st2 a").click(function(){
		$j("#apply_now").validate(formRules);
		var st1Val = $j("#firstname, #lastname").valid();
		if(st1Val){
			showTwo();
			$j("#st1").removeClass("active");
			$j("#st3").removeClass("active");
			$j("#st2").addClass("active");
		return false;
		} else {
			return false;
		}
	});
//Step 3 anchor
	$j("#st3 a").click(function(){
		$j("#apply_now").validate(formRules);
		var st1Val = $j("#firstname, #lastname, #dob").valid();
		var st2Val = $j("#housenumber, #town, #county, #postcode, #phone, #email").valid();
		if(st1Val && st2Val){
			showThree();
			$j("#st1").removeClass("active");
			$j("#st2").removeClass("active");
			$j("#st3").addClass("active");
			return false;
		} else {
			return false;
		}
	});
	function showOne(){
		$j("#step1").show();
		$j("#step2").hide();
		$j("#step3").hide();
	}
	function showTwo(){
		$j("#step1").hide();
		$j("#step2").show();
		$j("#step3").hide();
	}
	function showThree(){
		$j("#step1").hide();
		$j("#step2").hide();
		$j("#step3").show();
	}
	
function radioEvent() {
	var addressCount = 0;//create the addresscount array.
	
	var grabAddress = document.getElementsByTagName("div");
	/*Turn all the divs off*/
	for (i=0; i < grabAddress.length; i++) {
		if(grabAddress[i].className == "anotheraddresson"){
			grabAddress[i].className = "anotheraddressoff";
		}
	}
		function showAddress() {
			var linkDiv = document.createElement("div");
			linkDiv.setAttribute("class", "addaddress");
			
			var linkAnchor = document.createElement("a");
			var linkAnchorText = document.createTextNode("Add another address");
			linkAnchor.appendChild(linkAnchorText);
			linkAnchor.setAttribute("href", "#");
			linkAnchor.setAttribute("id", "aID");
			
			var phoneDiv = document.getElementById("phoneDiv");
			var parent = phoneDiv.parentNode;
			
			parent.insertBefore(linkAnchor, phoneDiv);
			
			for (i=0; i < grabAddress.length; i++) {
				if(grabAddress[i].className == "anotheraddressoff"){
					grabAddress[i].className = "anotheraddresson";
					var aID = document.getElementById("aID");
					grabAddress[i].appendChild(linkDiv);
					$j("#aID").click(function(){
						addAddress();
						return false;
					});
				}
			}
		}
		
	
}
radioEvent();
});
/*
function enableFormShowHide() {
	if (!document.getElementsByTagName) return;
	if(!$("apply")) return;
	$("step2").className="off";
	$("step3").className="off";
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.className == "link") {
			addEvent(anchors[i], 'click', 
				function() {
					doFormShowHide(this);
				}
			);
		}
	}
}
*/
/*
function doFormShowHide(objectRef){
	var previous_step = 1;
	var contentID = objectRef.toString().substr(objectRef.toString().indexOf("#")+1);
	var tab_list = $$("#apply_tabs li"); // create an array of li elements in div with id of "apply_tabs", using prototype 
	for(x=1;x<5;x++) {// turn all tabs off and hide content
		if($("step"+x).className == "on") previous_step = x; // save previous step
		$("step"+x).className = "off";
		$(tab_list[x-1]).className="";
	}
	
	// validate each section before next / previous 
    // iterate through each fieldset and grab all input based on their id
    // use this next line maybe if next stuff fails?
    //document.getElementByClassName("required", $("step"+x));
    
	var valid = false;
	var next_step = objectRef.toString().substr(objectRef.toString().indexOf("#")+5);// plus 5 because "step" is 4 chars + 1 for "#"

	var validate_input_array = $$("#step"+previous_step+" input");
	for(var i=0;i<validate_input_array.length;i++) {
		valid = Validation.validate(validate_input_array[i]);
		if(!valid) break;
	}
    if(next_step < previous_step) valid = true;
    if(valid) {
		// turn selected tab and content on
		$(contentID).className="on";
		$(tab_list[next_step-1]).className="active";
	} else {
			$("step"+previous_step).className="on";
			$(tab_list[previous_step-1]).className="active";
	}
}

//create function, it expects 2 values.
function insertAfter(newElement,targetElement) {
	//target is what you want it to go after. Look for this elements parent.
	var parent = targetElement.parentNode;
	
	//if the parents lastchild is the targetElement...
	if(parent.lastchild == targetElement) {
		//add the newElement after the target element.
		parent.appendChild(newElement);
		} else {
		// else the target has siblings, insert the new element between the target and it's next sibling.
		parent.insertBefore(newElement, targetElement.nextSibling);
		}
}
*/
