
// set up RSH so we can preserve back button functionality ////////////////////////////////////////////////

var do_rsh = false;

if (do_rsh) {
    window.dhtmlHistory.create({
        //debugMode : true, 
        // becuase we're using jquery we need to override json handling
        toJSON: function(o) { return $.toJSON(o); },
        fromJSON: function(s) { return $.evalJSON(s); }
    });
}

var historyChangeListener = function(newLocation, historyData) {
    // when history changes, use newLocation to either show a modal page close the current modal

    //alert(newLocation);
    if (newLocation == "") {
        $.nyroModalRemove();
    } else {
        doModalPage(newLocation + ".asp");
    }
}

var historyInit = function() {
    dhtmlHistory.initialize();
    dhtmlHistory.addListener(historyChangeListener);
}

if (do_rsh) addEvent(window, "load", historyInit);

// configure nyroModal ////////////////////////////////////////////////

var nyroModalInit = function() {
    $.nyroModalSettings({
        width: 711,
        height: 520,
        closeButton: "",
        autoSizable: false,
        resizable: false,
        endRemove: function() {
            // when a modal window is closed we need to update the history
            if (window["needs_refresh"]) {
                window["needs_refresh"] = false;
                var dt = new Date();
                window.location.href = "?" + dt.valueOf();
            } else {
                if (do_rsh) dhtmlHistory.add("#");
            }
        }
        //		,endResize : function() { }
        		,endShowContent : function() { checkHash(url); }
        //		,beforeHideContent : function() { }
        //		,endFillContent : function() { $.nyroModalSettings({width:533,height:500}); } // need this so that window doesn't resize on form post
		, endFillContent: function() { setWidth(url); checkPageRefresh(url); } // need this so that window doesn't resize on form post
    });
    //alert("autoSizable: " + $.fn.nyroModal.settings.autoSizable);
}

addEvent(window, "load", nyroModalInit);

// functions ////////////////////////////////////////////////

var nyroModal_hash = "";

function doModal(t) { // this function should be in the onclick event for all links on this site

    var lnk = t.href;
    lnk = lnk.replace("http://10.0.0.128:85/rcr2010/", "");
    lnk = lnk.replace("http://dev.egroupnet.com/rcr2010/", "");
    lnk = lnk.replace("http://www.egroupnet.com/rcr2010/", "");
    lnk = lnk.replace("http://staging.egroupnet.com/rcr2010/", "");
    lnk = lnk.replace("http://www.redcrossracing.com/", "");
    lnk = lnk.replace("http://redcrossracing.com/", "");
    lnk = lnk.replace("http://2010.redcrossracing.com/", "");
    if (do_rsh) dhtmlHistory.add(lnk.replace(".asp", ""));
    
    var winW = 0, winH = 0;

    winW = getWindowWidth();
    winH = getWindowHeight();
    
    //alert(winW + 'x' + winH);
    
    if (winW > 711 && winH > 520) {
        return doModalPage(lnk);
    } else {
        top.location.href = lnk;
    }
}

