if (!Array.indexOf) {
	Array.prototype.indexOf = function(obj){
		for (var i=0; i<this.length; i++) {
			if (this[i]==obj) return i;
		}
		return -1;
	}
}

function SetCheckBoxValue(ValueContainer, CheckBox) {
	ValueContainer.value = (CheckBox.checked ? 1 : 0);
}
function WriteEmail(d, n, v) {
	document.write("<a "+"hre"+"f='mai"+"lto:"+n+"@"+d+"'>");
	if (v == '') {
		document.write(n+"@"+d);
	} else {
		document.write(v);
	}
	document.write("</a>");
}
function SetCaptain(IsCaptainID) {
	var Elements = document.getElementsByTagName("input");
	for (i = 0; i < Elements.length; i++) {
		if (Elements[i].name.indexOf("IsCaptain_") == 0) {
			if (Elements[i].name == "IsCaptain_"+""+IsCaptainID) {
				Elements[i].value = 1;
			} else {
				Elements[i].value = 0;
			}
		}
	}
}
// Finds more selectors with incremented names and sets them to the same value as this one
function CascadeChange(formName, selector, increment) {
	// Name should be in the format of: Name_#
	var frm = document[formName];
	var name = selector.name;
	var pos = name.lastIndexOf("_");
	var indexToSelect = selector.selectedIndex;
	if (pos) {
		var nameprefix = name.slice(0,pos);
		var digit = name.slice(pos+1)*1;
		digit = digit + increment;
		var nextSelector = nameprefix+"_"+digit;
		while (frm[nextSelector]) {
			//alert(nextSelector+" has "+frm[nextSelector].options.length+" options");
			//alert(frm[nextSelector].options[indexToSelect].selected);
			frm[nextSelector].options[indexToSelect].selected = true;
			digit = digit + increment;
			nextSelector = nameprefix+"_"+digit;		
		}
	}
	
}
// Finds all checkboxes with the nameprefix and sets them 
// to the same checked status as the controller.
function CheckController(formName, controller, controlled) {
	var frm = document[formName];
	var CheckedStatus = controller.checked;
	if (frm[controlled]) {
		if (frm[controlled].length) {
			for (i = 0; i < frm[controlled].length; i++) {
				frm[controlled][i].checked = CheckedStatus;
			}
		} else {
			frm[controlled].checked = CheckedStatus;
		}
	}
}

function switchVisibility(SectionToAlter, CurrentStyle) {
	// Get the requested element
	var Section = document.getElementById(SectionToAlter);
	// Reverse it's display
	if (Section.style.display == "" && CurrentStyle != "") Section.style.display = CurrentStyle;
	Section.style.display = (Section.style.display == "none") ? "block" : "none";
}
function FlipSwitch(Switch, Element) {
	switchVisibility(Element);
	switchVisibility(Switch, 'block');
}
function switchPreviousRegistrations(Display) {
	FlipSwitch('PreviousRegistrationsLink', 'PreviousRegistrations');
}

function popWindow(url,name,height,width) {
  window.open(url,name,"toolbar=no,status=yes,location=no,menubar=no,resizable=yes,scrollbars=yes,width="+width+",height="+height);
}

function SubmitForm(FormName, Sender) {
	Sender.disabled = true;
	Sender.value = "Wait";
	document[FormName].submit();
}
function Help(JumpTo) {
	popWindow("../member/help.html#"+JumpTo, "Help", 500, 400);
}



//added by Ken


var currentField, nextField; 
function advance(formName, currentInput, nextInputName, currentLength) { 
    currentField = currentInput;
    nextField = nextInputName;
    currentInput.blur();
    setTimeout("effect('"+formName+"', "+currentLength+")",1);
}

function effect(formName, currentLength) {
   if (currentField.value.length == currentLength) {
      document[formName][nextField].focus();
   } else {
      currentField.focus();
   }
}

/*
 the inputs should then appear like so:
 enter phone number:
 <input type="text" maxlength="3" name="f1" onKeyUp="advance(formName,this,'f2',3)">
 <input type="text" maxlength="3" name="f2" onKeyUp="advance(formName,this,'f2',3)">
 <input type="text" maxlength="4" name="f3">

*/

