/* ----    ----    ----
		Common Systems JavaScript
   ----    ----    ---- 
Contents:
-	Alert Box Functions (alert & confirm replacements)
-	Alert Box Engine
-	Common Functions (used in alert box and form validations)
-	Validation Functions (email checks, etc... for our form validation system)
   
   
/* ----    ----    ----		Alert Box Functions		----    ----    ---- */

//		Variables
var sAlertBoxID = "TBAlertBox"; // Identity of alert box wrapper div
var nAlertBoxOffset = 30; // The gap between window and top of the box
var bAlertLimitScroll = false; // Limit scrolling when page is long? (removes page content
var sPageWrapperID = "mainWrapper"; // ID of an element that contains the entire page for heights

//			Replacement Functions
// Fix for IE 7 needing clearfix, but Firefox and IE8 breaking with it.
var IEstyles = "";
if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) == "7") { IEstyles=" clearfix"} //clearfix

function fErrorValidation(vMsg) {
	var vS = "";
	if (vMsg.split("-").length-1 > 1) { vS = "s"; }
	vMsg = vMsg.replace(/\n/g,"</li>");
	vMsg = '<h3 class="icon_excl">Incomplete Form</h3><strong>You are required to fill in the following field'+vS+':</strong><ul>'+vMsg.replace(/-/g,'<li>')+'</ul>';
	fErrorAlert(vMsg);
}

function fErrorValidationEntries(vMsg) {
	var vS = "";
	if (vMsg.split("-").length-1 > 1) { vS = "s"; }
	vMsg = vMsg.replace(/\n/g,"</li>");
	vMsg = '<h3 class="icon_excl">Incomplete Form</h3><strong>You are required to fill in the following field'+vS+':</strong><ul>'+vMsg.replace(/-/g,'<li>')+'</ul><br /><p>If you wish to save an incomplete entry use the "Save For Later" button at the bottom of the entry form.</p>';
	fErrorAlert(vMsg);
}

function fErrorAlert(vMsg,sCloseFunction) {
	if(!sCloseFunction) { var sCloseFunction = ""; }
	vMsg = HTMLizeJS(vMsg);
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="javascript:tb_alert_close();'+sCloseFunction+'" class="button_style button_cross'+IEstyles+'">Close</a></div>';
	tb_alertbox(vMsg);
}

function fNavAway(sURL) {
	if(vSystem == "entries") {
		vMsg = "<h3>Incomplete Entry</h3><p>You have chosen to navigate away from this form. Do you want to save a copy of this incomplete entry that will be available next time you visit this page?</p>";
		vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="javascript:fPressSave()" class="button_style icon_disk'+IEstyles+'" style="margin-right:15px;">Save For Later</a><a href="javascript:tb_alert_close();" style="margin-right:15px;" class="button_style icon_cross'+IEstyles+'">Complete Form</a><a href="javascript:docLoad(\''+sURL+'\');" class="button_style arrow_right'+IEstyles+'"  style="margin-right:15px;">Continue</a></div>';
	} else {
		vMsg = "<h3>Incomplete Form</h3><p>Would you like to navigate away from this page?</p><br>";
		vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="javascript:tb_alert_close()" style="margin-right:50px;" class="button_style icon_cross'+IEstyles+'">Cancel</a><a href="javascript:tb_alert_close();docLoad(\''+sURL+'\');" class="button_style arrow_right'+IEstyles+'">Continue</a></div>';
	}
	tb_alertbox(vMsg);
	return false;
}

// =--  Confirm Alert Replacement  --=
// Displays a Confirm and Cancel button.
// This doesn't work like a JS confirm as the code that calls it doesn't wait for a return.
// Therefore code to call on cancel can be put into the vCode variable, which can also be a function.
function fErrorConfirm(vMsg,vCode) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = "<p>"+vMsg+"</p>";
	//vMsg = "<h3>Please Note...</h3>"+vMsg;
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="javascript:tb_alert_close()" style="margin-right:50px;" class="button_style button_cross'+IEstyles+'">Cancel</a><a href="javascript:tb_alert_close();'+vCode+'" class="button_style button_tick'+IEstyles+'">Confirm</a></div>';
	tb_alertbox(vMsg);
}

function fErrorConfirmLink(vMsg,vLink) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = "<p>"+vMsg+"</p>";
	//vMsg = "<h3>Please Note...</h3>"+vMsg;
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();return false" class="button_style icon_cross'+IEstyles+'" style="margin-right:40px;">Cancel</a><a href="'+vLink+'" class="button_style icon_tick'+IEstyles+'">Confirm</a></div>';
	return tb_alertbox(vMsg);
}

