/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0) 
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // Garvin: deactivated onclick marking of the checkbox because it's also executed
            // when an action (like edit/delete) on a single item is performed. Then the checkbox
            // would get deactived, even though we need it activated. Maybe there is a way
            // to detect if the row was clicked, and not an item therein...
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function getDirections(addr, city, province, zip)
{
	href = "http://www.mapquest.com/directions/main.adp?src=maps&2a=";
	href = href + addr;
	href = href + "&2c=" + city;
	href = href + "&2s=" + province;
	href = href + "&2z=" + zip + "&2y=CA";

	document.location.href = href;
} 

function getMap(addr, city, province, zip)
{
	href = "http://www.mapquest.com/maps/map.adp?country=Canada&countryid=CA";
	href = href + "&searchtab=address&searchtype=address&";
	href = href + "address=" + addr + "&city=" + city + "&state=" + province;
	href = href + "&zipcode=" + zip + "&search=++Search++";

	document.location.href = href;
}

function validateEvent()
{
	frm = document.eventinfo;

	if(frm.OI007ENAME.value == "")
		alert("'Event name' is a required field");
	else
	if(frm.OI007TYPE.value == "")
		alert("'Event Type' is a required field");
	else
	if(frm.OI007LOC.value == "")
		alert("'Location' is a required field");
	else
	if(frm.OI007CITY.value == "")
		alert("'City' is a required field");
	else
	if(frm.OI007HOURS.value == "")
		alert("'Hours' is a required field");
	else
	if(frm.OI007CNAME.value == "")
		alert("'Name' is a required field");
	else
	if(frm.OI007ORG.value == "")
		alert("'Org' is a required field");
	else
	if(frm.OI007EMAIL.value == "")
		alert("'Email' is a required field");
	else
	if(frm.ECONF.value == "")
		alert("'Email Conf' is a required field");
	else
	if(frm.OI007EMAIL.value != frm.ECONF.value)
		alert("The email address and the confirmation email address must be the same");
	else
	if(frm.OI007DESC.value == "")
		alert("'Description' is a required field");
	else 
		frm.submit(); 
}

function zeroOut()
{
	frm = document.dirinfo;

	if(frm.OI002TEL2.value == "0")
		frm.OI002TEL2.value = "";

	if(frm.OI002FAX.value == "0")
		frm.OI002FAX.value = "";

	if(frm.OI002800.value == "0")
		frm.OI002800.value = "";

}

function validateDir()
{
	frm = document.dirinfo;

	if(frm.OI001NAME.value == "")
		alert("'Organization name' is a required field");
	else
	if(!validNbr(frm.OI002TEL1.value, frm.OI002TEL1, "mandatory"))
		alert("'Main Line' is required and must include the area code and the 7 digit phone number");
	else
	if(!validNbr(frm.OI002TEL2.value, frm.OI002TEL2, "optional"))
		alert("'Alternate Line' must include the area code and the 7 digit phone number");
	else
	if(!validNbr(frm.OI002FAX.value, frm.OI002FAX, "optional"))
		alert("'Fax' must include the area code and the 7 digit phone number");
	else
	if(!validNbr(frm.OI002800.value, frm.OI002800, "optional"))
		alert("'Toll Free' must include the area code and the 7 digit phone number");
	else
		frm.submit();
}

function validateOrganic()
{
	frm = document.dirinfo;

	if(frm.OI001NAME.value == "")
		alert("'Organization name' is a required field");
	else
	if(frm.OI002ADR1.value == "")
		alert("'Address line 1' is a required field");
	else
	if(frm.OI002CITY.value == "")
		alert("'City' is a required field");
	else
	if(frm.OI002TEL1.value == "")
		alert("'Main Line' is a required field");
	else
	if(!validNbr(frm.OI002TEL1.value, frm.OI002TEL1, "mandatory"))
		alert("'Main Line' is required and must include the area code and the 7 digit phone number");
	else
	if(!validNbr(frm.OI002TEL2.value, frm.OI002TEL2, "optional"))
		alert("'Alternate Line' must include the area code and the 7 digit phone number");
	else
	if(!validNbr(frm.OI002FAX.value, frm.OI002FAX, "optional"))
		alert("'Fax' must include the area code and the 7 digit phone number");
	else
	if(!validNbr(frm.OI002800.value, frm.OI002800, "optional"))
		alert("'Toll Free' must include the area code and the 7 digit phone number");
	else
		frm.submit();
}

