/*
	VERSION 1.1
	changes: improved validate_DATE

'+---------------------------------------------------------------+
'| COPYRIGHT 2002 MACHROTECH, LLC                                |
'| http://www.machrotech.com                                     |
'|                                                               |
'| This software contains confidential information which is the  |
'| property of MachroTech, LLC. This entire software package is  |
'| protected by the copyright laws of the United States and      |
'| elsewhere. All rights are reserved. No part of this software  |
'| may be copied, transcribed or used without the express 		 |
'| written permission of MachroTech, LLC. This includes, but is  |
'| not limited to the source code, designs, concepts, 			 |
'| interfaces, and documentation that are associated with this 	 |
'| software and its development. Any violation of the copyright  |
'| laws and regulations of the United States and elsewhere will  |
'| be reported to the appropriate authorities and prosecuted.	 |
'+---------------------------------------------------------------+
*/
	function validate_TEXT(obj, sFieldName)
	{
		var exp = /^(\S|\s)+$/
		return checkExp(exp, obj, "The " + sFieldName + " must be filled out")
	}

	function validate_ZIP(obj, sFieldName)
	{
		var exp = /^\d{5}$/
		return checkExp(exp, obj, "Please enter a valid ZIP code (5 digits only)")
	}

	function validate_PHONE(obj, sFieldName)
	{
		var exp = /^((\(\d{3}\) ?)|(\d{3}(-| )?))\d{3}(-| )?\d{4}$/
		return checkExp(exp, obj, "Please enter a valid US phone number (10 digits)")
	}
	
	function validate_EMAIL(obj, sFieldName)
	{
		var exp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
		return checkExp(exp, obj, "Please enter a valid email address")
	}
	
	function validate_UPC(obj, sFieldName)
	{
		var exp = /(^\d{6}$)|(^\d{12}$)/
		return checkExp(exp, obj, "Please enter a valid UPC code (6 or 12 digits)")
	}

	function validate_SSN(obj, sFieldName)
	{
		var exp = /^\d{3}-\d{2}-\d{4}$/
		return checkExp(exp, obj, "Please enter a valid tax ID or SSN in the format nnn-nn-nnnn")
	}

	function validate_WEIGHT(obj, sFieldName)
	{
		if (validate_DOUBLE(obj, sFieldName))
			if (obj.value <= 0)
			{
				alert("Please enter a number greater than zero for the weight")
				obj.focus();
				return false
			}
			else
				return true
		else
			return false
	}
	
	function validate_CURRENCY(obj, sFieldName)
	{
		obj.value = obj.value.replace("$", "")
		obj.value = obj.value.replace(",", "")
		
		if (validate_DOUBLE(obj, sFieldName))
			if (obj.value < 0)
			{
				alert("Please enter a number greater or equal to zero for the " + sFieldName)
				obj.focus();	
				return false
			}
			else
			{
				obj.value = parseInt((parseFloat(obj.value) + 0.005) * 100) / 100
				return true
			}
		else
			return false		
	}	
	
	function validate_INTEGER(obj, sFieldName)
	{
		var val
		
		if (parseInt(obj.value) < 0)
			val = parseInt(parseInt(obj.value) - 0.5)
		else
			val = parseInt(parseInt(obj.value) + 0.5)	
		
		if (isNaN(val) || obj.value == "")
		{
			alert("Please enter a numeric value")
			obj.focus();	
			return false
		}
		else
		{
			obj.value = val
			return true
		}
	}

	function validate_DOUBLE(obj, sFieldName)
	{
		var val = parseFloat(obj.value)
		
		if (isNaN(val) || obj.value == "")
		{
			alert("Please enter a numeric value for the " + sFieldName)
			obj.focus();	
			return false
		}

		obj.value = val
		return true
	}
	
