//determining the browser type
var brwsr_ns = (document.layers ? true : false);
var brwsr_ie = (document.all ? true : false);
var brwsr_navig = navigator.userAgent;
var brwsr_moz = (brwsr_navig.indexOf("Gecko")>=0 ? true : false);

var DEFAULT_DIALOG_OPTIONS = 'width=800, height=600, status=1, resizeable=1, scrollbars=1';

/*
	this function returns an event object
	depending on browser type
*/

function getEvnt()
{
  return brwsr_ie ? event : eventObj;
}

function openWindow(url, windowName, options)
{
  if (options.length == 0)
  {
    options = DEFAULT_DIALOG_OPTIONS;
  } //end if
	
  return window.open(url, windowName, options);
} //end function openWindow 

//Global variable set at start of script
var emptyString = " field is blank. Please enter a "

/*
	this function returns true
	if a given string is float value
	.0 or 0.0 or 0.00
*/

function isFloat(str)
{
	var pattern = /^\d*\.\d+$/;
	return pattern.test(str);
} //end function isFloat  

/*
	this function returns true
	if a given string is integer value
	0 or 1 or 1000000000 etc
*/

function isInteger(str)
{
	var pattern = /^\d+$/;
	return pattern.test(str);
} //end function isInteger 

/*
	same as isInteger
*/

function isDigits(str) 
{
	var pattern = /^\d+$/;
	return pattern.test(str);
} //end function isDigits 

function isNumber(str) {
  var mychar, numdecs = 0;
  for (i = 0; i < str.length; i++) 
	{
    mychar = str.charAt(i)
    if ((mychar >= "0" && mychar <= "9") || mychar == ".") 
  	{
      if (mychar == ".")
        numdecs++
    } //end if
    else
  	{
      return false;
  	} //else
  } //end for
	
  if (numdecs > 1)
	{
    return false;
	} //end if

  return true;
} //end function isNumber

/*
	this function makes sure that user
	is entering integers only
*/

function onKeyPressInteger()
{
  try
  {
    var inptChar = String.fromCharCode(getEvnt().keyCode);
    if (isDigits(inptChar))
		{
  		return true;
		} //end if
  } //try
  catch (e) {} 
	
	return false;
} //end function onKeyPressInteger

/*
	this function makes sure that user
	is entering float value only
*/

function onKeyPressFloat()
{
  try
  {
    var inptChar = String.fromCharCode(getEvnt().keyCode);
    if (isNumber(inptChar))
		{
  		return true;
		} //end if
  }
  catch (e) {} 
	
	return false;
} //end function onKeyPressFloat 

function onKeyPressTimeRange()
{
  try
  {
    var inptChar = String.fromCharCode(getEvnt().keyCode);
    if (isDigits(inptChar) | inptChar==":" | inptChar=="a" | inptChar=="p" | inptChar=="m" | inptChar=="-")
		{
  		return true;
		} //end if
  } //try
  catch (e) {}
	
  return false;
} //end function onKeyPressTimeRange 

function onKeyPressTime()
{
  try
  {
    var inptChar = String.fromCharCode(getEvnt().keyCode);
    if (isDigits(inptChar) | inptChar==":" | inptChar=="a" | inptChar=="p" | inptChar=="m" )
		{
  		return true;
		} //end if
  } //try
  catch (e) {}

  return false;
} //end function onKeyPressTime 

function onChangeFloat(field, fieldName)
{
  field.value = field.value.replace(/,/g,'.');
  field.value = field.value.replace(/\D^\./g,'');
  if (!isNumber(field.value)) 
	{
    alert(fieldName + ': Please input number. Use "." (dot) as decimal symbol.');
    field.value = '';
    return false;
  } //end if
	
  return true;
} //end function onChangeFloat 

function onChangeInteger(field, fieldName)
{
  field.value = field.value.replace(/\D/g,'');
  if (!isDigits(field.value)) 
	{
    alert(fieldName + ': Please input number');
    field.value = '';
    return false;
  } //end if
	
  return true;
} //end function onChangeInteger 

function showError(show, errElementId)
{
  var e = document.getElementById(errElementId);
  if (e != null)
  {
    if (show)
    {
      e.style.visibility = "visible";
    } //end if
    else
    {
      e.style.visibility = "hidden";
    } //else
  } //end if

  return show;
} //end function showError 

function isBlank(str) 
{
  return isNull(str);
} //end function isBlank