function validateFestApp()
{
	frm = document.festapp;
	country = frm.OI009COUNTRY.options[frm.OI009COUNTRY.selectedIndex].text;
      province = frm.OI009PROV.options[frm.OI009PROV.selectedIndex].value; 

	if(frm.OI0091CHOICE.options[frm.OI0091CHOICE.selectedIndex].text == "")
	{
		alert("'1st Choice' is a required field");
		frm.OI0091CHOICE.focus();
	}
	else
	if(isNaN(frm.OI0091CHOICE.value))
	{
		alert("'1st Choice' must be a number");
		frm.OI0091CHOICE.focus();
	}
	else
	//if(frm.OI0092CHOICE.value != "" && isNaN(frm.OI0092CHOICE.value))
	//{
	//	alert("'2nd Choice' must be a number");
	//	frm.OI0091CHOICE.focus();
	//}
	//else
	//if(frm.OI0093CHOICE.value != "" && isNaN(frm.OI0093CHOICE.value))
	//{
	//	alert("'3rd Choice' must be a number");
	//	frm.OI0091CHOICE.focus();
	//}
	//else
	if(frm.OI009TENT.value != "" && isNaN(frm.OI009TENT.value))
	{
		alert("'Tent Rental' must be a number");
		frm.OI009TENT.focus();
	}
	else
	if(frm.OI009UMBRELLA.value != "" && isNaN(frm.OI009UMBRELLA.value))
	{
		alert("'Umbrella Rental' must be a number");
		frm.OI009UMBRELLA.focus();
	}
	else
	if(frm.OI009TABLE.value != "" && isNaN(frm.OI009TABLE.value))
	{
		alert("'Table Rental' must be a number");
		frm.OI009TABLE.focus();
	}
	else
	if(frm.OI009CHAIR.value != "" && isNaN(frm.OI009CHAIR.value))
	{
		alert("'Chair Rental' must be a number");
		frm.OI009CHAIR.focus();
	}
	else
	if(frm.OI009SERV.value == "")
	{
		alert("'Product/Service' is a required field");
		frm.OI009SERV.focus();
	}
	else
	if(frm.OI009CLNAME.value == "")
	{
		alert("'Business name' is a required field");
		frm.OI009CLNAME.focus();
	}
	else
	if(frm.OI009ADR1.value == "")
	{
		alert("'First line of Address' is a required field");
		frm.OI009ADR1.focus();
	}
	else
	if(frm.OI009CITY.value == "")
	{
		alert("'City' is a required field");
		frm.OI009CITY.focus();
	}
	else
	if((country == "Canada" || country == "United States of America") && province == "")
	{
		alert("'Province' is a required field");
		frm.OI009PROV.focus();
	}
	else
	if((country == "Canada" || country == "United States of America") && frm.OI009ZIP.value == "")
	{
		alert("'Postal Code' is a required field");
		frm.OI009ZIP.focus();
	}
	else
	if(!validNbr(frm.OI009TEL1.value, frm.OI009TEL1, "mandatory"))
	{
		alert("'Telephone' must be numeric");
		frm.OI009TEL1.focus();
	}
	else
	if(!validNbr(frm.OI009FAX.value, frm.OI009FAX, "optional"))
	{
		alert("'Fax' if entered must be numeric");
		frm.OI009TEL1.focus();
	}
	else
	if(frm.OI009EMAIL1.value == "")
	{
		alert("'Email' is a required field");
		frm.OI009EMAIL1.focus();
	}
	else
	if(frm.OI009FNAME1.value == "")
	{
		alert("'First Name' is a required field");
		frm.OI009FNAME1.focus();
	}
	else
	if(frm.OI009LNAME1.value == "")
	{
		alert("'Last Name' is a required field");
		frm.OI009LNAME1.focus();
	}
	else
	if(frm.OI009TITLE.value == "")
	{
		alert("'Title' is a required field");
		frm.OI009TITLE.focus();
	}
	else
	if(!validNbr(frm.OI009TEL2.value, frm.OI009TEL2, "mandatory"))
	{
		alert("'Telephone' must be numeric");
		frm.OI009TEL2.focus();
	}
	else
	if(!validNbr(frm.OI009MOBILE.value, frm.OI009MOBILE, "optional"))
	{
		alert("'Mobile' must be numeric");
		frm.OI009MOBILE.focus();
	}
	else
	if(frm.OI009FNAME2.value == "")
	{
		alert("'First Name' is a required field");
		frm.OI009FNAME2.focus();
	}
	else
	if(frm.OI009LNAME2.value == "")
	{
		alert("'Last Name' is a required field");
		frm.OI009LNAME2.focus();
	}
	else
		frm.submit();
}

