// Helper function to mess w/ DateTime
this.GetDateFromDateTime = function( sDateTimeIn )
{
	var dDateOut = new Date( sDateTimeIn );
	if( isNaN( dDateOut ) ) 
	{	
		return "";
	}
	else
	{
		return this.MakeTwoDigits(dDateOut.getMonth() + 1) + "/" + this.MakeTwoDigits(dDateOut.getDate()) + "/" + this.MakeTwoDigits(dDateOut.getFullYear());	
	}
		
}

this.GetTimeFromDateTime = function( sDateTimeIn )
{
	
	//Check to see if there's a time component
	// assume that, in the string, if there is a :, there is a time
	
	if( String(sDateTimeIn).indexOf(":") == -1 )
	{
		return "";	
	}
	
	// So, there is probably a time in there
	// test to see if it's a real date
	var dDateOut = new Date( sDateTimeIn );
	if( isNaN( dDateOut ) ) 
	{	
		// may be missing the date component
		// add random date to front and see if it still works out to a date
		var dTemp = new Date( "1/1/2000" + " " + sDateTimeIn );
		if( isNaN( dTemp ) )
		{
			return "";	
		}
		else
		{
			dDateOut = dTemp;
		}
	}

	// Figure AM or PM
	var iHours = dDateOut.getHours();
	if(iHours==0||iHours==12)
	var iHoursOut = 12;
	else
	var iHoursOut = iHours % 12;
	var sTimeOut = iHoursOut + ":" + this.MakeTwoDigits(dDateOut.getMinutes());
	
	var ap = "AM";
	if (iHours   > 11) { ap = "PM"; }
	
	sTimeOut = sTimeOut;// + " " + ap;
	
	// pump it out
	return sTimeOut;

}
	
this.GetAMPMFromDateTime = function(sDateTimeIn)
{
	if( String(sDateTimeIn).indexOf(":") == -1 )
	{
		return "";	
	}
	
	// So, there is probably a time in there
	// test to see if it's a real date
	var dDateOut = new Date( sDateTimeIn );
	if( isNaN( dDateOut ) ) 
	{	
		// may be missing the date component
		// add random date to front and see if it still works out to a date
		var dTemp = new Date( "1/1/2000" + " " + sDateTimeIn );
		if( isNaN( dTemp ) )
		{
			return "";	
		}
		else
		{
			dDateOut = dTemp;
		}
	}

	// Figure AM or PM
	var iHours = dDateOut.getHours();
	var iHoursOut = iHours % 12;
	var sTimeOut = iHoursOut + ":" + this.MakeTwoDigits(dDateOut.getMinutes());
	
	var ap = "AM";
	if (iHours   > 11) { ap = "PM"; }
	
	//sTimeOut = sTimeOut;// + " " + ap;
	
	// pump it out
	return ap;
	
}

function MakeTwoDigits(intNumber) 
{ 
	if (intNumber < 10) 
		return ( "0" + intNumber); 
	else 
		return ( intNumber ); 
}

function isValidDate(dateStr) 
{
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
   
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})/;

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
	//alert("Date is not in a valid format.")
	return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
	alert("Month must be between 1 and 12.");
	return false;
	}
	if (day < 1 || day > 31) {
	alert("Day must be between 1 and 31.");
	return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	alert("Month "+month+" doesn't have 31 days!")
	return false
	}
	if (month == 2) { // check for february 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day>29 || (day==29 && !isleap)) {
	alert("February " + year + " doesn't have " + day + " days!");
	return false;
	}
	}
	return true;  // date is valid
}

this.ValidateZip = function(sZip)
{
	var bValid = true;
	
	if( sZip )
	{
		 //check for valid Zipcodes
		var objRegExpUS  = /(^\d{5}$)|(^\d{5}-\d{4}$)/ ;
	  	var objRegExpCanada = /[A-Z]\d[A-Z] \d[A-Z]\d/ ;
		var objRegExpCanadaWithoutSpace =  /[A-Z]\d[A-Z]\d[A-Z]\d/ ;
		var objRegExpCanadaWithHyphen =  /[A-Z]\d[A-Z]-\d[A-Z]\d/ ;
		
		//set to upper case
		sZip = sZip.toUpperCase();
		
	  	//check if this is a canadian witout the space
	  	if (objRegExpCanadaWithoutSpace.test(sZip) && sZip.length == 6)
	  	{
	  		//add the space in after the third character
	  		var sFirstThreeChar = sZip.substring(0,3);
	  		var sLastThreeChar = sZip.substring(3,6);
	  		sZip = sFirstThreeChar + " " + sLastThreeChar ;
	  	}
	
		//check if this is cnadian with a hyphen
		if (objRegExpCanadaWithHyphen.test(sZip) && sZip.length == 7)
		{
			//add the space in after the third character
			var sFirstThreeChar = sZip.substring(0,3);
			var sLastThreeChar = sZip.substring(4,7);
			sZip = sFirstThreeChar + " " + sLastThreeChar ;
	  	}
	
	
	  if (!objRegExpUS.test(sZip) && !objRegExpCanada.test(sZip))
	  {
			bValid = false;						
	 		
	  }
	
	}
	return bValid;
}	

// Sub-Function
this.StringChk = function(cMainString, cChkString) 
{
	var i, j;
	var StringChk = "";
	for (i = 0; i < cMainString.length; i++)
	{
		for (j = 0; j < cChkString.length; j++)
		{
			if (cMainString.substr(i,1) == cChkString.substr(j, 1))
				StringChk += cMainString.substr(i, 1);
		}
	}
	if (cMainString.length == StringChk.length)
		return true;
	else
		return false;
}

this.ValidatePhone = function(sPhone)
{
	var bValid = true;	
	if (sPhone)
	{
		if (sPhone.length < 12) bValid = false;
		
		if (!this.StringChk(sPhone,"0123456789-"))	bValid = false;				
	}
	return bValid;
}

function EmailChk(cMainString)
{

var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	
// The following matches the basic syntax of an e-mail address,
// according to the following rules (in order of appearance):
//
// One or more characters before the "@".
// An optional "[", because user@[255.255.255.0] is a valid e-mail.
// A sequence of letters, numbers, and periods, which are all valid
// domain or IP address characters.
// A period followed by a 2-3 letter suffix.
// An optional "]".

var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid

if (!reg1.test(cMainString) && reg2.test(cMainString))
{
	return true;
}
else
{
	return false;
}	
}

function DropDown_SetSelected(options, value)
{
    for (var x = 0; x < options.length; x++)
    {
        if (options[x].value == value)
            options[x].selected = true;
    }
}

function getChecked(list)
{
    for ( var i = 0; i < list.length; i++ )
    {             
        if ( list[i].checked == true )
        {  
            return list[i].value;
        }
    }
}
function setChecked(list, value)
{
    for ( var i = 0; i < list.length; i++ )
    {             
        if ( list[i].value == value )
        {  
            return list[i].checked=true;
        }
    }
}