function isSize(str, size) 
{
  return (str.length == size);
} //end function isSize

function dialogListValidation(num, formfieldname, messagefieldname) {
  if (num == -1)
  {
    alert(messagefieldname + ":  requires a value. Please select a value.");
    formfieldname.focus();
    return false
  } //end if
  else
	{
    return true;
	} //else
} //end function dialogListValidation

function isInRange(myfield, fname, num1, num2) 
{
  var i = parseFloat(myfield.value);
  if (isNaN(num1) && isNaN(num2))
  {
    return true;
  } //end if

  if (isNaN(num2))
  {
    if(i >= num1)
      return true
    else
      alert(fname + ": enter a number greater than or equal to " + num1);
  } //end if
	
  if (isNaN(num1))
  {
    if(i <= num2)
      return true
    else
      alert(fname + ": enter a number less than or equal to " + num1);
  } //end if

  if (!isNaN(num1) && !isNaN(num2))
	{
    if ((i >= num1) && (i <= num2))
		{
      return true;
		} //end if
    else
    {
      alert(fname + ": enter a number between " + num1 + " and " + num2);
    } //else
	} //end if
	
  myfield.focus();
	
  return false;
} //end function 

function stripNonDigits(str) 
{
  var i, mychar, newstring = "";
  for (i = 0;  i < str.length; i++) 
	{
    mychar = str.charAt(i);
    if (isDigits(mychar))
		{
      newstring += mychar;
		} //end if
  } //end for
	
  return newstring;
} //end function stripNonDigits 

function stripChars(str, chars) 
{
  var i, mychar, newstring = "";
  for (i = 0;  i < str.length; i++) 
	{
    mychar = str.charAt(i);
    if (chars.indexOf(mychar) == -1)
		{
      newstring += mychar;
		} //end if
  } //end for
	
  return newstring;
} //end function stripChars

function validateString(myfield, s) {
  if (notNull(myfield.value)&& notBlank(myfield.value))
	{
    return true;
	} //end if
  else 
	{
    myfield.focus()
    alert("The " + s + emptyString + s)
    return false
  } //else
} //end function validateString 

/*
	this function returns true if
	str has atleast 1 character
*/

function notBlank(str) 
{
  for (i = 0; i < str.length; i++) 
	{
    if (str.charAt(i) != " ")
		{
      return true;
		} //end if
  } //end for
	
  return false;
}

/*
	this function return true if str
	is null or 0 length
*/

function isNull(str) {
  return (str.length == 0);
}

/*
	this function checks if a given date is correct
	yyyy-mm-dd or yyyy/mm/dd or yyyy.mm.dd
*/

function checkDate(myfield, fname) {
  var a,b,c,d,e,f, err=0;
  a = myfield.value;
	
  if (a.length != 10)
	{
  	return false;
	} //end if
	
  b = parseInt(a.substring(0, 2),10)// month;
  c = a.substring(2, 3)// '/';
  d = parseInt(a.substring(3, 5),10)// day;
  e = a.substring(5, 6)// '/';
  f = parseInt(a.substring(6, 10),10)// year;
	
  if ((b<1 || b>12 || isNaN(a.substring(0, 2)) || a.substring(0, 2).indexOf('.') !=-1) ) 
	{
  	return false;
	} //end if
	
  if (!checkDateFormat(a))
	{
  	return false;
	} //end if
	
  if ((d<1 || d>31|| isNaN(a.substring(3, 5)) || a.substring(3, 5).indexOf('.') != -1) ) 
	{
	  return false;
	} //end if
	
	//acceptable years range
  if (f<0000 || f>9999 || isNaN(a.substring(6, 10)) || a.substring(6, 10).indexOf('.') !=-1) 
	{
	return false;
	} //end if
	
  if (b==4 || b==6 || b==9 || b==11)
	{
    if (d==31) 
		{
		  return false
		} //end if
  } //end if
	
  if (b == 2)
	{
    var g = parseInt(f/4,10);
    if (isNaN(g)) 
  	{
      return false;
    }
    if (d>29) 
  	{
    	return false
  	} //end if
    if (d==29 && ((f/4)!= parseInt(f/4,10)))
		{
		  return false;
		} //end if
  } //end if
	
	return true;
	
} //end function checkDate 