function getWindowHeight() {
    var windowHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function getWindowWidth() {
    var windowWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        windowWidth = window.innerWidth;
    }
    else {
        if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        }
        else {
            if (document.body && document.body.clientWidth) {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    return windowWidth;
}

function doModalForm(t) {
    alert(t.action);
    return false;
}

function doModalPage(page) {
    try {
        pageTracker._trackPageview(page);
    } catch (e) { }

	nyroModal_hash = "";
	if (page.indexOf("#") > 0) {
		nyroModal_hash = page.substring(page.indexOf("#"));
		page = page.substring(0,page.indexOf("#"));
	}

    var dt = new Date();
    page = page + ((page.indexOf("?") > 0) ? "&" : "?") + "modal=true&dt=" + dt.valueOf();
    $.nyroModalManual({ url: page });

    return false;
}

function do_google(page) {
    //alert(page + " : " + window.location.href);
}

function setWidth(page) {
    var w = 711;
    var h = 520;
    try {
		if (page.substring(0,3) == "fr/") {
			$("#nyroModalFull").addClass("fr_nyro");
		}
		
		page = page.substring(page.lastIndexOf("/") + 1, page.indexOf("?"));
        //alert(page);
        switch (page) {
            //case "my_account.asp" : w = 711; break; 
            //case "friends.asp" : w = 560; break; 
            //case "register.asp" : h = 700; break; 
            //case "schedule.asp" : h = 300; break; 
            //case "thanks.asp" : h = 300; break; 
            //case "financial_donation.asp" : h = 300; break; 
        }
    } catch (e) {

    }

    $.nyroModalSettings({ width: w, height: h });
    //$.nyroModalSettings({width:w});
	
}

function checkHash(page) {
	// look for any anchors in the url and scroll there if possible
	if (nyroModal_hash != "") {
		var top = $("#nyroModalContent a[name=" + nyroModal_hash.replace("#","") + "]").position().top - 30;
		$("#modal_content .scroll").animate({scrollTop:top},1000);
	}

	//if (page.indexOf("fr/") > 0) alert("hi");

}

function checkPageRefresh(page) {
    //alert(page);
    var needs_refresh = false;

    needs_refresh = (page.indexOf("thankyou_register.asp") >= 0) ? true : needs_refresh;
    needs_refresh = (page.indexOf("register.asp") >= 0) ? true : needs_refresh;

    window["needs_refresh"] = needs_refresh;

    if (page.indexOf("races.asp") > 0) replaceChecks();
}


// functions for pages

var trackInfo;
var product_to_show = "";
var process_jsonTrackInfo = function(json_text) {
    trackInfo = JSON.parse(json_text);
    showMainProduct();
}

function get_product_info(sku) {
    product_to_show = sku;
    if (typeof (trackInfo) != "object") {
        var dt = new Date();
        getAjaxResponse("ajax/get_product_info.asp?" + dt.valueOf(), process_jsonTrackInfo, true);
    } else {
        showMainProduct();
    }

    return false;
}

function showMainProduct() {
    var MainProduct = getElement("MainProduct").innerHTML;
    MainProduct = trackInfo.products[product_to_show].name + MainProduct.substring(MainProduct.indexOf("<"));
    getElement("MainProduct").innerHTML = MainProduct;

	getElement("Description").innerHTML = trackInfo.products[product_to_show].description;
    getElement("Image").src = "images/products/larger/" + product_to_show + ".jpg";

    var ciid = trackInfo.products[product_to_show].ciid;
    if (ciid == 0) {
        getElement("Points").innerHTML = "<a href=\"saf.asp" + "\" onclick=\"return doModal(this);\" class=\"\">Learn More</a>";
    } else {
        getElement("Points").innerHTML = trackInfo.products[product_to_show].points + " Points";
    }

    if (trackInfo.user_info.id != "") {
        var pts_needed = trackInfo.products[product_to_show].points - trackInfo.user_info.pts;
        if (ciid == 0) {
            getElement("Action").innerHTML = "<a href=\"saf_donate.asp" + "\" onclick=\"return doModal(this);\" class=\"btn btn_red\">Donate Now</a>";
        } else {
            if (pts_needed > 0) {
                getElement("Action").innerHTML = "POINTS NEEDED:<br /><span>" + pts_needed + "</span>";
            } else {
                getElement("Action").innerHTML = (product_to_show == "ticket") ? "" : "<a href=\"product_detail.asp?catid=539&ciid=" + ciid + "\" onclick=\"return doModal(this);\" class=\"btn btn_black\">Get Prize</a>";
            }
        }
    } else {
        getElement("Action").innerHTML = "<a href=\"register.asp\" onclick=\"return doModal(this);\" class=\"btn btn_black\">Register Now</a>";
    }

    if (product_to_show == "ticket") {
        getElement("Description").innerHTML += "<br /><br /><a href='drawing_rules.asp' onclick='return doModal(this);'>Click here for details</a>";
        getElement("Image").style.display = "none";
    } else {
        getElement("Image").style.display = "block";
    }

}

function createcrew() {
    var teamname = getElement("team_name");
    var user = getElement("userid1");
    if (user.value != '') {
        if (teamname.value != '') {
            var answer = confirm("Are you sure you want to create this new team?")
            if (answer) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            alert('You Must Enter a Team Name');
            return false;
        }
    }
    else {
        alert('You Must Be Logged in to Create a Team');
        return false;
    }
}

function joincrew() {
    var join_code = getElement("join_code");
    var user = getElement("userid2");
    if (user.value != '') {
        if (join_code.value != '') {
            return true;
        }
        else {
            alert('You Must Enter a Team Code');
            return false;
        }
    }
    else {
        alert('You Must Be Logged in to Join a Team');
        return false;
    }
}


function confirmation2() {
    var answer = confirm("Are you sure you want to leave this team? If you are the Team Owner, ownership will be passed to the first person whom responded to your invitation.")
    if (answer) {
        return true;
    }
    else {
        return false;
    }
}

function leave_team() {
    if (confirmation2()) {
        getElement('leave_form').submit();
    }
}

function notloggedin(action) {
    alert('You must be logged in to ' + action + ' a team.');
    return false;
}



function toggleRequestForm() {
    var selected_frm = "";
    getElement("frm_tickets").style.display = (getElement("tickets").checked) ? "" : "none";
    //getElement("frm_autographed_merchandise").style.display = (getElement("autographed_merchandise").checked) ? "" : "none";
    //getElement("frm_logo_approval").style.display = (getElement("logo_approval").checked) ? "" : "none";
    getElement("frm_appearance").style.display = (getElement("appearance").checked) ? "" : "none";
    getElement("frm_custom_merch").style.display = (getElement("custom_merch").checked) ? "" : "none";
    //getElement("request_form_container").style.display = (getElement("tickets").checked | getElement("autographed_merchandise").checked | getElement("logo_approval").checked | getElement("appearance").checked) ? "" : "none";
    //getElement("request_form_container").style.display = (getElement("tickets").checked | getElement("autographed_merchandise").checked | getElement("appearance").checked | getElement("custom_merch").checked) ? "" : "none";
    getElement("request_form_container").style.display = (getElement("tickets").checked | getElement("appearance").checked | getElement("custom_merch").checked) ? "" : "none";
    /*var err = getElement("err").innerHTML;
    if (err.length > 0){
    getElement("err").innerHTML = ""
    }*/
}

// used on special/charlotte/index.asp
function switchphoto(offset, mx) {
    var currphoto = parseInt(getElement("photonum").innerHTML);
    var newphoto = currphoto + offset
    if (newphoto < 1) newphoto = 1;
    if (newphoto > mx) newphoto = mx;

    if (currphoto != newphoto) {
        getElement("photonum").innerHTML = newphoto
        getElement("photo" + currphoto).style.display = "none";
        getElement("photo" + newphoto).style.display = "block";
    }
    return false;
}

var teamTab = 'msgbd';
var dvTab = '1';

function showTeamMsgBd() {
    if (dvTab == '1') {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_msgbd_dv1.gif) top right no-repeat', 'padding': '0' });
    } else {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_msgbd_dv2.gif) top right no-repeat', 'padding': '0' });
    }
    $('#tb_msgbd').css({ 'color': 'gray' });
    $('#tb_msgbd').css({ 'font-weight': 'bold' });
    $('#tb_rstr').css({ 'color': 'red' });
    $('#tb_rstr').css({ 'font-weight': 'normal' });
    $('#t_msgbd').show();
    $('#t_rstr').hide();
    teamTab = 'msgbd';
    return false;
}