function validateFestEdit()
{
	frm = document.festapp;

	if(frm.OI0091CHOICE.value =="")
	{
		alert("'1st Choice' is a required field");
		frm.OI0091CHOICE.focus();
	}
	else
	if(isNaN(frm.OI0091CHOICE.value))
	{
		alert("'1st Choice' must be a number");
		frm.OI0091CHOICE.focus();
	}
	else
	if(frm.OI0092CHOICE.value != "" && isNaN(frm.OI0092CHOICE.value))
	{
		alert("'2nd Choice' must be a number");
		frm.OI0092CHOICE.focus();
	}
	else
	if(frm.OI0093CHOICE.value != "" && isNaN(frm.OI0093CHOICE.value))
	{
		alert("'3rd Choice' must be a number");
		frm.OI0093CHOICE.focus();
	}
	else
	if(frm.OI009CLNAME.value == "")
	{
		alert("'Business name' is a required field");
		frm.OI009CLNAME.focus();
	}
	else
	if(frm.OI009ADR1.value == "")
	{
		alert("'First line of Address' is a required field");
		frm.OI009ADR1.focus();
	}
	else
	if(frm.OI009CITY.value == "")
	{
		alert("'City' is a required field");
		frm.OI009CITY.focus();
	}
	else
	if(frm.OI009PROV.options[frm.OI009PROV.selectedIndex].value == "")
	{
		alert("'Province' is a required field");
		frm.OI009PROV.focus();
	}
	else
	if(frm.OI009ZIP.value == "")
	{
		alert("'Postal Code' is a required field");
		frm.OI009ZIP.focus();
	}
	else
	if(!validNbr(frm.OI009TEL1.value, frm.OI009TEL1, "mandatory"))
	{
		alert("'Telephone' is required, and must be numeric");
		frm.OI009TEL1.focus();
	}
	else
	if(!validNbr(frm.OI009FAX.value, frm.OI009FAX, "optional"))
	{
		alert("'Fax' if entered must be numeric");
		frm.OI009TEL1.focus();
	}
	else
	if(frm.OI009EMAIL1.value == "")
	{
		alert("'Email' is a required field");
		frm.OI009EMAIL1.focus();
	}
	else
	if(frm.OI009SERV.value == "")
	{
		alert("'Product/Service' is a required field");
		frm.OI009SERV.focus();
	}
	else
	if(frm.OI009FNAME1.value == "")
	{
		alert("'First Name' is a required field");
		frm.OI009FNAME1.focus();
	}
	else
	if(frm.OI009LNAME1.value == "")
	{
		alert("'Last Name' is a required field");
		frm.OI009LNAME1.focus();
	}
	else
	if(frm.OI009TITLE.value == "")
	{
		alert("'Title' is a required field");
		frm.OI009TITLE.focus();
	}
	else
	if(!validNbr(frm.OI009TEL2.value, frm.OI009TEL2, "mandatory"))
	{
		alert("'Telephone' is required, and must be numeric");
		frm.OI009TEL2.focus();
	}
	else
	if(frm.OI009FNAME2.value == "")
	{
		alert("'First Name' is a required field");
		frm.OI009FNAME2.focus();
	}
	else
	if(frm.OI009LNAME2.value == "")
	{
		alert("'Last Name' is a required field");
		frm.OI009LNAME2.focus();
	}
	else
		frm.submit();
}

