/*
js-api.js
These are global JavaScript functions
*/

// If session.loggedin is 0, then take user to login page
function reset()
	{
		top.location="index.cfm";
		return;
	}
		
function checkNumber(tmpNum, part) {
	theNum = parseFloat(tmpNum);
	theString = theNum.toString();
	if (isNaN(theString))
		{
			return false;
		}
	for (x=0; x<(tmpNum.length); x++)
		{	
			y = tmpNum.charAt(x);
			if (y == '.')
				x++;
			y = parseInt(tmpNum.charAt(x));
			if (isNaN(y))
				return false;
		}		
	if (part == 0)
		{	
			if (tmpNum.length == 0)
				return false;
		}
	if (part == 1)
		{
			if (tmpNum.length != 5)
				return false;
		}
	if (part == 2)
		{
			if (tmpNum.length != 4)
				return false;
		}
	if (part == 3)
		{
			if (tmpNum.length != 3)
				return false;
		}
	if (part == 5)
		{
			if (tmpNum.length != 2)
				return false;
		}
	if (part == 100)
		{
			if (theNum > 100)
				return false;
		}
	return true;
}

function checkInteger(tmpNum, part) {
	theNum = parseFloat(tmpNum);
	theString = theNum.toString();
	if (isNaN(theString))
		{
			return false;
		}
	for (x=0; x<(tmpNum.length); x++)
		{	
			y = tmpNum.charAt(x);
			if (y == '.')
				return false;
		}		
	if (part == 0)
		{	
			if (tmpNum.length == 0)
				return false;
		}
	if (part == 1)
		{
			if (tmpNum.length != 5)
				return false;
		}
	if (part == 2)
		{
			if (tmpNum.length != 4)
				return false;
		}
	if (part == 3)
		{
			if (tmpNum.length != 3)
				return false;
		}
	if (part == 5)
		{
			if (tmpNum.length != 2)
				return false;
		}
	if (part == 100)
		{
			if (theNum > 100)
				return false;
		}
	return true;
}

function checkZip(tmpZip) {
	firstpart = tmpZip.substring(0,5);
	if ( (tmpZip.length != 10) && (tmpZip.length != 5) )
		{
		return false;
		}
	if (tmpZip.length == 5)
		{
			if (checkNumber(firstpart, 1))
				return true;
			else
				return false;
		}
	if (tmpZip.length == 10)
		secondpart = tmpZip.substring (6,10);
		separator = tmpZip.substring(5,6);	
		{
			if (separator != "-")
				return false;
			if (checkNumber(firstpart,1))
				{
					if (checkNumber(secondpart,2))
						return true;
					else
						return false;
				}
			else
				return false;
		}
	return true;
}

function checkDate(a)
{
     if (a.length != 10)
			return false;
        b = a.substring(0, 2)// month
        c = a.substring(2, 3)// '/'
        d = a.substring(3, 5)// day
        e = a.substring(5, 6)// '/'
        f = a.substring(6, 10)// year
        //basic error checking
        if (b<1 || b>12)
			return false;
        if (c != '/')
			return false;
        if (d<1 || d>31)
			return false;
        if (e != '/')
			return false;
        
        //advanced error checking

        // months with 30 days
        if (b==4 || b==6 || b==9 || b==11)
			{
               	 if (d==31)
					return false;
	        }

        // february, leap year
        if (b==2)
	{
                // feb
                var g=parseInt(f/4, 10)
                if (isNaN(g)) 
					{
                        err=1
                	}

                if (d>29)
					return false;
                if (d==29 && ((f/4)!=parseInt(f/4, 10)))
					return false;
	}
	return true;
}

function checkRadio(theList) 
	{
		for (x=0; x<theList.length; x++)
			{
				if (theList[x].checked)
					return true;
			}
		return false;
	}
	
function checkSeparatedDate(mo,da,ye)
	{
	    if (mo.length == 1)
		 	mo = "0" + mo;
		if (da.length == 1)
		 	da = "0" + da;
		if (mo.length != 2)
			return false;
		if (da.length != 2)
			return false;
		if (ye.length != 4)
			return false;
	    //basic error checking
	        if (mo<1 || mo>12)
				return false;
	        if (da<1 || da>31)
				return false;
	        //advanced error checking
	
	        // months with 30 days
	        if (mo==4 || mo==6 || mo==9 || mo==11)
				{
	               	 if (da==31)
						return false;
		        }
	
	        // february, leap year
	        if (mo==2)
				{
	                // feb
	                var g=parseInt(ye/4, 10)
	                if (isNaN(g)) 
						{
	                        err=1
	                	}
	
	                if (da>29)
						return false;
	                if (da==29 && ((ye/4)!=parseInt(ye/4, 10)))
						return false;
				}
		return true;
	}	

// This function is changed for this program, as to include foreign email addresses
function checkEmail(emailaddy)
	{
			if (emailaddy == "")
				{
					return false;
				}
			if (emailaddy.indexOf("@") < 2)
				{
					return false;
				}
			if (emailaddy.indexOf(".") <= 5)
				{
					return false;
				}
			// if ( (emailaddy.indexOf(".com") < 5) && (emailaddy.indexOf(".org") < 5) && (emailaddy.indexOf(".gov") < 5) && (emailaddy.indexOf(".net") < 5) && (emailaddy.indexOf(".mil") < 5) && (emailaddy.indexOf(".edu") <5) )
			//	{
			//		return false;
			//	}
			return true;
	}	