﻿// Trim String
function Trim(inString) 
{
  inString = inString.replace( /^\s+/g, "" );	// strip leading
  return inString.replace( /\s+$/g, "" );		// strip trailing
}


// Invalid Date (mm/dd/yyyy)
function InvalidDate(month,day,year)
{
	// Check if length is 10 characters (mm/dd/yyyy)
	if(!(isInteger(month) && isInteger(day) && isInteger(year)))
		return true;
		
	if(month.length != 2)
		return true;
	if(day.length != 2)
		return true;
	if(year.length != 4)
		return true;
	
	// Check for invalid values
	if(month > 12 || day > 31 || month <= 0 || day <= 0 || year <= 0)
		return true;
	
	// If month is September, April, June or November, those months only have 30 days
	if(month == 9 || month == 4 || month == 6 || month == 11)
	{
		if(day == 31)
			return true;
	}
	// If month if February, check for leap year
	else if(month == 2)
	{
		if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
		{
			if(day > 29)
				return true;
		}
		else
		{
			if(day > 28)
				return true;
		}
	}
	
	return false;
}

// Invalid E-mail
function isValidEmail(email)
{
    var RegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    if(RegExp.test(email))
       return true;
    else
       return false;
}

// Invalid Zip code
function InvalidZip(zip)
{
	numberzip = Trim(zip).replace("-","");
	
	if(!isInteger(numberzip))
		return true;
		
	if(zip.indexOf("-") == -1)
	{
		if(Trim(zip).length != 5)
			return true;	
	}
	else
	{	
		var zipstart = Trim(zip).substring(0,zip.indexOf("-"));
		var zipend = Trim(zip).substring(zip.indexOf("-")+1,Trim(zip).length);
		
		if(zipstart.length != 5 || zipend.length != 4)
			return true;
	}

	return false;
}

// Invalid Phone No.
function InvalidPhone(phone)
{
	var fmtphone = Trim(phone).replace(/ /g,"");
	fmtphone = fmtphone.replace(/-/g,"");
	fmtphone = fmtphone.replace("(","");
	fmtphone = fmtphone.replace(")","");
	
	if(!isInteger(fmtphone))
		return true;

	// 9999999
	if(fmtphone.length == 7)
	{
		return true;
		/*if(phone.indexOf("-") != -1)
		{
			if(Trim(phone).charAt(3) != '-')
				return true;
			else if(!isInteger(Trim(phone).replace("-","")))
				return true;
		}	*/
	}
	// 1112223333
	else if(fmtphone.length == 10)
	{
		if(phone.indexOf("(") != -1)
		{
			if(Trim(phone).charAt(0) != '(' && Trim(phone).charAt(4) != ')')
				return true;
			if(Trim(phone).charAt(Trim(phone).length-5) != '-')
				return true;		
		}
		else if(phone.indexOf("-") != -1)
		{
			// (111) 222-3333 or 111-222-3333 or 111 222-3333
			if(!(Trim(phone).charAt(3) == '-' || Trim(phone).charAt(3) == ' ') ||
			 Trim(phone).charAt(Trim(phone).length-5) != '-')
				return true;	
		}
	}
	// 18001112222 
	else if(fmtphone.length == 11)
	{
		var nospace = phone.replace(/ /g,"");
		
		if(phone.indexOf("(") != -1)
		{
			if(nospace.charAt(1) != '(' && nospace.charAt(4) != ')')
				return true;
			else if(nospace.charAt(nospace.length-5) != '-')
				return true;		
		}
		else if(phone.indexOf("-") != -1)
		{		
			if(!(Trim(phone).charAt(1) == '-' || Trim(phone).charAt(1) == ' ') || 
		 	!(Trim(phone).charAt(5) == '-' || Trim(phone).charAt(5) == ' ') ||
			nospace.charAt(nospace.length-5) != '-')
				return true;
		}
	}
	else
		return true;

	return false;
}

// Check to see if string is all numeric
function isInteger(num)
{
	var numbers = "0123456789";
	var i = 0;
	
	for(i=0;i<num.length;i++)
	{
		if(numbers.indexOf(num.charAt(i)) == -1)
			return false;	
	}
	
	return true;
}

// Check to see if string is a float
function isFloat(num)
{
	var numbers = "0123456789.";
	var i = 0;
	
	for(i=0;i<num.length;i++)
	{
		if(numbers.indexOf(num.charAt(i)) == -1)
			return false;	
	}
	
	return true;
}