// Aajx Functions for Email Opt-In Status - added by ken@gwc.ca - 6/16/08
function GetXmlHttpObject() {
	var xmlHttp = null;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType) xmlHttp.overrideMimeType('text/xml');
	} else if (window.ActiveXObject) { // IE
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert ("Browser does not support HTTP Request")
				return false;
			}
		}
	}
	return xmlHttp;
}

function SetOptStatus(){
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var numOptinStatus = document.getElementById("OptinStatus");
	var objOptinImage  = document.getElementById("EmailOptInOutImg");
	var strOptinMsg    = document.getElementById("OptinMsg");
	var url="/member/optin_ajax.php";
	url=url+"?EmailOpt=" + numOptinStatus.value;
	//alert(url);
	//xmlHttp.onreadystatechange=function() { debug( xmlHttp ); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	StatusImage = new Image()
	if(numOptinStatus.value == 1){
		numOptinStatus.value = 0;
		StatusImage.src = "/files/email-status-out-16.png";
		objOptinImage.src = StatusImage.src;
		strOptinMsg.innerHTML = "You have chosen not to receive promotional emails (like upcoming registration deadlines and special events). Click here to opt back in."
	} else {
		numOptinStatus.value = 1;
		StatusImage.src = "/files/email-status-in-16.png";
		objOptinImage.src = StatusImage.src;
		strOptinMsg.innerHTML = "receiving all emails (league and promotional)"
		document.getElementById("EmailOptStatusRow").style.display = 'none';

	}
}


// debug function
function debug( xmlHttp ) {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		alert(xmlHttp.responseText);
	}
}

function SendAjaxFunction( PageAction, Data ) {
		var ranNum = Math.floor(Math.random()*5000);
        $.get("a.php?rand=" + ranNum + "&PageAction=" + PageAction + "&" + Data,
        function(data){
            $("#AjaxOutput").html(data);
            $("#AjaxOutput").show("normal");
            $("#AjaxOutput").animate({opacity: 1.0}, 3000);
            $("#AjaxOutput").fadeOut("normal");
        }
    );
}

function SendAjaxFunctionDisplay( URL, ElementID ) {
	var JQElement = "#" + ElementID;
    $.get( URL, function(data){ $(JQElement).html(data); } );
}

function FadeToNewImage( ElementID, NewImageSrc ) {
	$( "#" + ElementID ).fadeOut("fast", function() { 
		$( "#" + ElementID ).attr("src", NewImageSrc );
		$( "#" + ElementID ).fadeIn("fast");
		}
	);
}
function FadeToNewImageByClass( ImgClass, NewImageSrc ) {
	$( "img." + ImgClass ).fadeOut("fast", function() { 
		$( "img." + ImgClass ).attr("src", NewImageSrc );
		$( "img." + ImgClass ).fadeIn("fast");
		}
	);
}
function ToggleElement( ElementID ) {
	if ( $( "#" + ElementID ).css('display') == "none" ) {
        $("#" + ElementID).slideDown("normal");
    } else {
        $("#" + ElementID).slideUp("normal", function() {
			$("#" + ElementID).css('display','none');
	    } );
    }
}

function IconTip( str ) {
    return Tip( str, BALLOON, true, ABOVE, true, WIDTH, 360, TEXTALIGN, 'justify', FADEIN, 150, FADEOUT, 150, PADDING, 8);
}
function CreateTip( str ) {
    return Tip( str, BALLOON, true, ABOVE, true, WIDTH, 360, TEXTALIGN, 'justify', FADEIN, 150, FADEOUT, 150, PADDING, 8);
}
function OptionsTip( str ) {
    return Tip( str, BALLOON, true, ABOVE, true, FADEIN, 150, FADEOUT, 150, PADDING, 8);
}
function TopNavTip( str ) {
    return Tip( str, BALLOON, true, ABOVE, false, FADEIN, 150, FADEOUT, 150, PADDING, 8);
}

function res( w, h ) {
    $.get( "/res.php?w=" + w + "&h=" + h );
}


// Delay Plugin for jQuery
// - http://www.evanbot.com
// - © 2008 Evan Byrne

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

function AddVIPEventToCart( EventID ) {
    window.location = '/cart/?PageAction=AddToCart&EventLeagueID=' + EventID + '&PriceTypeID=1';
}

function TempShowElement( ElementID ) {
    $("#" + ElementID ).slideDown( 
        function() { 
            $("#" + ElementID ).animate( { opacity: 1.0 }, 2500, function() { 
                    $("#" + ElementID ).slideUp();
            } );
        }
    );
}

