//this function strips out the characters
function stripChar(character, sVal)
{
	var myarray = sVal.split(character);
	var x;
	var anothervar = "";
	
	for (x=0;x < myarray.length; x++)
	{
		anothervar = anothervar + myarray[x];
	}
	return anothervar;
}

//this function strips non-numeric values out
function stripNonNumeric(sVal)
{
	for (i=0; i<sVal.length; i++)
	{
		if (sVal.substring(i, i+1) < "0" || sVal.substring(i, i+1) > "9")
		{
			sVal = stripChar(sVal.substring(i, i+1), sVal);
		}			
	}
	
	return sVal;
}

//this function will make sure there is an "@" in the email address
function isValidEmail(strVal)
{
	strVal = strVal.toUpperCase();
	var uLen = strVal.length;

	for(var i=0; i!=uLen; i++)
	{
		if (strVal.substring(i,i+1) < "A" || strVal.substring(i,i+1) > "Z")
		{
			if ((strVal.substring(i,i+1) != "-")
			&& (strVal.substring(i,i+1) != ".")
			&& (strVal.substring(i,i+1) != "@")
			&& (strVal.substring(i,i+1) != "_")
			&& (strVal.substring(i,i+1) != "0")
			&& (strVal.substring(i,i+1) != "1")
			&& (strVal.substring(i,i+1) != "2")
			&& (strVal.substring(i,i+1) != "3")
			&& (strVal.substring(i,i+1) != "4")
			&& (strVal.substring(i,i+1) != "5")
			&& (strVal.substring(i,i+1) != "6")
			&& (strVal.substring(i,i+1) != "7")
			&& (strVal.substring(i,i+1) != "8")
			&& (strVal.substring(i,i+1) != "9"))
			{
				return false;
			}
		}
	}
	if (strVal.indexOf("@") == -1 || strVal.indexOf(".") == -1)
	{
		return false;
	}	else	{
		return true;
	}
}

//this function will make sure that values are alpha-numeric for passwords
function isValidPassword(strVal)
{	
	strVal = strVal.toUpperCase();
	var uLen = strVal.length;
	
	for(var i=0; i!=uLen; i++)
	{
		if (strVal.substring(i,i+1) < "A" || strVal.substring(i,i+1) > "Z")
		{			
			if(strVal.substring(i,i+1) < "0" || strVal.substring(i,i+1) > "9")
			{
				return false;
			}	
		}
	}
	return true;
}

//this function will make sure that values are alpha-numeric or underscore (_) for user name
function isValidUser(strVal)
{	
	strVal = strVal.toUpperCase();
	var uLen = strVal.length;
	
	for(var i=0; i!=uLen; i++)
	{
		if (strVal.substring(i,i+1) < "A" || strVal.substring(i,i+1) > "Z")
		{			
			if(strVal.substring(i,i+1) < "0" || strVal.substring(i,i+1) > "9")
			{
				if(strVal.substring(i,i+1) != "_")
				{
					return false;
				}
			}	
		}
	}
	return true;
}

//this function will make sure that values are alphabetic
//or are "-", "'", or "`", or "."
function isValidChar(strVal)
{	
	strVal = strVal.toUpperCase();
	var uLen = strVal.length;
	
	for(var i=0; i!=uLen; i++)
	{
		if (strVal.substring(i,i+1) < "A" || strVal.substring(i,i+1) > "Z")
		{
			if ((strVal.substring(i,i+1) != "-") && (strVal.substring(i,i+1) != "'") && (strVal.substring(i,i+1) != "`") && (strVal.substring(i,i+1) != ".") && (strVal.substring(i,i+1) != " "))
			{
				return false;
			}	
		}
	}
	return true;
}

//this function checks to make sure at least one of the values in a multi select box is selected
//pass in the multi select object name as the first parameter
//pass in the number of option as the second value since if there is only one option
//the object behaves differently
//if you don't know the number of options, just pass in a value greater than one
//because most option boxes have a value greater than one
function CheckMultiSelect(oMultiSelect, iNumOptions)
{
	if (iNumOptions == 1)
	{
		if (oMultiSelect.selected)
		{
			return true;
		}
	}
	else
	{
		for(i=0;i<oMultiSelect.length;i++)
		{
			if (oMultiSelect[i].selected)
			{
				return true;
			}
		}
	}

	//if we get here then none of them have been selected
	return false;
}

//this function checks to make sure at least one of the radion button in a list is checked
//pass in the radio button object name as the first parameter
//pass in the number of radio buttons as the second value since if there is only one radio button
//the object behaves differently
//if you don't know the number of radio buttons, just pass in a value greater than one
function CheckRadioButton(oRadioButton, iNumButtons)
{
	if (iNumButtons == 1)
	{
		if (oRadioButton.checked)
		{
			return true;
		}
	}
	else
	{
		for(i=0;i<oRadioButton.length;i++)
		{
			if (oRadioButton[i].checked)
			{
				return true;
			}
		}
	}

	//if we get here then none of them have been selected
	return false;
}

