//EDEX CPD Standard JavaScript Functions - Created by Alex Fahey 0419 74 9898

function popUpForm(URL)
{
	newWindow = window.open(URL, "PopForm","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=572,height=500");
	newWindow.focus();
}

function popUpPage(URL)
{
	newWindow = window.open(URL, "PopPage","toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=700,height=500");
	newWindow.focus();
}

function popUpCustom(URL,Options)
{
	newWindow = window.open(URL, "PopCustom", Options);
	newWindow.focus();
}

function Popup(target,x,y){
	var newWindow = null;var i = 0;
	if (navigator.appName == "Microsoft Internet Explorer")	{
		if ( (navigator.appVersion.indexOf('Windows 3.1') < 0) && (navigator.appVersion.indexOf('Macintosh') < 0)  ) {
			newWindow = window.open("", "PopWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=" + x + ",height=" + y);
			if (newWindow){
				newWindow.close();
				while((! newWindow.closed)&&(i<100))i++;
			}
		}
	}		
	newWindow = window.open(target, "PopWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=" + x + ",height=" + y);
	if (navigator.appName != "Microsoft Internet Explorer")	{
		newWindow.focus()
	}
}

//function to clear a field - used by search field when the user clicks in it
function erase(elem) {
    if (elem) {
        elem.value = "";
    }
}


//Function to limit the number of characters in a text area
function getCount(ta)
{
	var maxnum = 20000;
	var c_cnt, tmpStr;

	c_cnt = ta.value.length;
	tmpStr = ta.value + " ";

	var w_space = /^[^A-Za-z0-9]+/gi;
	var lTStr = tmpStr.replace(w_space, "");
	var non_alpha = /[^A-Za-z0-9]+/gi;
	var cStr = lTStr.replace(non_alpha, " ");
	var splitStr = cStr.split(" ");
	var w_cnt = splitStr.length-1;
	
	if (tmpStr.length <2) 
	{
		word_count = 0;
	}

	if (maxnum < c_cnt)
	{
		ta.value = ta.value.substring(0,maxnum);
		alert('Sorry, but this form can only take up to 20,000 characters (roughly 3000 words). You have now reached this limit.');
	}
}

//Form validation function
function validateMandatoryFields(formobj,fieldRequired,fieldDescription){
var alertMsg = "The following fields must be entered before submitting this form:\n";
var l_Msg = alertMsg.length;
for (var i = 0; i < fieldRequired.length; i++){
     var obj = formobj.elements[fieldRequired[i]];
     if (obj){
     	//alert(obj.type);
         switch(obj.type)
         {
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""|| obj.options[obj.selectedIndex].value == "")
{alertMsg += " - " + fieldDescription[i] + "\n";}
break;
case "select-multiple":
if (obj.selectedIndex == -1)
{alertMsg += " - " + fieldDescription[i] + "\n";}
break;
case "text":
case "password":
case "textarea":
if (obj.value == "" || obj.value == null)
{alertMsg += " - " + fieldDescription[i] + "\n";}
break;
default:
if (obj.value == "" || obj.value == null)
{if(obj.length >0)
{
var pass=false;
for (var x = 0; x < obj.length; x++)
{
if (obj[x].checked)
{pass = true;
break;}}
if (pass == false)
{alertMsg += " - " + fieldDescription[i] + "\n";}
}}}}}
if (alertMsg.length == l_Msg){
  //Process special fields
  var strContinue = "true"
  //Process Email address
  if(formobj.Email){
	if(formobj.Email.value!=""){
		if (confirm ("Please confirm that this is your email address is:" + "\n" + "\n" + formobj.Email.value)){
			strContinue = "true";
		}else{
			strContinue = "false";
		}
	}else{
		strContinue = "true";
	}
  }

  //Process Password address
  if(formobj.Password && formobj.Password2){
  	if (formobj.Password.value == formobj.Password2.value){
		strContinue = "true";
	}else{
		alert('The passwords you entered do not match. Please check and try again.');
		strContinue = "false";
	}
  }

  if(strContinue == 'true'){
	return true;
  }else{
    return false;
  }
  } else {
    //Display the error message  
    alert(alertMsg);
    return false;
  }
}
