/*
* 
* @author James Morrow 9/29/2009
*
*/
 

$(document).ready(function(){
	
	//alert(myForm);
	//add click handler to submit button	
	$("#imageSubmit").click(function(e){
		e.preventDefault();
		if ( $("#imageSubmit").hasClass('nextBtnGetArrowContainer') == true)
		{
			if(validate()!= true)
			return;
		}

		
		if ( $("#imageSubmit").hasClass('validateSchoolForm') == true)
		{	
			if (!validateSchool(myForm)) { return };
		}

		// TODO: this is not a good idea to hardcode form name here. Needs to be fixed
		validateForm('GetDetail');
		
		//$.blockUI({ message: '<img src="/com/assets/images/ajax-loader-large-circle.gif">'  });
		$.blockUI({ message: '<h3><img src="/com/assets/images/busy.gif" /> Just a moment...</h3>' });
	});
	
//masking a phone number field. This requires jquery.maskinginput plugin
$("#FullPhoneNumber").mask("(999) 999-9999");
$("#EvePhone").mask("999-9999");

	
function validateForm(formid,divid){
	
	//alert('validate form called');
	var serverLocation = top.location.host;
	var locationString = "http://"+ serverLocation + "/com/academix/service/validationRouter.cfc?wsdl";
	var serializedFormData=$('form#'+formid).serialize();
	var x;
	
	
	valid=false;
	$.get(locationString,
	{
		method : "processRequestProxy",
		inputData : serializedFormData,
		bAppReset : "0"
	},
	successReturn);
	
	
}


function js_RemoveChar(str,charToRemove)
{
	regExp = new RegExp("["+charToRemove+"]","g");
	return str.replace(regExp,"");
}


function successReturn(response, operationStatus){
	var resp = jQuery.trim(response);
	
	//alert(operationStatus);
	//alert(resp);
	//alert(resp.length);
			
	if (operationStatus == "success") {
		//alert('success!');
		if (resp.length >= 3) {
			//alert('error response Exists!');
			//alert(resp);
			//alert(resp.length);
			
			//show error message in sliding div
			scroll(0,0);
			resp = js_RemoveChar(resp, '"');
			$('#errorDisplayText').html(resp);
			$('#errorDisplayText').append("<div id='sliderClose'></div>");
			$("#errorDisplayText").slideDown("slow");

			$.unblockUI();
			
			//alert('enabled!');

			//add click handler to error close slider	
			$("#sliderClose").click(function(){
				$("#errorDisplayText").slideUp("slow");
			});
		}else{
			//submit form
			//alert('submitting!');
			//$.unblockUI();
			
			$('#errorDisplayText').hide();
			//$('form#'+document.forms[0].name).submit();
			// TODO: also form name hardcoding. Please fix.
			$('form#GetDetail').submit();
			
		}		
	} else {
		//alert('generic error - no success in connecting');
		//show error message in sliding div
		$.unblockUI();
		
		$('#errorDisplayText').html(operationStatus);
		$('#errorDisplayText').append("<div id='sliderClose'></div>");
		$("#errorDisplayText").slideDown("slow");
		
	}
}

function validateSchool(myForm)

{
	var arrFromFields = myForm;
	// TODO: knock-knock. who's there? form hardcoded
	data = document.GetDetail;

//alert(myForm);
	//if (schooldata != '')
	//  {
		for (i = 0; i < arrFromFields.length; i++)
		  {
			if (!data[arrFromFields[i]].disabled)
			  {
				formlabel = document.getElementById(myForm[i]).innerHTML;
				//stripping HTML
				formlabel = formlabel.replace(/(<([^>]+)>)/ig,""); 
				if (data[arrFromFields[i]].type == 'text' && data[arrFromFields[i]].value == '')
				  {
					alert('Please complete TEXT form at: \n\n'+formlabel);
					return false;
				  }
				if (data[arrFromFields[i]].type == 'checkbox' && data[arrFromFields[i]].checked == false)
				  {
					alert('Please complete form CHECKBOX at: \n\n'+formlabel);
					return false;
				  }
				if (data[arrFromFields[i]].type == 'select-one' || data[arrFromFields[i]].type == 'select-multiple')
				  {
					tmpvar1 = data[myForm[i]].selectedIndex;
					if (tmpvar1 <= 0 || data[arrFromFields[i]].options[tmpvar1].value == '')
						  {
							//alert(tmpvar1);
							alert('Please complete form SELECTION at: \n\n'+formlabel);
							return false;
						  }
				  }
				else
				  {
					if (data[arrFromFields[i]].length > 1)
					  {
						if (!data[arrFromFields[i]][0].disabled)
						  {
							checkall = false;
							for (j = 0; j < data[arrFromFields[i]].length; j++)
							  {
								if (data[arrFromFields[i]][j].checked)
									checkall = true;
							  }
							if (!checkall)
							  {
								alert('Please complete option form ' + data[arrFromFields[i]][0].type + '  at: \n\n'+formlabel);
								return false;
							  }
						  }
					  }
				  }
			  }
		  }

			if (data.Form_DisclaimerCheckbox && data.Form_DisclaimerCheckbox.checked == false)
			{
				alert('Please confirm you agree to have a representative of this school to  contact you regarding programs and registration.');
				return false;
			}
		
		//setting up fields to submit process the submission
		data.AddLead.value='Yes';
		data.VerifyResult.value='submit';
		return true;
}
	
	
});