function desigSelected(desigRadio)
{
	return (desigRadio[0].checked || desigRadio[1].checked || desigRadio[2].checked || desigRadio[3].checked);
}

function boothSelected(boothRadio)
{
	return (boothRadio[0].checked || boothRadio[1].checked || boothRadio[2].checked || boothRadio[3].checked || boothRadio[4].checked || boothRadio[5].checked || boothRadio[6].checked || boothRadio[7].checked);
}

function validNbr(phoneNbr, field, membership)
{
	var nbrs = "";

	// No number entered
	if(phoneNbr.length == 0 && membership == "mandatory")
		return false;

	if(phoneNbr.length == 0 && membership == "optional")
		return true;

	for(x=0;x < phoneNbr.length; x++)
	{
		currNbr = phoneNbr.substring(x, x+1);
		if(currNbr >= '0' && currNbr <= '9')
			nbrs = nbrs + currNbr;
	}

	if(nbrs.length == 0)
		return false;
	else
    {
		field.value = nbrs;
		return true;
    }
}

function selectCountry(val)
{
    provinces = new Array("Alberta","British Columbia","Manitoba","New Brunswick","Newfoundland",
                       "Northwest Territories","Nova Scotia","Nunavut","Ontario","Prince Edward Island","Quebec","Saskatchewan","Yukon");
    provabbrev = new Array("AB","BC","MB","NB","NL","NT","NS","NU","ON","PE","QC","SK","YT");
    
    states = new Array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", 
                    "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", 
                    "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", 
                    "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", 
                    "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", 
                    "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", 
                    "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", 
                    "Wisconsin", "Wyoming"); 
    stateabbrev = new Array("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", 
                    "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", 
                    "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
                    "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"); 
    var x = 0;
    
	frm = document.festapp;
    provctl = frm.OI009PROV;
          
	if(frm.OI009COUNTRY.options[frm.OI009COUNTRY.selectedIndex].text == "United States of America")
    {
	    provctl.options.length = 0;      
        
        provctl.options[0] = new Option("", "", false, false);
        for(x=1;x < states.length; x++)
        {
            provctl.options[x] = new Option(states[x], stateabbrev[x], false, false);
        }
    }
    else
	if(frm.OI009COUNTRY.options[frm.OI009COUNTRY.selectedIndex].text == "Canada")
    {
	    provctl.options.length = 0;      
        
        provctl.options[0] = new Option("", "", false, false);
        for(x=1;x < provinces.length; x++)
        {
            provctl.options[x] = new Option(provinces[x], provabbrev[x], false, false);
        }
    }
    else
    {
	    provctl.options.length = 0;      
        
        provctl.options[0] = new Option("", "", false, false);
        provctl.options[1] = new Option("Leave this blank, type after city", "", false, false);
    }
}

function setTip(tip)
{
	tipImg = document.getElementById("tips");
	if(tip != "")
		tipImg.src = "images/" + tip;
	else
		tipImg.src = "images/spacer.gif";
}

function chgcol1(field, colour)
{
	field.parentNode.style.backgroundColor=colour;
}

function chgcol2(field, colour)
{
	field.style.backgroundColor=colour;
}