/*=- As Above but reversed so confirm does nothing -=*/
function fErrorConfirmReversed(vMsg,vCode) {
		vMsg = HTMLizeJS(vMsg)
		vMsg = "<p>"+vMsg+"</p>";
		vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();'+vCode+';return false;" style="margin-right:50px;" class="button_style icon_cross'+IEstyles+'">Cancel</a><a href="#" onclick="tb_alert_close();return false" class="button_style icon_tick'+IEstyles+'">Confirm</a></div>';
	return tb_alertbox(vMsg);
}

/*=- As Above but two actions -=*/
function fErrorConfirmTwoAction(vMsg,vCodeConfirm,vCodeCancel) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = "<p>"+vMsg+"</p>";
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();'+vCodeCancel+';return false;" style="margin-right:50px;" class="button_style icon_cross'+IEstyles+'">Cancel</a><a href="#" onclick="'+vCodeConfirm+';tb_alert_close();return false" class="button_style icon_tick'+IEstyles+'">Confirm</a></div>';
	return tb_alertbox(vMsg);
}

var AllowSubmit = 0; // Switch used in IAF specific validation
function fPressSubmit() { AllowSubmit = 1; document.getElementById('submit').click(); }
function fPressSubmitForm(vFormID) { AllowSubmit = 1; document.getElementById(vFormID).submit(); }
function fPressYes() { AllowSubmit = 1; document.getElementById('button_yes').click(); }
function fAllowSubmit() { AllowSubmit = 1; }


/* ----    ----    ----		Alert Box ENGINE		----    ----    ---- */

// IE6 - WARNING: THIS DETECTION IS DEPRECIATED IN JQUERY 1.3
var isIE6 = jQuery.browser.msie && jQuery.browser.version.substr(0,1)=="6" ? true : false;

// For IE 6 we use a fudge to mimic position:fixed
function tb_ie6_pos() {
	if ($("#"+sAlertBoxID)){
	$("#"+sAlertBoxID).css("top", ($(window).scrollTop()+nAlertBoxOffset) + "px");
	}
}
if(isIE6) {
	$(window).scroll(function() {
		if ($("#"+sAlertBoxID).height() < $(window).height()-nAlertBoxOffset) {
	    	tb_ie6_pos();
		}
	});
}


// window resize listener
$(window).resize(function(){ tb_alert_check_sizing(); });


// Check size of lightbox to switch fixed or absolute positioning
function tb_alert_check_sizing() {
	//$("#"+sAlertBoxID).height() > $(window).height()-nAlertBoxOffset ? $("#"+sAlertBoxID).css("position","absolute").css("top", ($(window).scrollTop()+nAlertBoxOffset)+"px") : $("#"+sAlertBoxID).css("position","fixed").css("top",nAlertBoxOffset+"px");
	if ($("#"+sAlertBoxID).height() > $(window).height()-nAlertBoxOffset) {
		if (bAlertLimitScroll) {
			// Alert is taller than window
			$("#"+sAlertBoxID).css("position","absolute").css("top", nAlertBoxOffset+"px") ;
			$(window).scrollTop(0);
		}
		if(!isIE6) {
			$("#TBBoxBlackOut").height($(window).height() > $("#"+sAlertBoxID).height() ? $(window).height()+(nAlertBoxOffset*2) : $("#"+sAlertBoxID).height()+(nAlertBoxOffset*2) );
		}
	} else {
		// Alert is shorter than window
		$("#"+sPageWrapperID).height() > $(window).height() ? $("#TBBoxBlackOut").height($("#"+sPageWrapperID).height()) : $("#TBBoxBlackOut").height($(window).height());
		if(!isIE6) {
			// centre position
			var topPx = ($(window).height()-$("#"+sAlertBoxID).height())/2;
			topPx >  $(window).height()/4 ? topPx = topPx-$(window).height()/6 : topPx=topPx;
			$("#"+sAlertBoxID).css("top",topPx+"px");
		}
	}
}

function tb_alertbox(sContent) {
	if ( $("#"+sAlertBoxID).length > 0 ) { tb_alert_close(); }
	$("body").append('<div id="'+sAlertBoxID+'" class="TBPosFixed"><div id="TBAlertBoxInner">'+sContent+'</div><!--[if lte IE 6.5]><iframe></iframe><![endif]--></div><div id="TBBoxBlackOut"></div>'); // Add Container
	$("#"+sPageWrapperID).height() > $(window).height() ? $("#TBBoxBlackOut").height($("#"+sPageWrapperID).height()) : $("#TBBoxBlackOut").height($(window).height());
	if(isIE6) { tb_ie6_pos(); } else { tb_alert_check_sizing(); }
	return false;
}

