// JavaScript Document

// BSP Common JS File



function submitForm() {
	//make sure hidden and iframe values are in sync for all rtes before submitting form
	//updateRTEs();
	
	form = document.forms['main'];
	
	//change the following line to true to submit form
	//alert("rte1 = " + htmlDecode(form.txtPDescription.value));
	return false;
}



function addMeToFavourites() {
	window.external.AddFavorite("http://www.bspindustries.com.au/","BSP Industries");
}


function validateMandatoryFields(oForm, arrMandatory) {
	var oField;
	var vFieldLabel;
	var vDividerEnum;
	var vRadioCheckIndex;
	var vRadioCheckEnum;
	// Array to hold mandatory fields.
//	var arrMandatory = new Array();
	var arrMandatoryItem = new Array();
	var blnMandatoryItemRequired = true;
	// LOOP through Mandatory Fields.
	for (a = 0; a < arrMandatory.length; a ++) {
		
		// Put the currently looped mandatory field into arrMandatoryItem.
		arrMandatoryItem = arrMandatory[a].split(",");

		// Store the Mandatory field into object variable oField.
		oField = eval("oForm." + arrMandatoryItem[0]);
		
		// If show/hide dividers are included. Check if the field is required (displayed).
		if (arrMandatoryItem.length > 1) {
			// Initialise variable as Mandatory field is required.
			blnMandatoryItemRequired = true;
			// Loop through dependant show/hide dividers.
			for (vDividerEnum = 1; vDividerEnum < arrMandatoryItem.length; vDividerEnum ++) {
				// If any dependant show/hide dividers are not displayed.
				if (eval(arrMandatoryItem[vDividerEnum]).style.display.indexOf("none") != -1 ) {
					// Field is NOT required.
					blnMandatoryItemRequired = false;
				}
			}
		}
		// If show/hide dividers are NOT included.
		else {
			// If field does NOT exist.
			if (oField == null) {
				// Field is NOT required.
				blnMandatoryItemRequired = false;
			}
			// If field exists.
			else {
				// Field IS required.
				blnMandatoryItemRequired = true;
			}
		}

		// If Field is Required.
		if (blnMandatoryItemRequired == true) {

			// If field is Multiple radio or Multiple checkbox.
			if (oField.length > 0 && (oField[0].type == "radio" || oField[0].type == "checkbox")) {
				// If the currently looped Mandatory Field does NOT have a label attribute OR the label attribute is an empty string.
				if ((oField[0].label == null) || (oField[0].label.length < 1)) {
					// Store the currently looped array string (mandatory field name) in vFieldLabel.
					vFieldLabel = arrMandatoryItem[0];
				}
				// If the currently looped Mandatory Field HAS a valid label attribute.
				else {
					// Store the label attribute value of the currently looped mandatory field in vFieldLabel.
					vFieldLabel = oField[0].label;
				}
				// Initialise variable to hold the radio buttons selected/checked index.
				vRadioCheckIndex = -1;
				// Loop through the radio buttons to see if any are checked.
				for (vRadioCheckEnum = 0; vRadioCheckEnum < oField.length; vRadioCheckEnum ++) {
					if (oField[vRadioCheckEnum].checked == true) {
						vRadioCheckIndex = vRadioCheckEnum;
					}
				}
				// If the radio button DOES NOT have any checked items.
				if (vRadioCheckIndex == -1) {
					alert("Please select at least one option for '" + vFieldLabel + "' as it is mandatory." + "\n");
					oField[0].focus();
					return false;
				}
			}

			// If field is NOT Multiple radio or Multiple checkbox.
			else {
				// If the currently looped Mandatory Field does NOT have a label attribute OR the label attribute is an empty string.
				if ( (oField.label == null) || (oField.label.length < 1) ) {
					// Store the currently looped array string (mandatory field name) in vFieldLabel.
					vFieldLabel = arrMandatoryItem[0];
				}
				// If the currently looped Mandatory Field HAS a valid label attribute.
				else {
					// Store the label attribute value of the currently looped mandatory field in vFieldLabel.
					vFieldLabel = oField.label;
				}
				// If the form field is empty.
				if (oField.value.replace(/\s+/gi, "").length < 1) {
					alert("Please complete '" + vFieldLabel + "' as it is mandatory." + "\n");
					oField.focus();
					return false;
				}
			}

		// END If Field is Required.
		}
	// END LOOP through Mandatory Fields.
	}
	// Return form validation as True.
	return true;
}
// ######################################################


function verifyIt(){
	var form = document.forms[0];
  	var k=0;
  	for (i=0; i < form.elements.length; i++)
  	{
		//Validate text fields
		if (form.elements[i].id != "non_mandatory" && form.elements[i].type != "hidden" && form.elements[i].value.replace(/\s/gi, "").length < 1){
 			 		 alert("Please complete '"+form.elements[i].title+"' as it is mandatory.");
								form.elements[i].select();
								return (false);
			}	
			//Validate drop down
  		if (form.elements[i].id != "non_mandatory" && form.elements[i].type != "hidden" && form.elements[i].value=="na"){
 			 		
					//alert(form.elements[i].name);
					alert("Please complete '"+form.elements[i].title+"' as it is mandatory.");
						 if (form.elements[i].id == "select")
						 			//return true;
									form.elements[i].focus();
		  			else
  								form.elements[i].select();
								  return (false);
					}
					
				//Validate radio buttons
				if (form.elements[i].id != "non_mandatory" && form.elements[i].type=="radio"){

							      var cnt = -1;
										var rd = form.elements[i].name;
										var rdLength = form.elements(rd).length;

           					for (var k=0; k < rdLength; k++) {
													if(form.elements(rd)[k].checked){
																cnt = k; 
																k = rdLength;
										}
            }
            if (cnt <= -1){
	     			 		 alert("Please complete '"+form.elements[i].title+"' as it is mandatory.");
		   			 			form.elements[i].focus();
  	   							return(false);
									}
							}					
					
					
	}
return (true);
}



//Start CheckEmail
function CheckEmail(entered) {

emailStr = entered.value;

/*allows empty field*/
if (emailStr.length==0)
	return true;


/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]\\'";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
//For example, in john.doe@somewhere.com, john and doe are words.
//Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();


return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();

return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();

return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("Invalid email address. Please enter a valid email address.");
entered.select();
entered.focus();
return false;
}

// If we've gotten this far, everything's valid!
return true;
}



