/*---------------------------------------------------------------------------------------------------
 Purpose    : Contains javascript functions for formfield validation
 Program		: QTCM
 Programmer	: Shirley To
 Last Update: Jan 04, 2007
-----------------------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------
 Purpose    : centers a screen
 Parameters : none
 Returns    : none
-------------------------------------------------------------------------------------------*/
function CenterPg()
{
    var x, y;

    x = (window.screen.availWidth - document.body.clientWidth) / 2;
    y = (window.screen.availHeight - document.body.clientHeight) / 2;

    try
    {                       
		window.moveTo(x,y);
    }
    catch (e)
    {
		return false;
    }
}

/*-------------------------------------------------------------------------------------------
 Purpose	: Checks whether an input value is an integer
			  Returns false if input isn't an integer
 Parameters	: num_str - the number string
 Returns	: true or false
-------------------------------------------------------------------------------------------*/
function Is_Integer(num_str)
{
	// Trim the string before checking
	for (i=0; i<num_str.length; i++)
	{
		if (num_str.charCodeAt(i) < 48 || num_str.charCodeAt(i) > 59)
			return false;
	}
	return true;
}


/*----------------------------------------------------------------------
 Purpose    : Validate the format of an email address
 Parameters : email_field - the field containing an email address
 Returns    : true if email address is in valid format, false otherwise
----------------------------------------------------------------------*/
function Validate_Email(email_field)
{
    var email_exp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    
    if (email_field.value != "")
    {
		if (email_field.value.search(email_exp) == -1)
		{
		    alert("Invalid e-mail address format.  Please re-enter.");
		    email_field.focus();
		    email_field.select();
		    return false;
		}
		else
			return true;
	}
	else
		return true;
}

/*----------------------------------------------------------------------
 Purpose    : Validate a zip code
 Parameters : zip_field - the field containing a zip code
 Returns    : true if zip code is good.
----------------------------------------------------------------------*/
function Validate_Zip(zip_field)
{
    var zip_exp = /^\d{5}$|^\d{5}[\-=>\s]?\d{4}$/;
    
    if (zip_field.value != "")
    {
		if (zip_field.value.search(zip_exp) == -1)
		{
		    alert("Invalid zip code format.  Please re-enter.");
		    zip_field.focus();
		    zip_field.select();
		    return false;
		}
		else
			return true;
	}
	else
		return true;
}


/*----------------------------------------------------------------------
 Purpose    : Validate a ssn
 Parameters : ssn_field - the field containing a ssn
 Returns    : true if ssn is good.
----------------------------------------------------------------------*/
function Validate_SSN(ssn_field)
{
    var ssn_exp = /^\d{9}$/;
    
    if (ssn_field.value != "")
    {
		if (ssn_field.value.search(ssn_exp) == -1)
		{
		    alert("Invalid ssn format.  Please re-enter.");
		    ssn_field.focus();
		    ssn_field.select();
		    return false;
		}
		else
			return true;
	}
	else
		return true;
}


/*----------------------------------------------------------------
 Purpose	: Validates a date selected from 3 separate dropdown menus.
 Parameters : month - month field
			  day	- day field
			  year	- year field
 Returns	: true or false
----------------------------------------------------------------*/
function Validate_Date(month, day, year)
{
	var big_months = "'1','3','5','7','8','10','12'";
	var num_feb_days = 28;
	var month_val, day_val, year_val;
	month_val = month.value;
	day_val = day.value;
	year_val = year.value;
	
	// NEED TO VALIDATE THE DATE
	if (big_months.indexOf("'" + month_val + "'") == -1)	// months with 30 days or less
	{
		if (month_val == 2)
		{
			// check for leap year
			if (year_val % 4 == 0)				// year evenly divisble by 4
				num_feb_days = 29;
			else if (year_val % 100 == 0 && year_val % 400 == 0)	// and  evenly divisible by 400
				num_feb_days = 29;		// LEAP YEAR
			
			if (day_val > num_feb_days)
			{
				alert("Februaray in year " + year_val + " has " + num_feb_days + " days only.  Please select again.");
				day.selectedIndex = num_feb_days;
				day.focus();
				return false;
			}
		}
		else if (day_val > 30)
		{
			alert("Selected month has less than 31 days.  Please select again.");
			return false;
		}
	}
	return true;
}

