


$.prototype.disable = function() {
	var element = $(this);
	element.attr ("readonly", "readonly");
}
$.prototype.enable = function() {
	var element = $(this);
	element.removeAttr ("readonly");
}

var IFARegestration={
	SendIFrameSizeToServer:function(height){
		// call back to server to get size required for iframe
		pageUrl = window.location.href;
		trySplit = pageUrl.split("?");
		queryString = trySplit[1];
		queryStringParts = queryString.split("&");
		
		frameGuid = "";
		
		
		for(var i = 0; i < queryStringParts.length; i++)
		{
			loopItem = queryStringParts[i];
			qsKey = loopItem.split("=")[0];
			qsValue = loopItem.split("=")[1];

			if(qsKey == "frameGuid") {
				frameGuid = qsValue;
			}
		}
		currentServerRequest = csSendServerRequest("/processors/IFARegistrationProcessor/SaveIFrameSize?frameGuid=" + frameGuid + "&height=" + height + "&rqsb=" + new Date().getTime(),IFARegestration.GetDataHandler);
	},
	
	GetDataHandler:function(data){
		var body = $$("body")[0];
		
		body.style.height = data + "px";
		
	},
	
	SetParentIFrameSize:function(){
	
		//var form = $$("html")[0];
		var formHeight = document.body.scrollHeight ; //form.getHeight() + 40;
		
		
	 

		//var body = $$("body")[0];
		//var formHeight = body.getHeight();
		
		// wont work cross domain... 
		//var iFrame = parent.document.getElementById("iframe");
		//iFrame.style.height = formHeight+"px";
		
		// so it gets a little more complicated :(
		IFARegestration.SendIFrameSizeToServer(formHeight);
	},
	
	GetRadioValue:function(name){
		var result = null;
		var form = $(".formBuilder")[0];
		var radios = $(".formBuilder input");
		
		for(var i=0;i<radios.length;i++){
			var radio = radios[i];
			if ($(radio).attr ("name") == name) {
				var rValue = $(radio).val();
				if (rValue != null) {
					result = rValue;
				}
			}
			/*
			if(radio.getAttribute("name") == name){
				alert ($(radio).val());
				if($F(radio.id) != null){
					result = $F(radio.id);
					break;
				}
			}*/
		}
		return(result);
	},
	
	SetFormValidation:function(validatorFunction){
		var form = $$(".formBuilder")[0];
		form.onsubmit=validatorFunction;
	},
	
	GetFormName:function(){
		var form = $$(".formBuilder")[0];
		var formName = form.id;
		
		return(formName);
	},
	
	ChangeValidationOnClick:function(buttonName,validator){
		var button = $(buttonName);
		button.onclick = function(){
			IFARegestration.SetFormValidation(validator);
		}
	},
	
	DisableUnnecessaryValidation:function(){
		IFARegestration.ChangeValidationOnClick("button_previous",null);	
		IFARegestration.ChangeValidationOnClick("button_cancel",null);	
	},
	
	SetFieldSetVisibility:function(name,visible){
		var fieldSet = $("#" + name);
		if(visible){
			fieldSet.css ("display", "block");
			//fieldSet.setStyle({display:"block"});
		} else {
			fieldSet.css ("display", "none");
			//fieldSet.setStyle({display:"none"});
		}
	},

	// ############################## page specific functions ##############################

	BeforeYouRegisterOnLoad:function(){
		$('#haveAgencyNumber_1').bind ("click",  function(e) {
			$('#agencyNumber').disable(); 
			$('#agencyNumber').val("");
			}
		 );
		
		$('#haveAgencyNumber_2').bind ("click",  function(e) {
			 $('#agencyNumber').enable(); 
		}
		);
		
		//IFARegestration.SetFormValidation(IFARegestration.ValidateBeforeYouRegister);
	},
	
	ValidateBeforeYouRegister:function(){
		var ok = true;
		var formName = IFARegestration.GetFormName();
		
		IFARegestration.GetRadioValue("haveAgencyNumber");
		
		if (ok){
			// check email
			ok = validate(ok,formName,'emailAddress','email','Please enter your valid email address.','mandatory','','','','','');
			if($('emailAddress').value != $('emailAddressConfirm').value){
				ok = false;
				alert("Please enter your email address with confirmation.");
			} 
		}
		
		if (ok){
			// check agency selection
			ok = validate(ok,formName,'haveAgencyNumber','','Please select whether you have an agency number or not.','mandatory','','','','','');
		}
		
		
		if(ok){
			// neet to check if they have entered an agency number if they say they have one..
			var haveAgencyNumber = IFARegestration.GetRadioValue("haveAgencyNumber");
			switch(haveAgencyNumber){
				case "yes"	:
					//ok = validate(ok,formName,'agencyNumber','numeric','Please enter your agency number.','mandatory','','','','','');				
					break;
				case "no"	:
					break;
			}
		}
		return(ok);
	},
	
	NameDetailsOnLoad:function(){
		// add onclick handler to fsa number check button to disable validation if clicked..
		IFARegestration.DisableUnnecessaryValidation();
	},
	
	BusinessDetailsOnLoad:function(){
		$("#button_check").bind ("click", function() {
			$(".formBuilder").removeAttr ("onsubmit");
			window.setTimeout (function() {
				var element = $('.formBuilder')[0];
				element.submit();
				}
			, 100);
			return false;
			
		});
		
		IFARegestration.SetParentIFrameSize();
		
		// add onclick handler to fsa number check button to disable validation if clicked..
		IFARegestration.ChangeValidationOnClick("button_check",IFARegestration.ValidateBusinessDetailsFSACheck);	
		IFARegestration.DisableUnnecessaryValidation();
	},
	
	ValidateBusinessDetailsFSACheck:function(){
		var ok = true;
		var formName = IFARegestration.GetFormName();

		if (ok){
			ok = validate(ok,formName,'yourName','','Please enter your name.','mandatory','','','','','');
		}
		if (ok){
			ok = validate(ok,formName,'fsaNumber','','Please enter your FSA number.','mandatory','','','','','');
		}
		return(ok);
	},
	
	NetworkOnLoad:function(){
		// hide the bits that depend on if you are in a network
		var memberOfNetwork = IFARegestration.GetRadioValue("networkMember");
		
		switch(memberOfNetwork){
			case "yes":
				IFARegestration.SetFieldSetVisibility("networkElements",true);
				IFARegestration.SetFieldSetVisibility("noNetworkElements",false);
				break;
			case "no":
				IFARegestration.SetFieldSetVisibility("networkElements",false);
				IFARegestration.SetFieldSetVisibility("noNetworkElements",true);
				break;
			default:
				IFARegestration.SetFieldSetVisibility("networkElements",false);
				IFARegestration.SetFieldSetVisibility("noNetworkElements",false);
				break;
		}
		
		// add onclick to network member radio buttons 
		var networkMember1 = $("#networkMember_1");
		var networkMember2 = $("#networkMember_2");
		
		//networkMember1.onclick = IFARegestration.NetworkMember1OnClick;
		//networkMember2.onclick = IFARegestration.NetworkMember2OnClick;
		networkMember1.bind ("click", function() {
			IFARegestration.NetworkMember1OnClick();
		});
		networkMember2.bind ("click", function() {
			IFARegestration.NetworkMember2OnClick();
		});
				
		IFARegestration.DisableUnnecessaryValidation();
	
		// we probably need to do something here about the sizing in ie6..
		// setting a size on the element that is breaking can often give it a kick to resize itself properly..
		
		// this is going to be trial and error but maybe setting a height on each div in the page will give it a kick..
		$("div").each(
			function(index, item){
				var htmlElement = $(item);
				htmlElement.css("height", htmlElement.css("height"));
			}
		);
		
		//alert("here");
	},
	
	NetworkMember1OnClick:function(){
		if($("#networkMember_1").val() == "no"){
			IFARegestration.SetFieldSetVisibility("networkElements",false);
			IFARegestration.SetFieldSetVisibility("noNetworkElements",true);
			
			IFARegestration.SetFormValidation(IFARegestration.ValidateNoNetworkElements);
		} else {
			IFARegestration.SetFieldSetVisibility("networkElements",true);
			IFARegestration.SetFieldSetVisibility("noNetworkElements",false);
			
			IFARegestration.SetFormValidation(IFARegestration.ValidateNetworkElements);
		}
		IFARegestration.SetParentIFrameSize();
	},
	
	NetworkMember2OnClick:function(){
		if($("#networkMember_2").val() == "yes"){
			IFARegestration.SetFieldSetVisibility("networkElements",true);
			IFARegestration.SetFieldSetVisibility("noNetworkElements",false);
			
			IFARegestration.SetFormValidation(IFARegestration.ValidateNetworkElements);
		} else {
			IFARegestration.SetFieldSetVisibility("networkElements",false);
			IFARegestration.SetFieldSetVisibility("noNetworkElements",true);
			
			IFARegestration.SetFormValidation(IFARegestration.ValidateNoNetworkElements);
		}
		IFARegestration.SetParentIFrameSize();
	},
	
	ValidateNetworkElements:function(){
		var ok = true;
		var formName = IFARegestration.GetFormName();
		
		var selectedNetwork = $F("selectNetwork");
		
		if(selectedNetwork == "none"){
			// check network name
			ok = validate(ok,formName,'enterNetwork','','Please enter your network.','mandatory','','','','','');
		}
		return(ok);
	},
	
	ValidateNoNetworkElements:function(){
		var ok = true;
		var formName = IFARegestration.GetFormName();
		
		if(ok){
			// check agency selection
			ok = validate(ok,formName,'haveMasterAgency','','Please select wether you have a master agency.','mandatory','','','','','');
		}
		
		if(ok){
			// neet to check if they have entered an master agency number if they say they have one..
			var haveAgencyNumber = IFARegestration.GetRadioValue("haveMasterAgency");
			switch(haveAgencyNumber){
				case "yes"	:
					ok = validate(ok,formName,'masterAgencyNumber','numeric','Please enter your master agency number.','mandatory','','','','','');				
					break;
				case "no"	:
				
					break;
			}
		}
		return(ok);
	},
	
	AgencyInformationOnLoad:function(){
		//IFARegestration.SetFormValidation(IFARegestration.ValidateAgencyInformation);
		IFARegestration.SetParentIFrameSize();
	},
	
	ValidateAgencyInformation:function(){
		var ok = true;
		var formName = IFARegestration.GetFormName();
		
		// radio - previousRegistrationWithdrawn
		// memo - withdrawnRegistrationDetails
		ok = validate(ok,formName,'previousRegistrationWithdrawn','','Please answer question 1.','mandatory','','','','','');
		var previousRegistrationWithdrawn = IFARegestration.GetRadioValue("previousRegistrationWithdrawn");
		if(previousRegistrationWithdrawn != null){
			previousRegistrationWithdrawn = previousRegistrationWithdrawn.toLowerCase();
		}
		if(previousRegistrationWithdrawn == "yes"){
			ok = validate(ok,formName,'withdrawnRegistrationDetails','','Please give details.','mandatory','','','','','');				
		}
		
		// radio - agencyRefused
		// memo - agencyRefusedDetails
		ok = validate(ok,formName,'agencyRefused','','Please answer question 2.','mandatory','','','','','');
		var agencyRefused = IFARegestration.GetRadioValue("agencyRefused");
		if(agencyRefused != null){
			agencyRefused = agencyRefused.toLowerCase();
		}
		if(agencyRefused == "yes"){
			ok = validate(ok,formName,'agencyRefusedDetails','','Please give details.','mandatory','','','','','');				
		}

		if (ok){
			// check agency selection
			ok = validate(ok,formName,'indemnityTerms','','Please answer question 3.','mandatory','','','','','');
		}

		return(ok);	
	},
	
	ContactsOnLoad:function(){
		IFARegestration.SetParentIFrameSize();
		
		$('useBusinessAddress').onclick = function() { 
			PopulateContacts();
		}
		
	},
	
	ContactingYouOnLoad:function(){
		IFARegestration.SetParentIFrameSize();
	},

	DeclarationOnLoad:function(){
		IFARegestration.SetParentIFrameSize();
		IFARegestration.DisableUnnecessaryValidation();
	}
}




//remove client side validation for the back button
addLoader (function() {
	if (document.getElementById ("button_previous")) {
		var backButton = $("#button_previous");
		backButton.bind ("click", function() {
			var form = $(".formBuilder");
			if (form[0]) {
				var onSubmit = form.attr ("onsubmit");
				form.removeAttr ("onsubmit");
				window.setTimeout (function() {
					try {
						//add a hidden previous button to post that will be picked up server side
						form.append ($('<input type="hidden" name="button_previous" id="button_previous" value="Previous" />'));
						form[0].submit();
					} catch (e) {
						//alert (e.message);
					}
				}, 100);
			}
		});
	}
});
