// popup window for image galleries
function open_window_img(img_src, width, height, scroll, resize)
{
	if (!scroll) { scroll = "1";}
	if (!resize) { resize = "1";}

    window.open("/includes/popup.php?img=" + img_src + "", /*WARNING -- don't put any spaces in the window name */ "Large_image", "toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=" + scroll + ",resizable=" + resize +", width=" + width + ", height=" + height + "");
}

// popup window for popup pages
function open_window_html(html_src, width, height, scroll, resize, name)
{
	if (!scroll) { scroll = "1";}
	if (!resize) { resize = "1";}
	if (!name) { name = "popup";}
/*WARNING -- don't put any spaces in the window name */
    new_window = window.open(html_src, name, "toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=" + scroll + ",resizable=" + resize +", width=" + width + ", height=" + height + "");
    new_window.focus();
}


//
// Format a number as a dollar amount
//
function dollarFormat(dolFloat)
{
    var string = new String(Math.floor((dolFloat * 100)));
    return string.substring(0, string.length - 2) +"." + string.substring(string.length - 2, string.length)
}


function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


function toggleAddtoMyFestival(evv_id, se_id, ev_title, ev_link, a_obj)
{

    if (a_obj.className == "button added_to_myfest_dots") {
        // remove it
        a_obj.className = "button add_to_myfest_dots";
        removeEvent(evv_id);
    } else {
        // add it
        a_obj.className = "button added_to_myfest_dots";
        addEvent(evv_id, se_id, ev_title, ev_link);
    }

}

function toggleAddtoMyFestivalSmall(evv_id, se_id, ev_title, ev_link, a_obj)
{

    if (a_obj.className == "button added") {
        // remove it
        a_obj.className = 'button add';
        removeEvent(evv_id);
    } else {
        // add it
        a_obj.className = 'button added';
        addEvent(evv_id, se_id, ev_title, ev_link);
    }

}


//
// adds an event to the unsaved cookie thing
//
function addEvent(evv_id, se_id, ev_title, ev_link)
{

    // get unsaved cookie
    unsaved = readCookie("unsaved");

    // does the event already exist
    if (document.getElementById("myfest-" + evv_id)) {
        // event exists
        // update the event with the new session if one is passsed
        if (se_id) {
            add_str = "[" + evv_id + ":" + se_id + "]";
            // check if there are any values in the cookie
            if (unsaved) {
                // check if this evv_id already exists
                regex = new RegExp("\\[" + evv_id + ":");
                if (unsaved.match(regex)) {
                    // replace the existing session with this new one
                    regex = new RegExp("\\[" + evv_id + ":.*?\\]");
                    unsaved = unsaved.replace(regex, add_str);
                } else {
                    // if it doesn't exists in the cookie append it
                    unsaved += add_str;
                }
            } else {
                // if the cookie doesn't exist make it so
                unsaved = add_str;
            }
            createCookie("unsaved", unsaved, 365);
        }

    } else {

        // event doesn't exist add it to the cookie
        if (se_id) {
            add_string = "[" + evv_id + ":" + se_id + "]";
        } else {
            add_string = "[" + evv_id + ":undefined]";
        }

        if (unsaved) {
            unsaved += add_string;
        } else {
            unsaved = add_string;
        }

        // create new list item
        fest_ql = document.getElementById("festival_quicklist");
        fest_ql_ul = fest_ql.getElementsByTagName("UL")[0];
        new_li = document.createElement("li");
        new_li.innerHTML = "<a href='" + ev_link + "'>" + ev_title + "</a>";
        // it's very important that the id is created
        new_li.id = 'myfest-' + evv_id;
        fest_ql_ul.appendChild(new_li);

        createCookie("unsaved", unsaved, 365);

    }

    // remove it from the todelete cookie
    todelete = readCookie("todelete");
    if (todelete) {
        regex = new RegExp(evv_id + "\\|");
        todelete = todelete.replace(regex, "");
        createCookie("todelete", todelete, 365);
    }

}


//
// adds an event to the todelete cookie thing
//
function removeEvent(evv_id)
{

    // get todelete cookie
    todelete = readCookie("todelete");
    exists = false;

    if (todelete) {
        var events = todelete.split('|');
    	for (var i = 0; i < events.length; i++)
    	{
    		if (events[i] == evv_id) {
    		    exists = true;
    		}
    	}
    }

    if (!exists) {

        add_string = evv_id + "|";

        if (todelete) {
            todelete += add_string;
        } else {
            todelete = add_string;
        }

        // create cookie - expires in 365 days
        createCookie("todelete", todelete, 365);

    }

    // remove item from myfestival list
    if (li_obj = document.getElementById("myfest-" + evv_id)) {
        li_obj.parentNode.removeChild(li_obj);
    }

    // remove it from the unsaved cookie
    unsaved = readCookie("unsaved");
    if (unsaved) {
        regex = new RegExp("\\[" + evv_id + ":.*?\\]");
        unsaved = unsaved.replace(regex, "");
        createCookie("unsaved", unsaved, 365);
    }

}


function toggle(div_id)
{

    if (div_obj = document.getElementById("toggle-" + div_id)) {

        img_obj = document.getElementById("toggle-img-" + div_id);

        if (div_obj.style.display == "none") {
            div_obj.style.display = "inline";
            if (img_obj) {
                img_obj.src = "/images/contract.gif";
            }
        } else {
            div_obj.style.display = "none";
            if (img_obj) {
                img_obj.src = "/images/expand.gif";
            }
        }
    }
}