/*----------------------------------------------------------------
 Purpose	: Validates a date string.
 Parameters : month_val - month val
			  day_val	- day val
			  year_val	- year val
 Returns	: true or false
----------------------------------------------------------------*/
function Validate_Date_Vals(month_val, day_val, year_val)
{
	var big_months = "'1','3','5','7','8','10','12'";
	var num_feb_days = 28;
	var num_year, num_month, num_day;
	
	num_year = Number(year_val);
	num_month = Number(month_val);
	num_day = Number(day_val);

	
	// VALIDATE MONTH
	if (num_month > 12 || num_month < 1)
	{
		alert("Invalid month value.  Please re-enter.");
		return false;
	}
	
	// VALIDATE DATE
	else if (big_months.indexOf("'" + Number(month_val) + "'") == -1)	// months with 30 days or less
	{
		if (num_month == 2)
		{
			// check for leap year
			if (year_val % 4 == 0)				// year evenly divisble by 4
				num_feb_days = 29;
			else if (year_val % 100 == 0 && year_val % 400 == 0)	// and  evenly divisible by 400
				num_feb_days = 29;		// LEAP YEAR
			
			if (day_val > num_feb_days)
			{
				alert("Februaray in year " + year_val + " has " + num_feb_days + " days only.  Please re-enter.");
				return false;
			}
			else
				return true;
		}
		else if (num_day > 30)
		{
			alert("Specified month has less than 31 days.  Please re-enter.");
			return false;
		}
		else if (num_day < 1)
			alert("Invaild date.  Please re-enter.");
		else
			return true;
	}
	else if (num_day > 31 || num_day < 1)
	{
		alert("Invalid date.  Please re-enter.");
		return false;
	}
	
	// VALIDATE YEAR (rough)
	else if (num_year < 1900)
	{
		alert("Invalid year value.  Please re-enter.");
		return false;
	}
	else
		return true;
}

/*----------------------------------------------------------------------
 Purpose    : Checks if the date supplied is in the future.
 Parameters : m, d, y - month, day, and year values of the date
 Returns    : true - if date is in future
			  false - if date is today or in the past
----------------------------------------------------------------------*/
function Is_Future_Date(m, d, y)
{
    var d, today;
    
    today = new Date();
    d = new Date(m + "/" + d + "/" + y);
      
    if (d.valueOf() > today.valueOf())
		return true;
	else
		return false;
}