function showTeamRstr() {
    if (dvTab == '1') {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_rstr_dv1.gif) top right no-repeat', 'padding': '0' });
    } else {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_rstr_dv2.gif) top right no-repeat', 'padding': '0' });
    }
    $('#tb_msgbd').css({ 'color': 'red' });
    $('#tb_msgbd').css({ 'font-weight': 'normal' });
    $('#tb_rstr').css({ 'color': 'gray' });
    $('#tb_rstr').css({ 'font-weight': 'bold' });
    $('#t_msgbd').hide();
    $('#t_rstr').show();
    teamTab = 'rstr';
    return false;
}

function showTeamDv1() {
    if (teamTab == 'msgbd') {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_msgbd_dv1.gif) top right no-repeat', 'padding': '0' });
    } else {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_rstr_dv1.gif) top right no-repeat', 'padding': '0' });
    }
    $('#tb_dv1').css({ 'color': 'gray' });
    $('#tb_dv1').css({ 'font-weight': 'bold' });
    $('#tb_dv2').css({ 'color': 'red' });
    $('#tb_dv2').css({ 'font-weight': 'normal' });
    $('#t_dv1').show();
    $('#t_dv2').hide();
    dvTab = '1';
    return false;
}

function showTeamDv2() {
    if (teamTab == 'msgbd') {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_msgbd_dv2.gif) top right no-repeat', 'padding': '0' });
    } else {
        $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_team_rstr_dv2.gif) top right no-repeat', 'padding': '0' });
    }
    $('#tb_dv1').css({ 'font-weight': 'normal' });
    $('#tb_dv1').css({ 'color': 'red' });
    $('#tb_dv2').css({ 'font-weight': 'bold' });
    $('#tb_dv2').css({ 'color': 'gray' });
    $('#t_dv1').hide();
    $('#t_dv2').show();
    dvTab = '2';
    return false;
}

