//Allows only digits to be entered into the form field
function digitsOnly(theTarget,event) {
    //TODO: add code for Netscape
	if (event.keyCode == 13) return;
	if (!((event.keyCode >= 48) && (event.keyCode <= 57)))
		event.keyCode = null;
}

//Allows only letters to be entered into the form field
function lettersOnly(theTarget,event) {
	if (!(((event.keyCode >= 97) && (event.keyCode <= 122)) || 
				((event.keyCode >= 65) && (event.keyCode <= 90))))
		event.keyCode = null;
}

//Allows only digits, "$", ",", and "." to be entered into the form field
function currencyOnly(theTarget,event) {
	if (!(((event.keyCode >= 48) && (event.keyCode <= 57)) || 
				(event.keyCode == 46) || (event.keyCode == 36) || (event.keyCode == 44)))
		event.keyCode = null;
}

//Allows only digits and "." to be entered into the form field
function decimalOnly(theTarget,event) {
	if (!(((event.keyCode >= 48) && (event.keyCode <= 57)) || (event.keyCode == 46)))
		event.keyCode = null;
}

function alphaNumericOnly(theTarget,event) {
	if (event.keyCode == 13) return;
	if (!(((event.keyCode >= 97) && (event.keyCode <= 122)) || (event.keyCode <= 95) ||
				((event.keyCode >= 65) && (event.keyCode <= 90)) ||
				((event.keyCode >= 48) && (event.keyCode <= 57))))
		event.keyCode = null;
}

//Allows only alpha and digits to be entered into the form field
function alphaNumericOnly(theTarget,event) {
	if (event.keyCode == 13) return;
	if (event.keyCode == 32) event.keyCode = null;
	if (!(((event.keyCode >= 97) && (event.keyCode <= 122)) || (event.keyCode <= 95) ||
				((event.keyCode >= 65) && (event.keyCode <= 90)) ||
				((event.keyCode >= 48) && (event.keyCode <= 57))))
		event.keyCode = null;
}

//Allows only digits and "," to be entered into the form field
function digitsWithCommasOnly(theTarget,event) {
	if (event.keyCode == 13) return;
	if (event.keyCode != 44) 
		if (!((event.keyCode >= 48) && (event.keyCode <= 57)))
			event.keyCode = null;
}

//Allows no digits to be entered into the form field
function noDigitsAllowed(theTarget,event) {
	if (event.keyCode == 13) return;
	if ((event.keyCode >= 48) && (event.keyCode <= 57))
		event.keyCode = null;
}