//check credit card function
//pass in the card number as the first parameter
//pass in the card type as the second parameter (either "Visa", "AMEX", or "MasterCard")
//function returns 0 if it is a valid number for this card type
//function returns 1 if it is NOT a valid number for this card type
//function returns 6 if it looks like this is a discover card
function verifycc(sCardNumber, sCardType)
{

    weight = 2;
    sum = 0;

    for(count = sCardNumber.length-2; count>=0; count=count-1)
    {
        digit = weight*sCardNumber.charAt(count);
		sum=sum+(Math.floor(digit/10))+(digit%10);
		
		if (weight==2)
		{
			weight=1;
		}
		else
		{
			weight = 2;
		}
    }

    mod = (10-sum%10)%10;

    if (mod!=sCardNumber.charAt(sCardNumber.length-1))
    {
        return(1);
    }

	//  Check if this is possibly a Discover card number
	var numCC = sCardNumber

	if (numCC.substring(0, 4) == "6011")
	{
   		return(6);
	}

    if (sCardType == "Visa")
    {
        if ((sCardNumber.length==13) ||
			(sCardNumber.length==16) && (numCC.substring(0, 1) == "4"))
		{
			return(0);
		}
		else
		{
			return(1);
		}
    }
    else if (sCardType == "MasterCard")
    {
		if (sCardNumber.length==16 && (numCC.substring(0, 2) >= "51" && numCC.substring(0, 2) <= "55"))
		{
			return(0);
		}
		else
		{
			return(1);
		}
    }
    else if (sCardType == "AMEX")
    {
		if (sCardNumber.length==15 && (numCC.substring(0, 2) == "34" || numCC.substring(0, 2) == "37"))
		{
			return(0);
		}
		else
		{
			return(1);
		}
    }
    
    //if we got here return 1
    return(1);
}

function isBlank(strVal)
{
	var retnStr;

    
	retnStr = rTrim(lTrim(strVal));

	if (retnStr.length > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function lTrim(LstrVal)
{
	while(LstrVal.charAt(0)==" ")
	LstrVal=LstrVal.substring(1,LstrVal.length)
	return LstrVal;
}

function rTrim(RstrVal)
{
	while(RstrVal.charAt(RstrVal.length-1)==" ")
	RstrVal=RstrVal.substring(0,RstrVal.length-1)
	return RstrVal;
}

function allTrim(sVal)
{
	sVal = rTrim(lTrim(sVal));
	return sVal;
}

function IsValidDate( idx, msg ){

	var sMsg;
	sMsg = "Invalid Date  - " + msg ;
		
	//var txtValue = allTrim(idx.value);
	var txtValue = idx.value;

	if ( txtValue.length == 0 )  {
		return true; 
		}
		
	var bOK = true;

	var lexp = "^(\\d{1,2})(\\/|\\-)(\\d{1,2})(\\/|\\-)(\\d{2}|\\d{4})$";
	var Reg = new RegExp( lexp );
	var bOK = Reg.test( txtValue);
	if ( ! bOK ) {
		sMsg = sMsg + "\n - Format: mm/dd/yy, mm/dd/yyyy, mm-dd-yy, mm-dd-yyyy"
		}
	
	if (bOK) {
		var aParts = txtValue.match(Reg) ;
	
		var nMon = Math.abs(aParts [1]) // month
		var sSep1 = aParts [2] // '/' or '-'
		var nDay = Math.abs(aParts [3]) // day
		var sSep2 = aParts [4] // '/' or '-'
		var nYear = Math.abs(aParts[5]) // year

		//basic error checking
		if (nMon < 1 || nMon > 12) {
			bOK = false;
			sMsg = sMsg + "\n - Month range: 1 to 12" ;
			}
	
		if (sSep1 != '/' && sSep1 != '-' ) { ;
			bOK = false;
			sMsg = sMsg + "\n - Separators: / or -" ;
			}

		if ( nDay < 1 || nDay > 31) { ;
			bOK = false;
			alert (nDay);
			sMsg = sMsg + "\n - Day range: 1 to 31" ;
			}

	
		if (sSep2 != '/' && sSep2 != '-' ) { ;
			bOK = false;
			sMsg = sMsg + "\n - Separators: / or -" ;
			}

	
	
		//advanced error checking

		// months with 30 days
		if ( nMon == 4 || nMon == 6 || nMon == 9 || nMon == 11){
			if ( nDay == 31) {
				bOK = false;
				sMsg = sMsg + "\n - 31 days not allowed in that month" ;
				} 
		}

		// february, leap year
		if ( nMon == 2){
			// feb
			var num = parseInt(nYear/4);
			if (isNaN(num)) {
				bOK = false;
				sMsg = sMsg + "\n - Invalid year for leap year check" ;
				}

			if ( nDay > 29) {
				bOK = false;
				sMsg = sMsg + "\n - Invalid number of days for month" ;
				}
			
			if ( nDay == 29 && (( nYear / 4) != parseInt( nYear / 4 ))) {
				bOK = false;
				sMsg = sMsg + "\n - Invalid number of days for non-leap year" ;
				}
			
			}
		}
	if (! bOK ){

		//alert( sMsg );
		return false;
	}
	else {
		idx.value = txtValue;
		return true;
	}
}

//display calendar page.
function ShowCalendar(idx) 
{
	Datefld = idx;
	gDatefld = Datefld
	window.open("/IssueLog/Calendar/Calendar.aspx", "Calendar", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=250,height=225");
}
// put selected date from calendar in text field
function SetDate(idx)
{
	gDatefld.value = idx;
}