function showFriendsRefer() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_friends_refer.gif) top right no-repeat', 'padding': '0' });
    $('#tb_refer').css({ 'color': 'gray' });
    $('#tb_refer').css({ 'font-weight': 'bold' });
    $('#tb_upload').css({ 'color': 'red' });
    $('#tb_upload').css({ 'font-weight': 'normal' });
    $('#tb_refstat').css({ 'color': 'red' });
    $('#tb_refstat').css({ 'font-weight': 'normal' });
    $('#tb_rules').css({ 'color': 'red' });
    $('#tb_rules').css({ 'font-weight': 'normal' });
    $('#f_refer').show();
    $('#f_upload').hide();
    $('#f_refstat').hide();
    $('#f_rules').hide();
    return false;
}

function showFriendsUpload() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_friends_upload.gif) top right no-repeat', 'padding': '0' });
    $('#tb_refer').css({ 'color': 'red' });
    $('#tb_refer').css({ 'font-weight': 'normal' });
    $('#tb_upload').css({ 'color': 'gray' });
    $('#tb_upload').css({ 'font-weight': 'bold' });
    $('#tb_refstat').css({ 'color': 'red' });
    $('#tb_refstat').css({ 'font-weight': 'normal' });
    $('#tb_rules').css({ 'color': 'red' });
    $('#tb_rules').css({ 'font-weight': 'normal' });
    $('#f_refer').hide();
    $('#f_upload').show();
    $('#f_refstat').hide();
    $('#f_rules').hide();
    return false;
}

function showFriendsRefStat() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_friends_refstat.gif) top right no-repeat', 'padding': '0' });
    $('#tb_refer').css({ 'color': 'red' });
    $('#tb_refer').css({ 'font-weight': 'normal' });
    $('#tb_upload').css({ 'color': 'red' });
    $('#tb_upload').css({ 'font-weight': 'normal' });
    $('#tb_refstat').css({ 'color': 'gray' });
    $('#tb_refstat').css({ 'font-weight': 'bold' });
    $('#tb_rules').css({ 'color': 'red' });
    $('#tb_rules').css({ 'font-weight': 'normal' });
    $('#f_refer').hide();
    $('#f_upload').hide();
    $('#f_refstat').show();
    $('#f_rules').hide();
    return false;
}