function tb_alert_close() {
	$("#"+sAlertBoxID).remove(); // Remove box and everything in it
	$("#TBBoxBlackOut").remove(); // Remove black out
}



/* ----    ----    ----   ----		Common Functions		----    ----    ----   ---- */

// Focuses on and Element by it's ID
function fFocusById(vId) {
	$("#"+vId).focus();
}

// Add commas to numbers for formatting
function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '.00';
	if (x2.length == 2) x2 = x2+'0';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

//	Basic open window
function openWindow(url,width,height) {
	var PopUpWin=window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizeable=0,scrollbars=yes,menubar=no,status=no');
	PopUpWin.focus()
}

//	docLoad - a simple JavaScript page load. Useful in a confirm dialog.
function docLoad(vURL) {
	document.location.href = vURL;
}

// Strip HTML if resorting to JS alerts.
function fRemoveHTML(str) {
	str = str.replace(/<strong>/g,"")
	str = str.replace(/<\/strong>/g,"")
	str = str.replace(/<em>/g,"")
	str = str.replace(/<\/em>/g,"")
	return str;
}

//		HTML LINE BREAKS
// =--  replaces Javascript new lines with HTML breaks.
function HTMLizeJS(vMsg) {
	vMsg = vMsg.replace(/\n/g,"<br />")
	return vMsg;
}

//		HIGHLIGHT MISSING FIELDS
//  Give it a list of field ids. The label needs an id of for_field_name
function showMissingFields(vFieldList) {
	var arFields = vFieldList.split(",");
	for (var i = 0; i < arFields.length; ++i) {
		var item = arFields[i];
		if (item != "") {
			if($(item)) $('#'+item).addClass('missing')
			if($('#for_'+item)) $('#for_'+item).addClass('missing')
		}
	}
}

// 		Reset Form Styles
//  Removes missing fields class from inside forms
function resetFormStyles(vFormID) {
	arFields = $("#"+vFormID+" > *").removeClass('missing')
}

//		Load URL into the parent window of a popup.
function openerWindow(url) {
	var Win=window.opener.location.href = url;
	setTimeout(window.close, 1000);
}