// Check to see if string is a valid US Dollar format
function isMoney(num)
{
	var numbers = "0123456789.";
	var i = 0;
	var j = 0;
	
	// Check to see if it's a float
	for(i=0;i<num.length;i++)
	{
		if(numbers.indexOf(num.charAt(i)) == -1)
			return false;	
	}
	
	// If the number has a decimal
	if(num.indexOf('.') != -1)
	{	
		// If decimal is not at least two places to the left (i.e. $100.00), it's invalid
		if(num.indexOf('.') > num.length-3)
			return false;
			
		// Count the number of decimals
		if(!validDecimals(num))
			return false;
	}
	
	return true;
}

function validDecimals(num)
{
	var count = 0;
	
	if(num.indexOf('.') != -1)
	{				
		// Count the number of decimals
		for(j=0;j<num.length;j++)
		{
			if(num.charAt(j) == '.')
				count++;
		}
		
		// If more than one decimal, it's invalid
		if(count > 1)
			return false;
	}
	
	return true;
}

function tabOver(tf, nf, len)
{
	var thisform = document.getElementById(tf);
	var nextform = document.getElementById(nf);
	
	if(Trim(thisform.value).length == len)
		nextform.focus();
}

function Capitalize(form)
{
	var textbox = document.getElementById(form);
	
	textbox.value = Trim(textbox.value);
	textbox.value = textbox.value.charAt(0).toUpperCase() + textbox.value.substring(1,textbox.value.length);
}

// Replace "&" with "%26" for URLs
function AmpReplace(inString) 
{
  inString = inString.replace( /&/g, "%26" );	// strip leading
  return inString.replace( /&/g, "%26" );		// strip trailing
}

// Only allow number input
function numbersOnly(e)
{
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event)
		keynum = e.keyCode;
	else if(e.which)
		keynum = e.which;
	
	if((keynum <= 57 && keynum != 32) || (keynum >= 96 && keynum <= 105))
		return true;
	else
		return false;
}


// Only allow float input
function floatsOnly(e)
{
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event)
		keynum = e.keyCode;
	else if(e.which)
		keynum = e.which;
	
	if((keynum <= 57 && keynum != 32) || (keynum >= 96 && keynum <= 105) || keynum == 190)
		return true;
	else
		return false;
}



function stripBad(string) {
    for (var i=0, output='', valid="eE-0123456789."; i<string.length; i++){
       if (valid.indexOf(string.charAt(i)) != -1){
          output += string.charAt(i)}
		}
	if (output=='') {output=0}
    return output;
}

function pad(n)
{
	if(n < 10)
	{
		return "0"+ n;
	}
	else
	{
		return n;
	}
}

// Only allow numbers and '/' input
function dateOnly(e)
{
	var keynum;
	var keychar;
	var numcheck;

	if(window.event)
		keynum = e.keyCode;
	else if(e.which)
		keynum = e.which;

	if((keynum <= 57 && keynum != 32) || (keynum >= 96 && keynum <= 105) || keynum == 191)
		return true;
	else
		return false;
}

function isValidURL(url)
{
       var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
       if(RegExp.test(url))
       {
           return true;
       }
       else
       {
           return false;
       }
}

function isValidDate(date)
{
    var RegExp = /^\d{2}(\/)\d{2}\1\d{4}$/;
    
    if(RegExp.test(date))
       return true;
    else
       return false;
}


function showhideCheck(chk, sp)
{
    var check = document.getElementById(chk);
    var span = document.getElementById(sp);

    if(check != null && span != null)
    {
        if(check.checked == true)
	        span.style.display = "inline";
        else
            span.style.display = "none";
    }
}

// Show the "required" span item.  Type 0 = textbox, type 1 = dropdown
function showRequired(valid, field, spErr, type)
{
	document.getElementById(spErr).style.display = "none"
	if(type == 0)
	{
		if(Trim(document.getElementById(field).value).length == 0)
		{
			document.getElementById(spErr).style.display = "inline"
			valid = false;
		}
	}
	else if(type == 1)
	{
		if(document.getElementById(field).selectedIndex == 0 || document.getElementById(field).selectedIndex == -1)
		{
			document.getElementById(spErr).style.display = "inline"
			valid = false;
		}
	}
	
	return valid;
}


// Return whether the two fields match
function showMatching(valid, field1, field2, spErr)
{
	document.getElementById(spErr).style.display = "none";
	
	if(Trim(document.getElementById(field1).value).length != 0 && Trim(document.getElementById(field2).value).length != 0)
	{
	    if(Trim(document.getElementById(field1).value) != Trim(document.getElementById(field2).value))
	    {
		    document.getElementById(spErr).style.display = "inline"
		    valid = false;
	    }
	}
	else
	    valid = false;
	
	return valid;
}


