// Custom Lair javascripts written by J.P.
// Enforces Postal/ZIP Code and State/Province codes if Country is Canada or US
function PCValidate() {
	Z=document.getElementById("PCZIP").value
	S=document.getElementById("StateProv").value
	C=document.getElementById("Country").value
	if((Z == '' || S == '') && (C == 'US' || C == 'CA'))
		{
		return false;
		}
	reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
	if ((C == 'US') && (!reZip.test(Z)))
		{
		return false;
		}
	return true;
	}
	
// General onvalidate routine to make sure that a cfselect option has been chosen (other than the --select --)
// must have some value other than a null string to be passed as true
// onvalidate always passes 3 parameters: the form object, the field object and the value of the field
function CheckCFSelect(form,field,value) {
	if (value == '')
		{
		return false;
		}
	return true;
}

// Returns the current date in mm/dd/yy format as a string.
function todayStr() 
{
	var today=new Date()
	myyear=today.getYear();
	if(myyear < 500) 
	{
		myyear += 1900;
	}
	return today.getMonth()+1+"/"+today.getDate()+"/"+myyear;
}

// Validate that both passwords are the same
function PasswordCheck() {
	P1=document.getElementById("Password").value;
	P2=document.getElementById("RepeatPassword").value;
	if (P1 == P2)
	{
		return true;
	}
	return false;
}

// from common.js
function turnoff(id) 
{
	var e = document.getElementById(id);
	e.style.display = 'none';
}

function turnon(id) 
{
	var e = document.getElementById(id);
	e.style.display = 'block';
}

function flipvis(id) 
{
	var e = document.getElementById(id);
	if (e.style.display == 'block')
	{
		e.style.display = 'none';
	}
	else
	{
		e.style.display = 'block';
	}
}

//  function to check for valid number between 2 supplied values	
function IsNumber(sText, minval, maxval)
{
   var ValidChars = "0123456789";
   var IsGood=true;
   var Char;
 
   for (i = 0; i < sText.length && IsGood == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsGood = false;
         }
      }
   if(!IsGood) return false;
   if (sText.length > 0 && sText >= minval && sText <= maxval) return true;
}

// function to test for a valid email address
function IsEMail(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;

	return true;				
}

// function to test for a valid date in format mm/dd/yyyy
function IsDate(input)
{
	var validformat=/^\d{1,2}\/\d{1,2}\/\d{2,4}$/ //Check for format validity (1-2 digits / 1- 2 digits / 2-4 digits
	if (!validformat.test(input))  return false;
	var monthfield=input.split("/")[0]
	var dayfield=input.split("/")[1]
	var yearfield=input.split("/")[2]
	var dayobj = new Date(yearfield, monthfield-1, dayfield)
	if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) return false;
	return true;
}

// function to get the checked value of a radio button
function GetCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// function to open a CF Ajax window and invoke the code that runs it
function WinGo(WindowName,WindowCode) {
	ColdFusion.Window.show(WindowName);
	ColdFusion.navigate(WindowCode,WindowName);
}

// function to check for valid username or password
function IsValidUserPass(input) 
{
	var validformat=/^[A-Za-z0-9]{4,20}$/ 	// rules for a valid username or password
	if (validformat.test(input)) return true;
	return false;
}

// function to allow Ajax windows to scroll to the top of the page
function ScrollTop() 
{
	window.scroll(0,0);
}
	