//		Format a decimal
function formatDecimal(number) {
	var decimal = Math.round(100*number) + "";
	decimal = decimal == "0" ? "100" : decimal;
	return parseInt(number) + "." + decimal.substr(decimal.length-2,2);
}
function roundVal(val){
	var dec = 2;
	var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


/* ----    ----    ----   ----		Validation Functions		----    ----    ----   ---- */
function fValidateLogin() {
	if ($.trim($("#email").val()) == "" || $.trim($("#password").val()) == "") {
		fErrorAlert('<h3 class="icon_excl">Incomplete Form</h3><p><strong>Enter your username and password to login.</strong></p>')
		return false;
	} else {
		return true;
	}
}

function fValidateForgotten() {
	if ($.trim($("#email").val()) == "") {
		fErrorAlert('<h3 class="icon_excl">Incomplete Form</h3><p><strong>Enter your email address to receive your login details.</strong></p>')
		return false;
	} else {
		return true;
	}
}

function fValidateChangePassword() {
	sHdr = '<h3 class="icon_excl">Incomplete Form</h3>';
	sPOld = $.trim($("#password_old").val());
	sPNew = $.trim($("#password_new").val());
	sPCon = $.trim($("#password_confirm").val());
	if (sPOld == "" && sPNew != "" && sPCon != "") {
		fErrorAlert(sHdr+'<p><strong>You must provide your current password.</strong></p>')
		return false;
	} else if (sPOld != "" && sPNew == "" && sPCon != "") {
		fErrorAlert(sHdr+'<p><strong>You must provide a new password.</strong></p>')
		return false;
	} else if (sPOld != "" && sPNew != "" && sPCon == "") {
		fErrorAlert(sHdr+'<p><strong>Please confirm your new password.</strong></p>')
		return false;
	} else if (sPOld == "" || sPNew == "" || sPCon == "") {
		fErrorAlert(sHdr+'<p><strong>Please complete all fields to change your password.</strong></p>')
		return false;
	} else if (sPNew != sPCon) {
		fErrorAlert(sHdr+'<p><strong>Your new password does not match the confirmation. Please try again.</strong></p>')
		return false;
	} else {
		return true;
	}
}
			
function fieldExists ( entryForm, fieldName) {
	if (!entryForm[fieldName]) return " DEVERROR: field " + fieldName.toUpperCase() + " doesn't exists in form " + entryForm.name.toUpperCase() + "\n";
		else return "";
}

function isDate( mm, dd, yyyy) {
	var d = new Date(mm + "/" + dd + "/" + yyyy);
	return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
}

function validateSelect ( themessage, entryForm, fieldName, ifError) {
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (themessage == orgmessage) {
		if (entryForm[fieldName].selectedIndex == "0") {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}

function validateText ( themessage, entryForm, fieldName, ifError) {
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (themessage == orgmessage) {
		if($.trim(entryForm[fieldName].value) == "") {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}

function validateEmail (themessage, entryForm, fieldName, ifError, isMandatory) {
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (themessage == orgmessage) {
		var filter = new RegExp(/^([a-zA-Z0-9_\.\-'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/);
		var fieldValue = $.trim(entryForm[fieldName].value);
		if ((isMandatory && fieldValue == "") || (fieldValue != "" && !filter.test(fieldValue))) {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}

function validateEmailString (themessage, fieldName, sInput, ifError, isMandatory) {
	var orgmessage = themessage;
	var filter = new RegExp(/^([a-zA-Z0-9_\.\-'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/);
	if ((isMandatory && $.trim(sInput) == "") || ($.trim(sInput) != "" && !filter.test($.trim(sInput)))) {
		themessage += " - " + ifError + "\n";
		window["vFieldList"] = window["vFieldList"] + fieldName+",";
	}
	return themessage;
}

// Validates URL without http:// in it.
function validateURLOptionalHttp (themessage, entryForm, fieldName, ifError, isMandatory)
{
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (themessage == orgmessage) {
		//var filter = new RegExp(/^(([a-zA-Z0-9\-\_])+\.)+(([a-zA-Z0-9]{2,4}).)+(\/+([a-zA-Z0-9\-\_]))*/);
		var filter = new RegExp(/^(http:\/\/|https:\/\/|)(([a-zA-Z0-9\-\_])+\.)+(([a-zA-Z0-9]{2,4}).)+(\/+([a-zA-Z0-9\-\_]))*/);
		if ((isMandatory && entryForm[fieldName].value == "") || (entryForm[fieldName].value != "" && !filter.test(entryForm[fieldName].value))) {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}

// Validates URL with http:// in it.
function validateURL (themessage, entryForm, fieldName, ifError, isMandatory)
{
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (themessage == orgmessage) {
		var filter = new RegExp(/^(http|https)+:\/\/(([a-zA-Z0-9\-\_])+\.)+(([a-zA-Z0-9]{2,4}).)+(\/+([a-zA-Z0-9\-\_]))*/);
		if ((isMandatory && entryForm[fieldName].value == "") || (entryForm[fieldName].value != "" && !filter.test(entryForm[fieldName].value))) {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}

function validateTelephone (themessage, entryForm, fieldName, ifError, isMandatory)
{
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (themessage == orgmessage) {
		var filter = /^((\+)?[0-9]{6,})+$/;
		if ((isMandatory && entryForm[fieldName].value == "") || (entryForm[fieldName].value != "" && !filter.test(entryForm[fieldName].value))) {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}

function validateBox (themessage, entryForm, fieldName, ifError)
{
	var orgmessage = themessage;
	var isBox = true;
	var isChecked = false;
	themessage += fieldExists( entryForm, fieldName);
	if (!entryForm[fieldName].length) {
		if (entryForm[fieldName].type != "radio" && entryForm[fieldName].type != "checkbox") isBox = false;
	} else if (entryForm[fieldName][0].type != "radio" && entryForm[fieldName][0].type != "checkbox") isBox = false;
	if (!isBox) themessage += " DEVERROR: field " + fieldName.toUpperCase() + " isn't a RADIO or CHECKBOX fieldtype\n";
	if (themessage == orgmessage) {
		if (!entryForm[fieldName].length) {
			if(entryForm[fieldName].checked) isChecked = true;
		} else {
			for (i=0; i<entryForm[fieldName].length; i++) {
				if(entryForm[fieldName][i].checked) { 
					isChecked = true;
					break;
				}
			}
		}
		if (!isChecked) {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}

function isNumeric(strString){
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++){
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1){
			blnResult = false;
		}
	}
	return blnResult;
}

function validateNumeric (themessage, entryForm, fieldName, ifError)
{
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (themessage == orgmessage) {
		if (!isNumeric(entryForm[fieldName].value)) {
			themessage += " - " + ifError + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;	
}

function validateNumericRange (themessage, entryForm, fieldName, minValue, maxValue, ifError)
{
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (!(isNumeric(entryForm[fieldName].value) && isNumeric(minValue) && isNumeric(maxValue))) themessage += " - " + ifError + "\n";
	if (minValue > maxValue) themessage += " DEVERROR: field " + fieldName.toUpperCase() + ", MINVALUE and MAXVALUE range mistake\n";
	if (themessage == orgmessage) {
		if (parseFloat(entryForm[fieldName].value) < parseFloat(minValue) || parseFloat(entryForm[fieldName].value) > parseFloat(maxValue)) {
			themessage += " - " + ifError + " must be between " + minValue + " and " + maxValue + "\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;	
}

function validateLengthInWords (themessage, entryForm, fieldName, maxWords, ifError, nLeeway) {			
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName);
	if (typeof(nLeeway) == 'undefined') { nLeeway = 25; }
	maxWordsAllowed = maxWords+nLeeway
	stringToTest = $.trim(entryForm[fieldName].value);
	currentWords = stringToTest.split(/\s+/g).length;
	if (themessage == orgmessage) {
		if (currentWords > maxWordsAllowed) {
			themessage += " - " + ifError + "  has a maximum of "+maxWords+" words. It is currently "+ currentWords +" words.\n";
			window["vFieldList"] = window["vFieldList"] + fieldName+",";
		}
	}
	return themessage;
}	

function validateConfirmation (themessage, entryForm, fieldName1, fieldName2, ifError)
{
	var orgmessage = themessage;
	themessage += fieldExists( entryForm, fieldName1);
	themessage += fieldExists( entryForm, fieldName2);
	if (themessage == orgmessage) {
		if (entryForm[fieldName1] != entryForm[fieldName2]) {
			if ($.trim(entryForm[fieldName1].value) != $.trim(entryForm[fieldName2].value)) {
				themessage += " - " + ifError + "\n";
				window["vFieldList"] = window["vFieldList"] + fieldName1+",";
				window["vFieldList"] = window["vFieldList"] + fieldName2+",";
			}
		} else themessage += " DEVERROR: you are trying to confirm field " + fieldName1.toUpperCase() + " with " + fieldName2.toUpperCase() + "\n";
	}
	return themessage;
}

function validateTextarea(themessage, entryForm, fieldName, maxWords, ifError, nLeeway){
	var string = $.trim(entryForm[fieldName].value);
	if (!nLeeway || typeof nLeeway == 'undefined') { nLeeway = 25; }
	if(string==""){
		themessage = themessage + " - " + ifError + "\n";     
		window["vFieldList"] = window["vFieldList"] + fieldName+",";;
	} else if (maxWords != 0) {
		themessage = validateLengthInWords(themessage, entryForm, fieldName, maxWords, ifError, nLeeway)
	}
	return themessage;
}

function validateNumericMax(themessage, entryForm, fieldName, maxLength, fieldLabel){	
	if(isNaN(entryForm[fieldName].value) == true || $.trim(entryForm[fieldName].value)=="" || (isNaN(entryForm[fieldName].value) == false && entryForm[fieldName].value > maxLength)){
		themessage = themessage + " - "+fieldLabel+" must not be more than " + maxLength + "\n"; 
		window["vFieldList"] = window["vFieldList"] + fieldName+",";;        
	}
	return themessage;
}


/* ----    ----    ----   ----		Login & Registration Functions		----    ----    ----   ---- */
function fRegCheckRole(){
	if(document.registrationForm.job_role_id.value=="100"){
		document.getElementById("otherRole").style.display="block";
	}else{
		document.getElementById("otherRole").style.display="none";
	}
}


//=---		WORD COUNTER	---=//
function wordCounter(oField) {
	wordcounter=1;
	val = $("#"+oField.id).val();
	val = jQuery.trim(val);

	if (val.length == 0) {
		wordcounter=0;
	} else {
		wordcounter = val.split(/\s+/g).length;
	}
	$("#wordCount"+oField.id).html(""+wordcounter);
	$("#"+oField.id).focus();
}

function wordCountInit() {
	$(".wordcount").each(function (i) {
		wordcounter=1;
		val = $("#"+this.id).val();
		val = jQuery.trim(val);
		
		if (val.length == 0) {
			wordcounter=0;
		} else {
			for (x=0;x<val.length;x++) {
			      if ((val.charAt(x) == " " || val.charAt(x) == "\n") && val.charAt(x-1) != " ") {
				  	wordcounter++; // Counts the spaces while ignoring double spaces, usually one in between each word.
				}
			}
		}
		$("#wordCount"+this.id).html(""+wordcounter);
	})
}