// Valid Phone No. with extension (3 fields, 2 spans)
function showPhoneNoExt(valid,phone1,phone2,phone3,ce,ex,spEmpty, spInv)
{	
    var p1 = Trim(document.getElementById(phone1).value);
    var p2 = Trim(document.getElementById(phone2).value);
    var p3 = Trim(document.getElementById(phone3).value);
    var chkExt = document.getElementById(ce).checked;
    var ext = Trim(document.getElementById(ex).value);
    
    document.getElementById(spEmpty).style.display = "none";
    document.getElementById(spInv).style.display = "none";
    
    if(p1.length == 0 && p2.length == 0 && p3.length == 0)
	{
	    document.getElementById(spEmpty).style.display = "inline";
		valid = false;
    }
    else if(p1.length != 3 || p2.length != 3 || p3.length != 4)
	{
	    document.getElementById(spInv).style.display = "inline";
		valid = false;
    }

	if(!isInteger(p1) || !isInteger(p2) || !isInteger(p3))
	{
	    document.getElementById(spInv).style.display = "inline";
		valid = false;
    }
    
    if(chkExt == true && ext.length != 3  && ext.length != 0)
    {
	    document.getElementById(spEmpty).style.display = "none";
	    document.getElementById(spInv).style.display = "inline";
		valid = false;    
    }
	
	return valid;
}

// Valid Phone No. (3 fields, 1 span)
function showInvPhoneNo(valid, phone1,phone2,phone3, spInv)
{	
    var p1 = Trim(document.getElementById(phone1).value);
    var p2 = Trim(document.getElementById(phone2).value);
    var p3 = Trim(document.getElementById(phone3).value);
    
    document.getElementById(spInv).style.display = "none";

    if((p1.length != 3 && p1.length > 0) || (p2.length != 3 && p3.length > 0) || (p3.length != 4 && p3.length > 0))
	{
	    document.getElementById(spInv).style.display = "inline";
		valid = false;
    }
	if(!isInteger(p1) || !isInteger(p2) || !isInteger(p3))
	{
	    document.getElementById(spInv).style.display = "inline";
		valid = false;
    }
	
	return valid;
}


// Valid Phone No. (3 fields, 2 spans)
function showPhoneNo(valid,phone1,phone2,phone3,spEmpty, spInv)
{	
    var p1 = Trim(document.getElementById(phone1).value);
    var p2 = Trim(document.getElementById(phone2).value);
    var p3 = Trim(document.getElementById(phone3).value);
    
    document.getElementById(spEmpty).style.display = "none";
    document.getElementById(spInv).style.display = "none";
    
    if(p1.length == 0 && p2.length == 0 && p3.length == 0)
	{
	    document.getElementById(spEmpty).style.display = "inline";
		valid = false;
    }
    else if(p1.length != 3 || p2.length != 3 || p3.length != 4)
	{
	    document.getElementById(spInv).style.display = "inline";
		valid = false;
    }

	if(!isInteger(p1) || !isInteger(p2) || !isInteger(p3))
	{
	    document.getElementById(spInv).style.display = "inline";
		valid = false;
    }
	
	return valid;
}

function clearEntry(e)
{
    var entry = document.getElementById(e);
    
    if(entry != null)
        entry.value = "";

}


// Prevents multi-line text boxes from typing over the max character limit
function maxLenKeypress(e,control,maxLength)
{
    value = control.value;
     if(maxLength && value.length > maxLength-1){
          e.returnValue = false;
          maxLength = parseInt(maxLength);
     }
}

// Added function to get this to work in Firefox
function maxLenUpdate(e,control,maxLength)
{
    value = control.value;
     if(maxLength && value.length > maxLength-1){
          control.value = control.value.substring(0, maxLength);
          e.returnValue = false;
     }
}

// Prevents pasting if over the max length
function maxLenBeforePaste(e,control,maxLength)
{
     if(maxLength)
     {
          e.returnValue = false;
     }
}

// Truncates a paste so it doesn't go over max length
function maxLenPaste(e,control,maxLength)
{
    value = control.value;
     if(maxLength){
          e.returnValue = false;
          maxLength = parseInt(maxLength);
          var oTR = control.document.selection.createRange();
          var iInsertLength = maxLength - value.length + oTR.text.length;
          var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
          oTR.text = sData;
     }
}


function maxlength(field, maxlen) 
{
    if (field.value.length > maxlen) 
    {
        field.value = field.value.substring(0, maxlen);
        
        return false;
    }
}