var Validation = {};

	Validation.errMsg_EmailInvalid = "Please enter a valid email";
	Validation.errMsg_EmailInUse = "The email address you've provided is already in our system. Please contact your channel manager if you'd like to apply for another position.";
	Validation.errMsg_FirstNameInvalid = "Please enter your first name"; /*** character count = "1" ***/
	Validation.errMsg_LastNameInvalid = "Please enter your last name"; /*** character count = "1" ***/
	Validation.errMsg_PhoneEmpty = "Please enter a valid phone number";
	Validation.errMsg_CityInvalid = "Please enter a valid city"; /*** character count = "1" ***/
	Validation.errMsg_StateNotSelected = "Please select your state";
	Validation.errMsg_GenderNotSelected = "Please select a gender";
	Validation.errMsg_CredentialsEmpty = "Please enter your qualifications";
	Validation.errMsg_ArticleTopicsEmpty = "Please enter a sample article topic";/*** character count = "1" ***/
	Validation.errMsg_SampleTitleEmpty = "Please enter a sample article title";/*** character count = "1" ***/
	Validation.errMsg_SampleArticleEmpty = "Please enter a sample article";/*** character count = "1" ***/
	Validation.errMsg_BioEmpty = "Your bio must be at least 100 letters long";/*** character count = "100" ***/
	Validation.errMsg_YourEmailInvalid = "Please enter a valid email address";
	Validation.errMsg_FriendsEmailInvalid = "Please enter a valid email address";
	Validation.errMsg_YourNameInvalid = "Please enter your name";
	Validation.errMsg_FriendsNameInvalid = "Please enter friend's name";
	Validation.errMsg_WriteInTopicTitleEmpty = "Please enter a topic title";
	Validation.errMsg_invalidOnlineExamplesURL = "Please enter a valid url";
	Validation.errMsg_SuggestFriendAgree = "You must agree to the terms of use";
	Validation.errMsg_WindowUnload = "'are you really really sure you want to bail?'";
	Validation.errMsg_TopicNotSelected = "Please select a topic to examine";
	Validation.errMsg_EditionNotSelected = "Please select an edition";
	Validation.errMsg_CategoryNotSelected = "Please select a category";

	Validation.errMsg_Skills_Photo = "Please rate your skill level for preparing, uploading photos";
	Validation.errMsg_Skills_Links = "Please rate your skill level for creating hyperlinks";
	Validation.errMsg_Skills_Video = "Please rate your skill level for embedding videos";
	Validation.errMsg_Skills_Promotion = "Please rate your skill level for online promotion (e.g. Digg)";
	
	Validation.errMsg_Referral = "Please tell us how you heard about Examiner.com";
	Validation.errMsg_Referral_Other = "Please fill out other";
	
	Validation.errMsg_ConvictQuestion = "Please list offences to continue.";
	Validation.errMsg_ConvictDescription = "Please elaborate on offences, including charge, date and location.";
	Validation.errMsg_ConvictDescriptionToLong = "Please do not exceed 255 characters.";
	
	Validation.phoneNumber_AreaCode = "Phone_AreaCode";
	Validation.phoneNumber_Prefix = "Phone_Prefix";
	Validation.phoneNumber_Suffix = "Phone_Suffix";

	Validation.validateStringLength = function(elem,errorElem,errorTextElem,errorMsg,len){
		var val = $(elem)[0].value;
		if(val.length < len){
			$(errorTextElem).html(errorMsg);	
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else{
			return true;
		}
	}

	Validation.validateMaxStringLength = function(elem,errorElem,errorTextElem,errorMsg,len){
		var val = $(elem)[0].value;
		if(val.length > len){
			$(errorTextElem).html(errorMsg);	
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else{
			return true;
		}
	}

	Validation.validateApplicantEmail = function(elem,errorElem,errorTextElem){

		var addr = $(elem)[0].value;
		
		if (addr.indexOf("@examiner.com") != -1)
		{
			return true;
		}

		if(!Validation.isValidEmail(addr)){
			$(errorTextElem).html(Validation.errMsg_EmailInvalid);
			Validation.showErrorMessage(errorElem);
			$("#btn_saveStepTwo").attr("disabled", "disabled"); 
			return false;
		}
		else if(Validation.isUniqueEmailAddr(addr)){
			$(errorTextElem).html(Validation.errMsg_EmailInUse);
			Validation.showErrorMessage(errorElem);
			$("#btn_saveStepTwo").attr("disabled", "disabled"); 
			return false;
		}
		else{
			$("#btn_saveStepTwo").removeAttr("disabled");
			return true;
		}
	}

	Validation.validatePhoneNumber = function(elemAreaCode,elemPreFix,elemSuffix,errorElem,errorTextElem){
		var areaCode = $(elemAreaCode)[0].value;
		var preFix = $(elemPreFix)[0].value;
		var suffix = $(elemSuffix)[0].value;
		
		if (areaCode.length < 3 || preFix.length < 3 || suffix.length < 4)
		{
			$(errorTextElem).html(Validation.errMsg_PhoneEmpty);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else if (!Validation.isNumeric(areaCode) || !Validation.isNumeric(preFix) || !Validation.isNumeric(suffix))
		{
			$(errorTextElem).html(Validation.errMsg_PhoneEmpty);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
			return true;
		}
	}

	Validation.validatePhonePart = function(elem,errorElem,errorTextElem)
	{
		var elemName = $(elem)[0].name;
		var elemValue = $(elem)[0].value;
		var result = true;

		if (elemName == Validation.phoneNumber_AreaCode || elemName == Validation.phoneNumber_Prefix)
		{
			if (elemValue.length < 3)
			{
				result = false;
			}
		}
		else if (elemName == Validation.phoneNumber_Suffix)
		{
			if (elemValue.length < 4)
			{
				result = false;
			}
		}
		
		if (!result)
		{
			$(errorTextElem).html(Validation.errMsg_PhoneEmpty);
			Validation.showErrorMessage(errorElem);
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
		}
		
		return result;
	}
	
	Validation.validateVipPhoneNumber = function(elemPhoneNumber,errorElem,errorTextElem){
		var phoneNumber = $(elemPhoneNumber)[0].value;
		
		phoneNumber = phoneNumber.replace("-","");
		phoneNumber = phoneNumber.replace(" ","");
		phoneNumber = phoneNumber.replace("(","");
		phoneNumber = phoneNumber.replace(")","");
		
		if (phoneNumber.length < 10)
		{
			$(errorTextElem).html(Validation.errMsg_PhoneEmpty);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else if (!Validation.isNumeric(phoneNumber))
		{
			$(errorTextElem).html(Validation.errMsg_PhoneEmpty);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
			return true;
		}
	}

	Validation.validateFriendsEmail = function(elem,errorElem,errorTextElem){
		
		var addr = $(elem)[0].value;
		if(!Validation.isValidEmail(addr)){
			
			$(errorTextElem).html(Validation.errMsg_FriendsEmailInvalid);
			Validation.showErrorMessage(errorElem); 
			return false;
		}
		else{
			return true;
		}
	}

	Validation.validateCheckbox = function(elem, errorElem, errorTextElem, errorMsg){
		if($(elem).attr('checked')){
			return true;
		}
		else{
			$(errorTextElem).html(errorMsg);
			Validation.showErrorMessage(errorElem); 
			return false;
		}
	}

	Validation.validateRadioButtons = function(elem, errorElem, errorTextElem, errorMsg){
		var result = false;
		
		for(var i = 0, n = $(elem).length; i < n; ++i)
		{
			if($(elem)[i].checked){
				result = true;
			}
		}
		
		if (result)
		{
			Validation.hideErrorMessage(errorElem);	
		}
		else
		{
			$(errorTextElem).html(errorMsg);
			Validation.showErrorMessage(errorElem); 			
		}
		
		return result;
	}

	Validation.validateState = function(elem,errorElem,errorTextElem){
		
		var state = $(elem).attr("selectedIndex");

		if(state == "0"){
			$(errorTextElem).html(Validation.errMsg_StateNotSelected);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else{
			Validation.hideErrorMessage(errorElem);
			return true;
		}
	}
	
	Validation.validateEdition = function(elem,errorElem,errorTextElem){
		
		var edition = $(elem).attr("selectedIndex");

		if(edition == "0"){
			$(errorTextElem).html(Validation.errMsg_EditionNotSelected);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else{
			Validation.hideErrorMessage(errorElem);
			return true;
		}
	}

	Validation.validateCategory = function(elem,errorElem,errorTextElem){
		
		var category = $(elem).attr("selectedIndex");

		if(category == "0"){
			$(errorTextElem).html(Validation.errMsg_CategoryNotSelected);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else{
			Validation.hideErrorMessage(errorElem);
			return true;
		}
	}

	Validation.countBioChars = function(elem,errorElem,errorTextElem,maxLength){
		
		Validation.hideErrorMessage("#error_Bio");
		var counter = $("#bioRemLen");
		
		var bioMaxLength = maxLength;
		var bioLength = $(elem).val().length;
		
		var remaining = bioMaxLength - bioLength;
		
		if (remaining < 0)
		{
			remaining = 0;	
		}
		
		if (bioLength > bioMaxLength) // if too long...trim it!
		{
			$(elem).val($(elem).val().substring(0, bioMaxLength));
		}
		
		counter.val(remaining);
	}

	Validation.isEmailValid = function(elem,errorElem,errorTextElem){
		if(!Validation.isValidEmail(addr)){
			var errorMsg = Validation.errMsg_EmailInvalid;
			$(errorTextElem).html(errorMsg);
			//sendFormEvent("e", "Application Form", "",errorMsg);
			Validation.showErrorMessage(errorElem);
		}
	}

	Validation.validateOnlineExamples = function(elem,errorElem,errorTextElem){
		if($(elem).val() != "" && !Validation.isValidURL($(elem).val())){
			var errorMsg = Validation.errMsg_invalidOnlineExamplesURL;
			$(errorTextElem).html(errorMsg);
			//sendFormEvent("e", "Application Form", "",errorMsg);
			Validation.showErrorMessage(errorElem);
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
		}
	}

	Validation.validateSelectedTopic = function(topicJson,errorElem,errorTextElem)
	{
		var raiseError = false;

		if (topicJson != null)
		{
			if (topicJson == '{"TopicList":[]}')
			{
				raiseError = true;
			}
		}
		else
		{
			raiseError = true;	
		}
		
		if (raiseError)
		{
			$(errorTextElem).html(Validation.errMsg_TopicNotSelected);
			Validation.showErrorMessage(errorElem);
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
		}
	}
	
	Validation.validateCity = function(elem,errorElem,errorTextElem,errorMsg,len)
	{
		var raiseError = false;
		var city = $(elem).val();
		
		if (!Validation.validateStringLength(elem,errorElem,errorTextElem,errorMsg,len))
		{
			$(errorTextElem).html(errorMsg);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else if (Validation.isNumeric(city))
		{
			$(errorTextElem).html(errorMsg);
			Validation.showErrorMessage(errorElem);
			return false;
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
			return true;	
		}
	}
	
	Validation.validateReferral = function(errorElem,errorTextElem)
	{
		var ReferredByResult = $("#ReferredBy").val();
		var ExaminerIDResult = $("#ExaminerID").val();
		
		if (ReferredByResult.length < 1 && ExaminerIDResult.length < 1)
		{
			$(errorTextElem).html(Validation.errMsg_Referral);
			Validation.showErrorMessage(errorElem);

			return false;	
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
			return true;
		}
	}
	
	Validation.validateConvictQuestion = function(convictQuestionElem,convictDescElem,errorElem,errorTextElem)
	{
		if (convictQuestionElem.length > 0)
		{
			Validation.validateRadioButtons(convictQuestionElem,errorElem,errorTextElem,Validation.errMsg_ConvictQuestion);
			
			if (!convictQuestionElem[1].checked && convictQuestionElem[0].checked)
			{
				Validation.validateStringLength(convictDescElem,errorElem,errorTextElem,Validation.errMsg_ConvictDescription,21);
			}
		}
		else
		{
			Validation.hideErrorMessage(errorElem);
		}
	}

	Validation.ValidateStep = function(step)
	{
		var jumpAnchor = "";
		
		switch (step)
		{
			case "StepOne":
				Validation.validateSelectedTopic(topics.returnTopicsJSON(),"#error_TopicNotSelected","#TopicNotSelectedErrorText");
				break;
			case "StepTwo":
				Validation.validateReferral("#error_Referral","#referralErrorText");
				Validation.validateApplicantEmail($("input[name=ApplicantEmail]"),"#error_ApplicantEmail","#emailErrorText",0);
				Validation.validateStringLength($("input[name=FirstName]"),"#error_FirstName","#firstNameErrorText",Validation.errMsg_FirstNameInvalid,1);
				Validation.validateStringLength($("input[name=LastName]"),"#error_LastName","#lastNameErrorText",Validation.errMsg_LastNameInvalid,1);
				Validation.validateCity($("input[name=City]"),"#error_City","#CityErrorText",Validation.errMsg_CityInvalid,1);
				Validation.validateState($("select[name=State]"),"#error_State","#StateErrorText");
				Validation.validatePhoneNumber($("input[name=Phone_AreaCode]"),$("input[name=Phone_Prefix]"),$("input[name=Phone_Suffix]"),"#error_PhoneNumber","#PhoneNumberErrorText");
				Validation.validateRadioButtons($("input[name=Gender]"),"#error_Gender","#GenderErrorText",Validation.errMsg_GenderNotSelected);
				break;
			case "StepThree":
				Validation.validateRadioButtons("input[name=Photos]","#error_SkillsPhoto","#skillsPhotoErrorText",Validation.errMsg_Skills_Photo);
				Validation.validateRadioButtons("input[name=Links]","#error_SkillsLinks","#skillsLinksErrorText",Validation.errMsg_Skills_Links);
				Validation.validateRadioButtons("input[name=Video]","#error_SkillsVideo","#skillsVideoErrorText",Validation.errMsg_Skills_Video);
				Validation.validateRadioButtons("input[name=Promotion]","#error_SkillsPromotion","#skillsPromotionErrorText",Validation.errMsg_Skills_Promotion);
				Validation.validateStringLength($("textarea[name=Credentials]"),"#error_Credentials","#credentialsErrorText",Validation.errMsg_CredentialsEmpty,2);
				Validation.validateStringLength($("textarea[name=TopicsToExamine]"),"#error_TopicsToExamine","#topicsToExamineErrorText",Validation.errMsg_ArticleTopicsEmpty,2);
				Validation.validateOnlineExamples($("input[name=OnlineExamples]"),"#error_OnlineExamples","#onlineExamplesErrorText",Validation.errMsg_invalidOnlineExamplesURL);
				Validation.validateConvictQuestion($("input[name=ConvictQuestion]"),$("textarea[name=ConvictDescription]"),"#error_ConvictDescription","#ConvictDescriptionErrorText");
				break;
			case "StepFour":
				Validation.validateStringLength($("input[name=SampleTitle]"),"#error_SampleTitle","#sampleTitleErrorText",Validation.errMsg_SampleTitleEmpty,2);
				Validation.validateStringLength($("textarea[name=SampleArticle]"),"#error_SampleArticle","#sampleArticleErrorText",Validation.errMsg_SampleArticleEmpty,2);
				Validation.validateStringLength($("textarea[name=Bio]"),"#error_Bio","#bioErrorText",Validation.errMsg_BioEmpty,100);
				break;
			case "VIP":
				var jumpAnchor = "";
				
				if (!Validation.validateStringLength($("input[name=First_Name]"),"#error_FirstName","#firstNameErrorText",Validation.errMsg_FirstNameInvalid,1))
				{
					jumpAnchor = "firstNameError";	
				}
				
				if (!Validation.validateStringLength($("input[name=Last_Name]"),"#error_LastName","#lastNameErrorText",Validation.errMsg_LastNameInvalid,1))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "lastNameError";	
					}
				}

				if (!Validation.validateApplicantEmail($("input[name=Email_Address]"),"#error_ApplicantEmail","#emailErrorText",0))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "emailError";	
					}
				}

				if (!Validation.validateVipPhoneNumber($("input[name=Phone_Number]"),"#error_PhoneNumber","#phoneNumberErrorText"))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "phoneNumberError";	
					}
				}

				if (!Validation.validateCity($("input[name=cityLiveIn]"),"#error_City","#CityErrorText",Validation.errMsg_CityInvalid,1))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "cityError";	
					}
				}

				if (!Validation.validateState($("select[name=stateLiveIn]"),"#error_State","#StateErrorText"))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "stateError";	
					}
				}

				if (!Validation.validateRadioButtons($("input[name=Gender]"),"#error_Gender","#GenderErrorText",Validation.errMsg_GenderNotSelected))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "genderError";	
					}
				}

				if (!Validation.validateEdition($("select[name=Edition]"),"#error_Edition","#editionErrorText"))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "editionError";	
					}
				}

				if (!Validation.validateCategory($("select[name=Category]"),"#error_Category","#categoryErrorText"))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "categoryError";	
					}
				}

				if (!Validation.validateStringLength($("textarea[name=bioInfo]"),"#error_Bio","#bioErrorText",Validation.errMsg_BioEmpty,100))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "bioError";	
					}
				}
			
				if (!Validation.validateReferral("#error_Referral","#referralErrorText"))
				{
					if (jumpAnchor == "")
					{
						jumpAnchor = "referralError";	
					}
				}

				break;
		}
		
		return Validation.SubmitForm(jumpAnchor);
	}

	Validation.SubmitForm = function(jumpAnchor){
		
		var isValid = true;
		
		if ($(".ui-state-error:visible").length > 0)
		{
			isValid = false;
		}
		
		if (!isValid && jumpAnchor != "")
		{
			window.location.hash=jumpAnchor;
		}

		return isValid;
	}
	