/*----------------------------------------------------------------------
 Purpose	: Checks whether or not a string is a valid date
 Parameters	: sDate - the date string to check
 Returns	: true	- if string is a valid date (eg. 1/2/2002; 02/3/2002; 12/13/2002)
			  false - if string is not a valid date
----------------------------------------------------------------------*/
function Check_Date_Str(sDate)
{
	var date_parts;

	dateRegExp = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (sDate != "")
	{
		if (dateRegExp.test(sDate))
		{
			date_parts = sDate.split("/");
			
			if (Validate_Date_Vals(date_parts[0], date_parts[1], date_parts[2]))
			{
				if (date_parts[2] > 1900)
					return true;
				else
					return false;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	else
		return true;
}

/*------------------------------------------------------------------------------------
 Purpose	: Compares 2 dates:
 Parameters : sDate1, sDate2
 Returns    : 1 - if 1st date is later than 2nd one
			  0 - if they equal
			  -1 - if 1st date earlier than the 2nd one
------------------------------------------------------------------------------------*/
function Dates_Compare(sDate1, sDate2)
{
	var oDate1 = new Date(sDate1);
	var oDate2 = new Date(sDate2);
	
	var sDate1;
	var sDate2;
	
	sDate1 = oDate1.valueOf();
	sDate2 = oDate2.valueOf();
	
	if (sDate1 > sDate2)
		return 1;
	else if (sDate1 < sDate2)
		return -1;
	else
		return 0;
}

/*------------------------------------------------------------------------------------
 Purpose	: Checks whether the value of an input field is numeric or not
 Parameters : field - the field containing a value to be checked
 Returns    : true if value is numeric, false otherwise
------------------------------------------------------------------------------------*/
function Is_Numeric(field)
{
	if (isNaN(field.value))		//not numeric
	{
		alert("Please enter a numeric value.");
		field.focus();
		field.select();
		return false;
	}
	else
		return true;
}


/*------------------------------------------------------------------------------------
 Purpose	: Trims leading and ending spaces from the value of a form field
 Parameters : field - the field containing the string to be worked on
 Returns    : nothing
------------------------------------------------------------------------------------*/
function Trim(field)
{
	while('' + field.value.charAt(0) == ' ')
		field.value = field.value.substring(1,field.value.length);

	while('' + field.value.charAt(field.value.length-1) == ' ')
		field.value = field.value.substring(0,field.value.length - 1);
	return;
}

/*------------------------------------------------------------------------------------
 Purpose	: Trims leading and ending spaces from a string
 Parameters : str - the str to be trimmed
 Returns    : the trimmed string
------------------------------------------------------------------------------------*/
function Trim_Str(str)
{
	while('' + str.charAt(0) == ' ')
		str = str.substring(1, str.length);

	while('' + str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);

	return str;
}

/*------------------------------------------------------------------------------------
 Purpose	: Validates an integer (range, whethter it's a positive integer)
 Parameters : field - the field containing the string to be worked on
 Returns    : true
------------------------------------------------------------------------------------*/
function Validate_Integer(field)
{
	var int_exp = /^\d$/;
	var alpha;

	if (field.value != "")
	{
		if (!isNaN(field.value))
		{
			alpha = field.value.search(int_exp);
			if (field.value <= 0)
			{
				alert("Integer cannot be smaller than 0.  Please re-enter.");
				field.focus();
				field.select();
				return false;
			}
			else if(field.value >= 2147483647)
			{
				alert("Integer out of range.  Please re-enter.");
				field.focus();
				field.select();
				return false;
			}
			else
				return true;
		}
		else
		{
			alert("Field must be numeric value.  Please re-enter.");
			field.focus();
			field.select();
			return false;
		}
	}
	return true;
}

/*------------------------------------------------------------------------------------
 Purpose	: Validates a phone number; has to be a 9-digit string
 Parameters : field - the field containing the phone number
 Returns    : true
------------------------------------------------------------------------------------*/
function Validate_Phone(field)
{
	if (field.value != "")
	{
		if (isNaN(field.value) || field.value.indexOf("-") > -1 || field.value.indexOf(".") > -1)
		{
			alert("Please enter phone number in the format indicated on the screen.");
			field.focus();
			field.select();
			return false;
		}
		else if (field.value.length != 10)
		{
			alert("Please enter phone number in the format indicated on the screen.");
			field.focus();
			field.select();
			return false;
		}
		else
			return true;
	}
	else
		return true;
}

/*------------------------------------------------------------------------------------
 Purpose	: Notifies the user what text is outside of the length limit
------------------------------------------------------------------------------------*/
function Highlight_Extra_Text(text, limit)
{
	var bad_text = new String(text);
	
	bad_text = bad_text.slice(limit, text.length);
		
	text = text.substr(0, limit) + "\n=== *****PLEASE DELETE THIS LINE OF TEXT AND THOSE THAT FOLLOW***** ===\n" + bad_text;
	
	return text;
}


/*------------------------------------------------------------------------------------
 Purpose	: Validates an integer id (range, whethter it's a positive integer)
 Parameters : field - the field containing the string to be worked on
 Returns    : true
------------------------------------------------------------------------------------*/
function Validate_ID(field)
{
	var int_exp = /^\d$/;
	//var alpha;

	if (field.value != "")
	{
		if (!isNaN(field.value))
		{
			//alpha = field.value.search(int_exp);
			
			if (field.value <= 0)
				return false;
			else if(field.value >= 2147483647)
				return false;
			else
				return true;
		}
		else
			return false;
	}
	return true;
}


/*------------------------------------------------------------------------------------
 Purpose	: Display a calendar for user to pick a date
 Parameters : dest_field:	the field to be populated once a date is chosen
			  arguments[1]: if exists, put focus on the dest_field
 Returns    : Nothing
------------------------------------------------------------------------------------*/
function Show_Calendar(dest_field, root_dir)
{
	var arr;
	var callerWin;			// caller window
	var modal_return;		// return value from the module dialog box


	if (Check_Date_Str(dest_field.value))
	{
		modal_return = window.showModalDialog("/" + root_dir + "/calendar/calendar.asp?chosen="+dest_field.value,dest_field,"dialogHeight:240px;dialogWidth:235px;center:1;status:0;help:0;scroll:0");
	
		//window.open("/" + root_dir + "/calendar/calendar.asp?chosen="+dest_field.value,"winCal","height=275,width=235,status=0,scrollbars=0");
	
		if (arguments[2])
		{
			if (modal_return != -1)	//the Cancel button was clicked
			{
				arr = arguments[2];
				callerWin = arr[0];
				callerWin.Submit_Form(callerWin.document.forms[0]);
			}
		}
	}
	else
	{
		dest_field.focus();
		dest_field.select();
		return false;
	}
}

/*------------------------------------------------------------------------------------
 Purpose	: Controlling the appearance of a button using stylesheet
				(mainly used for the calendar button now)
 Parameters : img:  the image
			  position: -1: depressed
						0 : normal
						1 : raised
 Returns    : 
------------------------------------------------------------------------------------*/
function Button(img, position)
{
	switch(position)
	{
		case -1:		// pushed
			img.className = "pushed";
		break;
			
		case 0:			// flag
			img.className = "flat";			
		break;
			
		case 1:			//raised
				
			img.className = "raised";
				
		break;
	}
}