function trimString(str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

//Validation Function for Business License Modificatoin/Renewal
function ValidateBusinessAddress(source, arguments)
{
    arguments.IsValid = true;
    
	var value = arguments.Value.toUpperCase();
	if (value.indexOf('PO BOX')>= 0 || value.indexOf('POBOX')>= 0
	    || value.indexOf('PO-BOX')>= 0 || value.indexOf('PO  BOX')>= 0
	    )
	    arguments.IsValid = false;
}

//Validation Function for Business License Renewal
//to validate the Amount Collected Duration must follow:
//1. From Year/Month and To Year/Month must be selected
//2. To Year/Month must be 12 months greater then the From Year/Month
function ValidateToDateForAmountCollected(source, arguments)
{
    var idFromYear = source.id.replace("cusvalAmtCol","CtlYearFrom_ddlYear");
    var idFromMonth = source.id.replace("cusvalAmtCol","CtlMonthFrom_ddlMonth");
    var idToYear = source.id.replace("cusvalAmtCol","CtlYearTo_ddlYear");
    var idToMonth = source.id.replace("cusvalAmtCol","CtlMonthTo_ddlMonth");
    
    var index_FromYear = document.getElementById(idFromYear).selectedIndex;
    if (index_FromYear < 0) index_FromYear = 0;
    var index_FromMonth = document.getElementById(idFromMonth).selectedIndex;
    if (index_FromMonth < 0) index_FromMonth = 0;

    var index_ToYear = document.getElementById(idToYear).selectedIndex;
    if (index_ToYear < 0) index_ToYear = 0;
    var index_ToMonth = document.getElementById(idToMonth).selectedIndex;
    if (index_ToMonth < 0) index_ToMonth = 0;

    var sFromYear = document.getElementById(idFromYear).options[index_FromYear].value;
    var sFromMonth = document.getElementById(idFromMonth).options[index_FromMonth].value;
    var sToYear = document.getElementById(idToYear).options[index_ToYear].value;
    var sToMonth = document.getElementById(idToMonth).options[index_ToMonth].value;

    if (sFromYear == "" && sFromMonth == "" && sToYear == "" && sToMonth == "")
    {
        arguments.IsValid = false;
        source.errormessage = "Duration for Amount Collected must be selected.";
        source.title = "Duration for Amount Collected must be selected";
    }
    else
    {
        if (Number(sToMonth)==Number(sFromMonth) && Number(sToYear) == Number(sFromYear)+1)
            arguments.IsValid = true;
        else
        {
            arguments.IsValid = false;
            source.errormessage = "The two Dates for Amount Collected must be twelve months apart.";
            source.title = "The two Dates for Amount Collected must be twelve months apart";
        }
    }
}

//Validation Function for Person License Application
function ValidatePersonLicenseQuestionADetail(source, arguments)
{
    var idPrefix = source.id.replace("cusvalQuesADetail","rdolistQues");
    if (document.getElementById(idPrefix+"A_0").checked)
    {
        var detail = document.getElementById(source.id.replace("cusvalQuesADetail","txtQuestionADetail")).value;
        if (trimString(detail) == "")
            arguments.IsValid = false;
    }   
}

function ValidatePersonLicenseQuestionBDetail(source, arguments)
{
    var idPrefix = source.id.replace("cusvalQuesBDetail","rdolistQues");
    if (document.getElementById(idPrefix+"B_0").checked)
    {
        var detail = document.getElementById(source.id.replace("cusvalQuesBDetail","txtQuestionBDetail")).value;
        if (trimString(detail) == "")
            arguments.IsValid = false;
    }   
}

function ValidatePersonLicenseQuestionCDetail(source, arguments)
{
    var idPrefix = source.id.replace("cusvalQuesCDetail","rdolistQues");
    if (document.getElementById(idPrefix+"C_0").checked)
    {
        var detail = document.getElementById(source.id.replace("cusvalQuesCDetail","txtQuestionCDetail")).value;
        if (trimString(detail) == "")
            arguments.IsValid = false;
    }   
}

function ValidatePersonLicenseQuestionDDetail(source, arguments)
{
    var idPrefix = source.id.replace("cusvalQuesDDetail","rdolistQues");
    if (document.getElementById(idPrefix+"D_0").checked)
    {
        var detail = document.getElementById(source.id.replace("cusvalQuesDDetail","txtQuestionDDetail")).value;
        if (trimString(detail) == "")
            arguments.IsValid = false;
    }   
}

function ValidatePersonLicenseQuestionEDetail(source, arguments)
{
    var idPrefix = source.id.replace("cusvalQuesEDetail","rdolistQues");
    if (document.getElementById(idPrefix+"E_0").checked)
    {
        var detail = document.getElementById(source.id.replace("cusvalQuesEDetail","txtQuestionEDetail")).value;
        if (trimString(detail) == "")
            arguments.IsValid = false;
    }   
}

function ValidateDateFromPersonLicTermination(source)
{
    return ValidateDateCommon(source);
}

//Validation Function for User Control CtlDateDropdown, to validate the selected date is a valid date
function ValidateDate(source, arguments)
{
    var bValid = ValidateDateCommon(source);
    arguments.IsValid = bValid;
}

function ValidateDateCommon(source)
{
    if (source.id.indexOf("btnOK") >= 0)
    {
        id_day = source.id.replace("btnOK","CtlDateDropdown1_ddlDay");
        id_month = source.id.replace("btnOK","CtlDateDropdown1_ddlMonth");
        id_year = source.id.replace("btnOK","CtlDateDropdown1_ddlYear");
    }
    else
    {
        id_day = source.id.replace("cusvalDate","ddlDay");
        id_month = source.id.replace("cusvalDate","ddlMonth");
        id_year = source.id.replace("cusvalDate","ddlYear");
    }
    
    if (document.getElementById(id_day).selectedIndex <= 0
        && document.getElementById(id_day).options[0].value == ""
        && document.getElementById(id_day).selectedIndex <= 0
        && document.getElementById(id_day).options[0].value == ""
        && document.getElementById(id_day).selectedIndex <= 0
        && document.getElementById(id_day).options[0].value == ""
        )
    {   ////Date (year, month & date) is all empty. 
        return true; //allow the date to be empty.
    }
    
    var index_day = document.getElementById(id_day).selectedIndex;
    if (index_day < 0) index_day = 0;
    var index_month = document.getElementById(id_month).selectedIndex;
    if (index_month < 0) index_month = 0;
    var index_year = document.getElementById(id_year).selectedIndex;
    if (index_year < 0) index_year = 0;

    var day   = Number(document.getElementById(id_day).options[index_day].value);
    var month = Number(document.getElementById(id_month).options[index_month].value);
    var year  = Number(document.getElementById(id_year).options[index_year].value);

    if (day == 0 || month == 0 || year == 0)
    {   //part of the date is unselected.
        return false;
    }
    
    if (day >= 31 && (month == 4 || month == 6 || month == 9 || month == 11))
        return false;
    else if (month == 2 && year%4 == 0 && day > 29)
        return false;
    else if (month == 2 && year%4 != 0 && day > 28)
        return false;
    else
    {
        var dt = new Date(year,month-1,day);
        if (month == dt.getMonth()+1 && day == dt.getDate())
            return true;
        else
            return false;
    }
}

//Validation Function for User Control CtlDateDropdown, to validate the date has been selected
function ValidateDate_Required(source, arguments)
{
    var id_day = source.id.replace("reqvalDate","ddlDay");
    var id_month = source.id.replace("reqvalDate","ddlMonth");
    var id_year = source.id.replace("reqvalDate","ddlYear");
    if (document.getElementById(id_day).selectedIndex <= 0
        && document.getElementById(id_day).options[0].value == ""
        && document.getElementById(id_day).selectedIndex <= 0
        && document.getElementById(id_day).options[0].value == ""
        && document.getElementById(id_day).selectedIndex <= 0
        && document.getElementById(id_day).options[0].value == ""
        )
        arguments.IsValid = false;
    else
        arguments.IsValid = true;
}

//Validation Function for User Control CtlPhoneNumber, to validate the Phone Number entered is valid
//-- the format must be 999 999 9999
function ValiatePhoneNumber(source, arguments)
{
    var id_areacode = source.id.replace("cusvalPhoneNumber","txtAreaCode");
    var id_part1 = source.id.replace("cusvalPhoneNumber","txtNumberPart1");
    var id_part2 = source.id.replace("cusvalPhoneNumber","txtNumberPart2");
    
    if (trimString(document.getElementById(id_areacode).value) == ""
        && trimString(document.getElementById(id_part1).value) == ""
        && trimString(document.getElementById(id_part2).value) == ""
        )
        arguments.IsValid = true; //allow the phone/fax number to be empty
    else
    {
        var rx999 = /[0-9]{3}/;
        var rx9999 = /[0-9]{4}/;
        if (rx999.test(trimString(document.getElementById(id_areacode).value))
            && rx999.test(trimString(document.getElementById(id_part1).value))
            && rx9999.test(trimString(document.getElementById(id_part2).value))
            )
            arguments.IsValid = true;
        else
            arguments.IsValid = false;
    }
}

//Validation Function for User Control CtlPhoneNumber, to validate the Phone Number entered is valid
//-- the format must be 999 999 9999
function ValiatePhoneNumber_Required(source, arguments)
{
    var id_areacode = source.id.replace("reqvalPhoneNumber","txtAreaCode");
    var id_part1 = source.id.replace("reqvalPhoneNumber","txtNumberPart1");
    var id_part2 = source.id.replace("reqvalPhoneNumber","txtNumberPart2");
    if (trimString(document.getElementById(id_areacode).value) == ""
        && trimString(document.getElementById(id_part1).value) == ""
        && trimString(document.getElementById(id_part2).value) == ""
        )
        arguments.IsValid = false;
    else
        arguments.IsValid = true;
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	return true;
}