/*
	following functions will remove spaces
	from a given string
	usage str.trim()
*/

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/*
	this function checks the date format
	yyyy/mm/dd or yyyy-mm-dd or yyyy.mm.dd
*/

function checkDateFormat(str)
{
  var pattern = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
  return pattern.test(str);
} //end function checkDateFormat

/*
	this function checks US Zip code format
	99999 or 99999-9999
*/

function checkZipCode(str)
{
  var pattern = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  return pattern.test(str);
} //end function checkZipCode

/*
	this function checks Canadian Postal code format
	Z5Z-5Z5 or Z5Z5Z5
*/

function checkPostalCode(str)
{
  var pattern = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;
  return pattern.test(str);
} //end function checkPostalCode

/*
	this function checks time format
	HH:MM or HH:MM:SS or HH:MM:SS.mmm
*/

function checkTimeFormat(str)
{
  var pattern = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;
  return pattern.test(str);
} //end function checkTimeFormat

/*
	this function checks ip address format
	999.999.999.999
*/

function checkIPAddress(str)
{
  var pattern = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  return pattern.test(str);
} //end function checkIPAddress

/*
	this function checks dollar amount format
	100, 100.00, $100 or $100.00
*/

function checkDollarAmount(str)
{
  var pattern = /^((\$\d*)|(\$\d*\.\d{2})|(\d*)|(\d*\.\d{2}))$/;
  return pattern.test(str);
} //end function checkDollarAmount

/*
	this function checks social security number format
	999-99-9999 or 999999999
*/

function checkSocialSecurityNumber(str)
{
  var pattern = /^\d{3}\-?\d{2}\-?\d{4}$/;
  return pattern.test(str);
} //end function checkSocialSecurityNumber

/*
	this function checks social insurance number format
	999999999
*/

function checkSocialInsuranceNumber(str)
{
  var pattern = /^\d{9}$/;
  return pattern.test(str);
} //end function checkSocialInsuranceNumber


/*
	this function checks social insurance number format
	9999999999 or 999-999-9999
*/

function checkPhoneFormat(str) 
{
	var pattern = /^\d{3}(\-)*\d{3}\1\d{4}$/;
	return pattern.test(str);
} //end function checkPhoneFormat 

/*
	this function checks email format
	john@domain.com or john.doe@email.ca
*/

function checkEmailFormat(str)
{
  var pattern = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{2,6})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2,6})*$)/i;
	return pattern.test(str);
} //end function checkEmailFormat 

String.prototype.ucwords = function()
{
  var str_arr = this.split(" ");
	var new_str = "";
	for(var i=0; i<str_arr.length; i++)
	{
	  new_str += str_arr[i].charAt(0).toUpperCase() + str_arr[i].substr(1) + " ";
	} //end for i
	
	return new_str.trim(); 
}; //end prototype

function get_field_name(field)
{
  var tmp_arr = field.getAttribute('name').split("_");
  var field_name = '';
  for(var i=0; i<tmp_arr.length; i++)
  {
    field_name += tmp_arr[i].ucwords() + " ";
  } //end for
	
	return field_name.trim();
} //end function get_field_name 

/***************************************************
* this function will check the length of string
***************************************************/

function check_length(field, len) {
  if(field != null) {
	  if(field.value.length <= len) {
		  return true;
		} // if length within limit
		else {
		  field.select();
			field.focus();
			alert("Please limit the number of characters to "+len);
			return false;
		} // else too long
	} //if not null
	
	return true;
	
} //end function check_length

/***************************************************
* this function will check if a string is left blank
***************************************************/

function check_text(field) {
  
	if(field.value == '') {
		alert("Missing a required field: "+get_field_name(field));
		if(!field.disabled)
		{
	    field.select();
      field.focus();
		} //end if
		return false;
	}
	return true;
} //end function check_text()

/***************************************************
* this function will check the email format: joe@johnson.com
***************************************************/

function check_email(email) {
  //got this string from http://www.codetoad.com/javascript/is_valid_email.asp
	if(!(email.value.indexOf(".") > 2 && email.value.indexOf("@") > 0)) {
	  alert("Please enter a valid email address\n\nExample: joe@johnson.com");
		email.select();
		email.focus();
		return false;
	} //if
	
	return true;
} //end function check_email()

function validateJoinMailingListFor(frm)
{
  return check_text(frm.name) &&
				 check_text(frm.email) &&
				 check_email(frm.email);
} //end function validateJoinMailingListForm 