function slide1() {
    $('#sidebar_team_standings_inside1').hide();
    $('#sidebar_team_standings_inside2').show();
    $('#sidebar_team_standings_complete').show();
    $('#sidebar_team_standings_inside3').slideDown();
    return false;
}

function slide2() {
    $('#sidebar_team_standings_inside1').show();
    $('#sidebar_team_standings_inside2').hide();
    $('#sidebar_team_standings_complete').hide();
     $('#sidebar_team_standings_inside3').slideUp();
    return false;
}

function showStandingsDv1() {
    $('#sidebar_team_standings_inside3').css({ 'background': 'url(images/sidebar_team_standings_dv1.jpg) no-repeat' });

    $('#h_dv1').css({ 'color': 'black' });
    $('#h_dv1').css({ 'font-weight': 'bold' });
	
    $('#h_dv2').css({ 'color': 'red' });
    $('#h_dv2').css({ 'font-weight': 'normal' });

    $('#dv2').hide();
    $('#dv1').show();
    return false;
}

function showStandingsDv2() {
    $('#sidebar_team_standings_inside3').css({ 'background': 'url(images/sidebar_team_standings_dv2.jpg) no-repeat' });

    $('#h_dv1').css({ 'color': 'red' });
    $('#h_dv1').css({ 'font-weight': 'normal' });
    
    $('#h_dv2').css({ 'color': 'black' });
    $('#h_dv2').css({ 'font-weight': 'bold' });
    
    $('#dv1').hide();
    $('#dv2').show();
    return false;
}

function showRstrAdd() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_teamrstr_add.gif) top right no-repeat', 'padding': '0' });
    $('#tb_add').css({ 'color': 'gray' });
    $('#tb_add').css({ 'font-weight': 'bold' });
    $('#tb_upload').css({ 'color': 'red' });
    $('#tb_upload').css({ 'font-weight': 'normal' });
    $('#tb_remove').css({ 'color': 'red' });
    $('#tb_remove').css({ 'font-weight': 'normal' });
    $('#t_add').show();
    $('#t_upload').hide();
    $('#t_remove').hide();
    return false;
}

function showRstrUpload() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_teamrstr_upload.gif) top right no-repeat', 'padding': '0' });
    $('#tb_add').css({ 'color': 'gray' });
    $('#tb_add').css({ 'font-weight': 'bold' });
    $('#tb_upload').css({ 'color': 'red' });
    $('#tb_upload').css({ 'font-weight': 'normal' });
    $('#tb_remove').css({ 'color': 'gray' });
    $('#tb_remove').css({ 'font-weight': 'bold' });
    $('#t_add').hide();
    $('#t_upload').show();
    $('#t_remove').hide();
    return false;
}

function showRstrRemove() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_teamrstr_remove.gif) top right no-repeat', 'padding': '0' });
    $('#tb_add').css({ 'color': 'red' });
    $('#tb_add').css({ 'font-weight': 'normal' });
    $('#tb_upload').css({ 'color': 'red' });
    $('#tb_upload').css({ 'font-weight': 'normal' });
    $('#tb_remove').css({ 'color': 'gray' });
    $('#tb_remove').css({ 'font-weight': 'bold' });
    $('#t_add').hide();
    $('#t_upload').hide();
    $('#t_remove').show();
    return false;
}

function showAcctManage() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_acct_manage.gif) top right no-repeat', 'padding': '0' });
    $('#tb_manage').css({ 'color': 'gray' });
    $('#tb_manage').css({ 'font-weight': 'bold' });
    $('#tb_exch').css({ 'color': 'red' });
    $('#tb_exch').css({ 'font-weight': 'normal' });
    $('#tb_accru').css({ 'color': 'red' });
    $('#tb_accru').css({ 'font-weight': 'normal' });
    $('#a_manage').show();
    $('#a_exch').hide();
    $('#a_accru').hide();
    return false;
}