/********** HELPER FUNCTIONS ********************************/

	Validation.hideErrorMessage = function(elem){
		$(elem).fadeOut("slow");
	}
	
	Validation.showErrorMessage = function(errorElem){
		$(errorElem).fadeIn("slow");



	}



	Validation.isUniqueEmailAddr = function(addr){ 
		var endPoint = "/scripts/emailVerify.cfm";
		var unique = true;
		$.ajax({
			url:endPoint+"?email="+addr, 
			async:false,
			success:function(resp){
				if($.trim(resp)=="1"){
					unique = true;
				}
				else{
					unique = false;
				}
			}
		});
		return unique;
		
	}

	Validation.isValidURL = function(url){
		var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
		if(RegExp.test(url)){
			return true;
		}
		else{
			return false;
		}
	}


	Validation.isValidEmail = function(addr){
		
		var filter=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/
		
		if (filter.test(addr)){
			return true;
		}
		else{
			return false;
		}
	}
	
	Validation.callOmniture = function(erorr_type, page_name, form_name, element_eror){
		sendFormEvent(succuss, form_name, page_name,element_error);
	}
	
	Validation.isNumeric = function(value) 
	{ 
		value = value.replace("-","");
		value = value.replace("(","");
		value = value.replace(")","");
		value = value.replace(" ","");
		
		var result = new Number(value);
		return isNaN(result) ? false : true;
	} 