/*	
	function validate_DATE(obj, sFieldName)
	{
  		var testDate=new Date(Date.parse(obj.value));
  		var tempDate = 	(testDate.getMonth() + 1) + "/" + testDate.getDate() + "/" + testDate.getYear()

		if(!testDate.getYear())
		{
		    alert("Please enter a valid date in the format mm/dd/yyyy");
			obj.focus();
		    return false;
		}
		
		obj.value = tempDate
		
		return true
	}
*/	
	
	function validate_DROPDOWN(obj, sFieldName)
	{	
		if (obj.selectedIndex == 0)
		{
			alert("Please select an option for the " + sFieldName)
			obj.focus();
			return false
		}
		
		return true
	}
	
	function validate_CUSTOM(obj)
	{
		return true
	}

	function validate_FILE(obj, sFieldName)
	{
		if (obj.value == "")
		{
			alert("Please browse for a file")
			obj.focus();
			return false
		}
		
		return true
	}

	function validate_TEXTAREA(obj, maxlen)
	{
		if (validate_TEXT(obj))
			if (obj.value.length > parseInt(maxlen))
			{
				alert("The maximum length for this text field is " + maxlen + " characters.\nPlease shorten the amount of text you entered.")
				obj.focus();
				return false
			}
			else
				return true
		else
			return false
	}

	function validate_CHECKBOX(obj, sFieldName)
	{
		return true
	}
	
	function validate_CUSTOM(obj)
	{
		return true
	}

	function validate_PASSWORD(obj)
	{
		var exp = /^([0-9a-zA-Z]){4,}$/
		return checkExp(exp, obj, "The password must be at least 4 characters long (letters and numbers only)")
	}
	
	function validate_HIDDEN(obj, sFieldName)
	{
		return true
	}		
	
	function trim(value)
	{
   		var exp = /^(\s*)(\S*)(\s*$)/;
   		if (exp.test(value)) 
   			value = value.replace(exp, '$2');
   			
   		return value;
   	}

	function checkExp(exp, obj, message)
	{
		obj.value = trim(obj.value)
				
		if (!exp.exec(obj.value))
		{
			alert(message);
			obj.focus();
			return false;
		}		
		
		return true;
	}
	
	function open_calendar(obj)
	{
		setDateField(obj);
   		top.newWin = window.open('/shared/calendar.html', 'cal', 'dependent=yes,width=210,height=230,screenX=200,screenY=300,titlebar=yes')
	}
	
	
	function validate_DATE(obj, sFieldName)
	{
  		var testDate=new Date();
  		
  		var blnResult = isDateFormatted(obj.value, testDate, "mm/dd/yyyy")
  		
  		var tempDate = 	(testDate.getMonth() + 1) + "/" + testDate.getDate() + "/" + testDate.getYear()

		if(!blnResult)
		{
		    alert("Please enter a valid date in the format mm/dd/yyyy");
			obj.focus();
		    return false;
		}
		
		obj.value = tempDate
		
		return true;
	}	
	
	
	/* EXAMPLES
	
	var d = new Date()
	
	alert(isDateFormatted("21/5/2002",d,"dd/mm/yyyy"))
	alert(d.getMonth())
	alert(isDateFormatted("12/22/2002"))
	
	*/
	
	//this function requires a 4 digit year (add leading 0s if necessary)
	function isDate (year, month, day, dtmOutputDate)
	{   // catch invalid years (4-digit) and invalid months and days.
	
	    if (! (isYear(year) && isMonth(month) && isDay(day))) return false;
	
	    // Explicitly change type to integer to make code work in both
	    // JavaScript 1.1 and JavaScript 1.2.
	    var intYear = parseInt(year, 10);
	    var intMonth = parseInt(month, 10);
	    var intDay = parseInt(day, 10);
	    
	    // catch invalid days, except for February
	    if (intDay > daysInMonth(intMonth)) return false; 
	
	    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
	    
	    var dtmOutput = new Date(intYear, intMonth - 1, intDay)
	    dtmOutputDate.setYear(dtmOutput.getYear());
	    dtmOutputDate.setMonth(dtmOutput.getMonth());
	   	dtmOutputDate.setDate(dtmOutput.getDate());
	
	    return true;
	}
	
	//checks the date according to format
	//valid formats: "mm/dd/yyyy", "mm-dd-yyyy", "dd/mm/yyyy", "dd-mm-yyyy", "yyyymmdd"
	// "-" and "/" are interchangeable and do not affect the result (i.e. "1/22/2002" is valid 
	// even if the format is "dd-mm-2002"
	//dtmOutputDate has to be a Date object if you want to get a result out (JS only passes objects by reference)
	function isDateFormatted(strDate, dtmOutputDate, strFormat)
	{
		var arr = Array(3);
		
		if (strFormat == null)
			strFormat = "mm/dd/yyyy";
		
		if (strFormat.indexOf("/") >= 0 || strFormat.indexOf("-") >= 0)
		{
			if (!checkDelimiters(strDate, arr))
			{
				return false;
			}
		}
		else
		{
			arr[0] = strDate.substring(0, 3);
			arr[1] = strDate.substring(4, 5);
			arr[2] = strDate.substring(6, 7);
		}
		
		switch (strFormat)
		{
			case "dd/mm/yyyy":
			case "dd-mm-yyyy":
				return isDate(arr[2], arr[1], arr[0], dtmOutputDate);
			case "yyyymmdd":
				return isDate(arr[0], arr[1], arr[2], dtmOutputDate);
			default:
				return isDate(arr[2], arr[0], arr[1], dtmOutputDate);
		}
	}
	
	function checkDelimiters(strDate, arrIn)
	{
		var arr = strDate.split("/");
		if (arr.length < 3)
			arr = strDate.split("-");
						
		if (arr.lenght < 3)
			return false;
		else
		{
			arrIn[0] = arr[0];
			arrIn[1] = arr[1];		
			arrIn[2] = arr[2];		
			return true;
		}
	}
	
	
	function daysInMonth(intMonth)
	{
		switch (intMonth)
		{
			case 1:
				return 31;
			case 2:
				return 29;
			case 3:
				return 31;
			case 4:
				return 30;
			case 5:
				return 31;
			case 6:
				return 30;
			case 7:
				return 31;
			case 8:
				return 31;
			case 9:
				return 30;
			case 10:
				return 31;
			case 11:
				return 30;
			case 12:
				return 31;
			default:
				return 0
		}
	}
	
	function daysInFebruary (year)
	{   // February has 29 days in any year evenly divisible by four,
	    // EXCEPT for centurial years which are not also divisible by 400.
	    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
	}
	
	function isYear (s)
	{	if (!isNonnegativeInteger(s)) return false;
	    return (String(s).length == 4);
	}
	
	function isMonth (s)
	{ 
		return isIntegerInRange (s, 1, 12);
	}
	
	function isDay (s)
	{
	    return isIntegerInRange (s, 1, 31);
	}
	
	function isNonnegativeInteger (s)
	{
	    return (parseInt(s, 10) >= 0 );
	}
	
	function isIntegerInRange (s, a, b)
	{ 
	    var num = parseInt (s);
	    return ((num >= a) && (num <= b));
	}	