function showAcctExch() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_acct_exch.gif) top right no-repeat', 'padding': '0' });
    $('#tb_manage').css({ 'color': 'red' });
    $('#tb_manage').css({ 'font-weight': 'normal' });
    $('#tb_exch').css({ 'color': 'gray' });
    $('#tb_exch').css({ 'font-weight': 'bold' });
    $('#tb_accru').css({ 'color': 'red' });
    $('#tb_accru').css({ 'font-weight': 'normal' });
    $('#a_manage').hide();
    $('#a_exch').show();
    $('#a_accru').hide();
    return false;
}

function showAcctAccru() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_acct_accru.gif) top right no-repeat', 'padding': '0' });
    $('#tb_manage').css({ 'color': 'red' });
    $('#tb_manage').css({ 'font-weight': 'normal' });
    $('#tb_exch').css({ 'color': 'red' });
    $('#tb_exch').css({ 'font-weight': 'normal' });
    $('#tb_accru').css({ 'color': 'gray' });
    $('#tb_accru').css({ 'font-weight': 'bold' });
    $('#a_manage').hide();
    $('#a_exch').hide();
    $('#a_accru').show();
    return false;
}

function showSafPopup() {
    $('#nyroModalContent').css({ 'width': '711px', 'background': 'url(images/modal_saf_popup.jpg) top right no-repeat', 'padding': '0' });
    return false;
}

function pageselectCallback7(page_index, jq) {
    var new_content = $('#hidden_result7 div.result7:eq(' + page_index + ')').clone();
    $('#search_result7').empty().append(new_content);
    return false;
}

function initPagination7() {
    var num_entries = $('#hidden_result7 div.result7').length;
    // Create pagination element
    $("#pagination7").pagination(num_entries, {
        num_display_entries: 0,
        callback: pageselectCallback7,
        items_per_page: 1
    });
}

function pageselectCallback8(page_index, jq) {
    var new_content = $('#hidden_result8 div.result8:eq(' + page_index + ')').clone();
    $('#search_result8').empty().append(new_content);
    return false;
}

function initPagination8() {
    var num_entries = $('#hidden_result8 div.result8').length;
    // Create pagination element
    $("#pagination8").pagination(num_entries, {
        num_display_entries: 0,
        callback: pageselectCallback8,
        items_per_page: 1
    });
}

function disp_err(id) {
    getElement(id).style.display = "inline"
}
function hide_err(id) {
    getElement(id).style.display = "none"
}

var countdown1 = {
    init: function() {
        countdown1.remaining = countdown1.max - $(countdown1.obj).val().length;
        if (countdown1.remaining > countdown1.max) {
            $(countdown1.obj).val($(countdown1.obj).val().substring(0, countdown1.max));
        }
        $(countdown1.obj).siblings(".remaining1").html(countdown1.remaining + " characters remaining");
    },
    max: null,
    remaining1: null,
    obj: null
};

var countdown2 = {
    init: function() {
        countdown2.remaining = countdown2.max - $(countdown2.obj).val().length;
        if (countdown2.remaining > countdown2.max) {
            $(countdown2.obj).val($(countdown2.obj).val().substring(0, countdown2.max));
        }
        $(countdown2.obj).siblings(".remaining2").html(countdown2.remaining + " characters remaining");
    },
    max: null,
    remaining2: null,
    obj: null
};

function upload_complete_promo() {
    $('#upload_msg').html('Your image has been successfully uploaded. Please navigate back to the <a href=\"team.asp\" onclick=\"return doModal(this);\">team page</a> to view the changes.');
}

function GetBaseFolder() {
    return "";
}

function GetTeams(region_id) {
    getPage(GetBaseFolder() + 'teams.asp?region_id=' + region_id, 'region_' + region_id, '', '', '', '', '', true, '', true);
}

