/*#################################################################
// (^_^) VERSION INFO /////////////////////////////////////////////
//	Version	: 5
// 	File	: site/scripts/bn.js
// 	Date	: October 23, 2006 8:54 AM
//	Name	: Errol 
//	Purpose	: Bubbanoosh validation
// (^_^) VERSION INFO /////////////////////////////////////////////
##################################################################*/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//			Form Validation
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//Global Array for use with Validating Manditory fields
var arrData = new Array();
var error_intro = "The following errors occured .. \n__________________________________\n\n";
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsValidFormData ------------------------------------------------------------------------------------------------
//  Purpose		- Fully Generic Form Validation
//	Args 		- form as Object, arrElements containg field names 
//	Consumes 	- fnIsNullOrEmptyFld
//  ** Note	**	- Only for validating Text, not Numeric or Date data
/* ________________________________________________________________________________________________________________________________ */
function fnIsValidFormData(frm, arrElements, tSection, tAction){//, bIsUplMsg, uplFld
	var fcsCtl; var bPost = false;
	var msg = "", tSect = "", fcsField = "";
	//resetBorder(frm);
	if ( typeof(arrElements) != "object" ) {											// If arrElements is not an Object validate entire form
		if (arrElements != "NoValidation" && arrElements == "") {						//Only validate if empty arg											
			for (var i=0 ; i<frm.length ; i++) {
				fcsField = "" ;
				msg += fnFormatFriendlyMessage( fnHandleDataType( frm.elements[i] ) );
				fcsField = fnHandleDataType( frm.elements[i] );
				if ( fcsField != "" ) {
					if ( typeof(fcsCtl) == "undefined" ) { fcsCtl = frm.elements[i] ; }
				}
			}
		}
	// onsubmit="return fnIsValidFormData(this, arrElements=['tLinkType','tLinkName','tLinkDesc','tLinkHref'], 'Link', 'Edit');"
	} else {																	// if arrElements is not null or empty
		for (var i=0 ; i<arrElements.length ; i++) {							// Then it is assumed to be an Array That was passed in containing only
				fcsField = "" ;
				msg += fnFormatFriendlyMessage( fnHandleDataType( eval("frm." + arrElements[i]) ) );		// form componant names. Loop through these fields only							
				fcsField = fnHandleDataType( eval("frm." + arrElements[i]) );	// Here we find out Which field to focus
				if ( fcsField != "" ) {
					if ( typeof(fcsCtl) == "undefined" ) { fcsCtl = eval("frm." + arrElements[i]) ; }
				}
		}
	}
	if (msg != "") {
		window.alert("The following errors occured .. \n__________________________________\n\n" + msg);
		if ( typeof(fcsCtl) != "undefined" ) {
			fcsCtl.focus() ; 
		}
		return false;
	} else {
		tSect = tSection ;tAct = tAction;
		if(fnIsNullOrEmptyValue(tSect)) {tSect = " Send " }
		if(fnIsNullOrEmptyValue(tAction)) {tAct = " Email "}
		bPost = window.confirm("Are you sure you want to " + tAct + " this " + tSect + " ?");
		/*if (bPost && bIsUplMsg) {  
			fnPleaseWait(uplFld);
		}*/
		return bPost
	}
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnHandleDataType ------------------------------------------------------------------------------------------------
//  Purpose		- Determine Datatype of Fld and handle appropriately
//	Args 		- Form field as Object
//	Consumes 	- Other Generic validation functions dependant on datatype
//  Returns 	- An Appropriate Error message to fnIsValidFormData for final output 
//  ** Note	**	- This function takes the 1st Char of the Fld name to determine datatype
/* ________________________________________________________________________________________________________________________________ */
function fnHandleDataType(obj) {
	var fld = "", fldName = "", retMsg = "";
	if (fnIsNullOrEmptyValue(obj)) { 
		return fld;
	} else {
		fld = obj.name;  										
		fldName = fld.substring(1, fld.length);								//Get field name minus the first char
		fld = fld.charAt(0);												//Get the 1st char to find datatype
	}
	switch (fld) {
		case "t"://text
			if ( fnIsNullOrEmptyFld( obj ) ) { 
			errorBorder(obj.name);
			return "\n * " + fldName + " - must be entered correctly" ;
			} else {return "";}
			break;
		case "e"://email
			errorBorder(obj.name);
			return fnIsInvalidEmail( obj, fldName ); 
			break;
		case "w"://url
			errorBorder(obj.name);
			return fnIsInvalidURL( obj, fldName ); 
			break;
		case "n"://general number
			if ( fnIsInvalidNum( obj ) ) { 
			errorBorder(obj.name);
				return "\n - " + fldName + " - must be a Valid Number";
			} else {return "";}
			break;
		case "i"://integer
			if ( fnIsInvalidInt( obj ) ) { 
			errorBorder(obj.name);
				return "\n * " + fldName + " - must be a Valid Number with no \'.\'";
			} else {return "";}
			break;
		case "f"://float
			if ( fnIsInvalidFloat( obj ) ) { 
			errorBorder(obj.name);
				return "\n * " + fldName + " - must be a Valid Decimal point number";
			} else {return "";}
			break;
		case "r", "c"://radio button checkbox, only single check box val
			if ( ! fnIsChecked( obj ) ) { 
				return "\n * " + fldName + " - must be Chosen";
			} else {return "";}
			break;
		default:
			if ( fnIsNullOrEmptyFld( obj ) ) { 
			errorBorder(obj.name);
				return "\n * " + fldName + " - must be entered correctly";
			} else {return "";}
			break;
	}
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnFormatFriendlyMessage ------------------------------------------------------------------------------------------------
//  Purpose		- Formats Error messages, replaces _ in element names with " "
//	Args 		- Text String Error Message
/* ________________________________________________________________________________________________________________________________ */
function fnFormatFriendlyMessage( tErrorString ){
	if ( tErrorString != "" ) {
		var ret = "";
		ret = tErrorString.split("_");
		ret = ret.join(" ");
		if ( ret.indexOf("", 0) ) {
			
		}
		return ret;
	} else {
		return "";
	}	
}


/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsNullOrEmptyFld ------------------------------------------------------------------------------------------------
//  Purpose		- Tests for invalid, empty, null values
//	Args 		- Form element as Object
/* ________________________________________________________________________________________________________________________________ */
function fnIsNullOrEmptyFld(obj) {
	var bRet = false;
	if (obj.disabled == false) {
		if (obj.value == "undefined" || obj.value == null || obj.value == "" ) {
			bRet = true;
		}
	}
	return bRet;
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsNullOrEmptyValue ------------------------------------------------------------------------------------------------
//  Purpose		- Tests for invalid, empty, null values
//	Args 		- text, number
/* ________________________________________________________________________________________________________________________________ */
function fnIsNullOrEmptyValue(x) {
	var bRet = false;
	if (x == "undefined" || x == null || x == "") {
		bRet = true;
	}
	return bRet;
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsInvalidInt ------------------------------------------------------------------------------------------------
//  Purpose		- Check if is a valid postive integer
//	Args 		- Form element as Object
/* ________________________________________________________________________________________________________________________________ */
function fnIsInvalidInt(obj) {
	var bRet = false ;
	if (obj.disabled == false) {
		if (obj.value=="" || isNaN(obj.value) || obj.value < 0 || obj.value == null || obj.value.indexOf(".",0) != -1) {
			bRet = true;
		}
	}
	return bRet;
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsInvalidFloat ------------------------------------------------------------------------------------------------
//  Purpose		- Check if is a valid postive integer
//	Args 		- Form element as Object
/* ________________________________________________________________________________________________________________________________ */
function fnIsInvalidFloat(obj) {
	var bRet = false ;
	if (obj.value=="" || isNaN(obj.value) || obj.value < 0 || obj.value == null || obj.value.indexOf(".",0) == -1) {
		bRet = true;
	}
	return bRet;
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsInvalidFloat ------------------------------------------------------------------------------------------------
//  Purpose		- Check if is a valid postive integer
//	Args 		- Form element as Object
/* ________________________________________________________________________________________________________________________________ */
function fnIsInvalidNum(obj) {
	var bRet = false ;
	if (obj.value=="" || isNaN(obj.value) || obj.value < 0 || obj.value == null) {
		bRet = true;
	}
	return bRet;
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsInvalidEmail ------------------------------------------------------------------------------------------------
//  Purpose		- Tests for invalid emails, empty, null values
//	Args 		- Form element as Object, Field name for msg
/* ________________________________________________________________________________________________________________________________ */
function fnIsInvalidEmail(obj, tField) {
	var bRet = "";
	if (obj.disabled == false) {
		if (!fnIsNullOrEmptyFld(obj)) {
			if (obj.value.indexOf('@',0)==-1||obj.value.indexOf('@',0)==0||obj.value.indexOf('.',0)==-1||obj.value.indexOf(',',0)!=-1||obj.value.indexOf(' ',0)!=-1) {
				bRet = "\n * " + tField + " - must be a VALID Email address containing an @ and 1 or more dots " ;
			} 
		} else {
			bRet = "\n * " + tField + " - cannot be blank " ;;
		}
	}
	return bRet;
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsInvalidURL ------------------------------------------------------------------------------------------------
//  Purpose		- Tests for invalid url, empty, null values
//	Args 		- Form element as Object
/* ________________________________________________________________________________________________________________________________ */
function fnIsInvalidURL(obj) {
	var bRet = "", bSelect = false;
	if (obj.disabled == false) {
		if (!fnIsNullOrEmptyFld( eval(obj) )) {
			if (obj.value.indexOf("http://", 0) == -1 && obj.value.indexOf("https://", 0) == -1) {
				bRet = "\n - A valid url must begin with either \'http:\/\/\' OR \'https:\/\/\'" ;
				bSelect = true;
			}
		} else {
				bRet = "\n - Web address cannot be blank";
		}
		if (bRet != "") {
			if (bSelect) { obj.select();} else { obj.focus(); }
		} return bRet;
	}
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsChecked ------------------------------------------------------------------------------------------------
//  Purpose		- Check if checkbox or radio is checked
//	Args 		- Form element as Object (Array of radios or checks)
/* ________________________________________________________________________________________________________________________________ */
function fnIsChecked(obj) {
	var bRet = false;
	if (obj.type == "hidden" || obj.disabled == true) {
		bRet = true;
	} else {
		bRet = obj.checked;
	}
	return bRet;
}

function fnIsInvalidPrice(obj) {
	var bRet = false, msg = "", fld = "", fldName = "";
	var tCents = "", nDecimalPlaces = 0;
	fld = obj.name;  										
	fldName = fld.substring(1, fld.length);								//Get field name minus the first char
	tCents = obj.value;
	if ( obj.value.indexOf(".", 0) != -1 ) {
		nDecimalPlaces = tCents.substring(obj.value.indexOf(".")+1, obj.value.length);
	}
	if(obj.value != ""){
		if ( isNaN(obj.value) ) {
			msg += " - " + fldName + " cannot contain non-numeric characters.\n"
		}
		if ( nDecimalPlaces.length > 2 ) {
			msg += " - Price\'s cannot have more than 2 Decimal places."
		}
		if ( msg != "" ) { window.alert("* Pricing Error __________________\n\n" + msg); obj.select(); return false; } else { return true; }
	} else {
		return true;
	}
}

/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnIsValidURL ------------------------------------------------------------------------------------------------
//  Purpose		- Used in onchange call, not used within main call
//	Args 		- Form element as Object
/* ________________________________________________________________________________________________________________________________ */
function fnIsValidURL(obj) {
	var error_intro = "The following errors occured .. \n__________________________________\n\n";
	var msg = "", bSelect = false;
	if (fnIsNullOrEmptyFld( eval(obj) )) {
		msg += error_intro + "\n - Web address cannot be blank";
	} else {
		if (obj.value.indexOf("http://", 0) == -1 && obj.value.indexOf("https://", 0) == -1) {
			msg += error_intro + "\n - A valid Web address must begin with either \'http:\/\/\' OR \'https:\/\/\'" ;
			obj.value = "";
			//bSelect = true;
		}
	}
	if (msg != "") {
		window.alert(msg);
		//if (bSelect) { obj.select();} else { obj.focus(); }
		obj.focus();
	}
}
//========================================================================================================//
function fnCheckBoxes(theForm){
	var i, toBoxesLen, ccBoxesLen, toAmt=0, ccAmt=0;
	var strMess="The following errors occured >_<\n\n";
	toBoxesLen = document.frmPeople.chTo.length; ccBoxesLen = document.frmPeople.chCC.length;
	for(i=0 ; i<toBoxesLen ; i++){
		//increment toAmt if more than 1 To is checked
		if(document.frmPeople.chTo[i].checked){toAmt++;}
		if(document.frmPeople.chTo[i].checked && document.frmPeople.chCC[i].checked){document.frmPeople.chTo[i].checked = false; strMess += "* To and CC ?? - " + document.frmPeople.chTo[i].value + "\n";}
	}
	if (toAmt < 1){strMess += "\n* There is no TO email address.\n\n"}
	if (toAmt > 1){strMess += "\n* To was checked " + toAmt +" times.\n"; }
	if (strMess != "The following errors occured >_<\n\n"){window.alert(strMess); return false;}
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnEnableDisable ------------------------------------------------------------------------------------------------
//  Purpose		- To Enable or Disable fields
//	Args 		- tFieldVal (Value of Field Yes|No)
//                frm (Form as object)
//                arrElements (Array of Fields to be affected)
/* ________________________________________________________________________________________________________________________________ */
function fnEnableDisable(tFieldVal ,frm ,arrElements){
	var fieldName = "", bDisable;
	if ( !fnIsNullOrEmptyValue(tFieldVal) || typeof(arrElements) != "object" ) {
		(tFieldVal == "Yes")? bDisable = true : bDisable = false; 
		for (var i=0 ; i<arrElements.length ; i++ ) {
			fieldName = arrElements[i];
			eval("document."+frm+"."+fieldName+".disabled="+bDisable+";")
			if (bDisable == false){ eval("document."+frm+"."+fieldName+".value=''"); }
			else { eval("document."+frm+"."+fieldName+".value='Disabled'"); } 	
		}	
	} else {
		return ;	
	}
}

function t(){
		if (tFieldVal == "Yes") {//disable
			for (var i=0 ; i<arrElements.length ; i++ ) {
				fieldName = arrElements[i];
				eval("document."+frm+"."+fieldName+".disabled=true;")	
			}	
		} else {
			for (var i=0 ; i<arrElements.length ; i++ ) {
				fieldName = arrElements[i];
				eval("document."+frm+"."+fieldName+".disabled=false;")
			}	
		}
}
function fnFirstElementFocus(){
	if (typeof(document.forms[0].elements[0]) != "undefined" && document.forms[0].elements[0].type != "hidden") {
		document.forms[0].elements[0].focus();
	}
}

/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnTextLength ------------------------------------------------------------------------------------------------
//  Purpose		- Only allow certain amount of text into field
//	Args 		- Form element as Object, max length allowed
/* ________________________________________________________________________________________________________________________________ */
function fnTextLength(obj, len){
	var data = "";
	if (obj && len > 1) {
		if (obj.value.length > len) {
			window.alert(error_intro + "You have exceeded the allowed data length..");
			data = obj.value;
			data = data.substring(0, len);
			obj.value = data;
			return false;
		} return;
	} else {
		return;
	}
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- fnPleaseWait ------------------------------------------------------------------------------------------------
//  Purpose		- Show message please wait while data is posting
//	Args 		- NA
/* ________________________________________________________________________________________________________________________________ */
function fnPleaseWait(uplFld) {
	if ( !fnIsNullOrEmptyFld(uplFld) ) {
		if (navigator.appName  == "Microsoft Internet Explorer"){
			document.all.loading.style.pixelTop = (document.body.scrollTop + 50);
			document.all.loading.style.visibility="visible";
		} else {
			if (navigator.appName  == "Netscape") {
				document.loading.pixelTop = (document.body.scrollTop + 50);
				document.loading.visibility="visible";
			}
		}
	}
}
//========================================================================================================//
function errorBorder(theId){
	if(navigator.appName.indexOf("Netscape")== -1){
		if(document.getElementById){var curStyle=document.getElementById(theId); curStyle.style.border="1px solid #FF0000";	return true;}
	}
}
//========================================================================================================//
function resetBorder(frm){
	if(document.getElementById){
		window.alert(document.frm.elements.length);
		for(var i=0 ; i<document.frm.elements.length ; i++){curStyle=document.getElementById(document.frm.elements[i].name); curStyle.style.border="1px solid #666699";}	
		//return true;
	}
}
/* _________________________________________________________________________________________________________________________________ */
//	Name 		- Scrolling status ------------------------------------------------------------------------------------------------
//  Purpose		- To show animated message in status bar
//	Args 		- 
/* ________________________________________________________________________________________________________________________________ */
msg = "BN - C . M . S (Version 2.0) - www.BubbaNoosh.com.au";

timeID 	= 10;
stcnt 	= 16;
wmsg 	= new Array(33);
wmsg[0]=msg;
blnk = "                                                               ";
for (i=1; i<32; i++) {
	b = blnk.substring(0,i);
	wmsg[i]="";
	for (j=0; j<msg.length; j++) {
		wmsg[i]=wmsg[i]+msg.charAt(j)+b;
	}
}

function wiper()
{
        if (stcnt > -1) str = wmsg[stcnt]; else str = wmsg[0];
        if (stcnt-- < -40) stcnt=31;
        status = str;
        clearTimeout(timeID);
        timeID = setTimeout("wiper()",100);
}

wiper()

