/* --------------------------------------------------------
   Javascript Global Functions
   global.js
   -------------------------------------------------------- */

/*
 preload_image()
 image preloading
*/
function preload_image(image_name, image_src) {
    if (document.images) {
        eval(image_name + ' = new Image()');
        eval(image_name + '.src = "' + image_src + '"');
    }
}

/*
 change_image()
 swap images
*/
function change_image(image_name, image_src) {
    if (document.images) {
        document.images[image_name].src = image_src;
    }
}

/*
 open_window()
 opens a new pop-up window
*/
function open_window(page_url, page_name, window_width, window_height, scrollbar_value, is_center) {
    // set window position
    var window_pos_x = 20;
    var window_pos_y = 20;

    // if popup is center
    if (is_center == 'yes') {
        window_pos_x = (screen.width / 2) - (window_width / 2);
        window_pos_y = (screen.height / 2) - (window_height / 2);
    }

    // open the window
    popup_window = this.open(page_url, page_name, "toolbar=no,status=no,menubar=no,location=no,scrollbars=" + scrollbar_value + ",resizable=no,width=" + window_width + ",height=" + window_height + ",screenX=" + window_pos_x + ",screenY=" + window_pos_y + ",left=" + window_pos_x + ",top=" + window_pos_y);

    // resize the window, in case successive calls are made to same window
    popup_window.resizeTo(window_width + 6, window_height + 54);

    // focus popup
    popup_window.focus();
}

/*
 anti_spam_email()
 protects email from spam bots
*/

function anti_spam_email(user,domain) {
    var username = user;
    var hostname = domain;
    var linktext = username + "&#064;" + hostname;
    document.write("<a href=" + "mail" + "to:" + username +"&#064;" + hostname + ">" + linktext + "</a>");
}

/*
 showHideElement()
 shows and hides DOM element
*/

function showHideElement(obj){
    var my_element = document.getElementById(obj);

    if (my_element.style.display == "none"){
        my_element.style.display = "block";
    } else {
        my_element.style.display = "none";
    }
}

/*
 showElement()
 shows DOM element
*/

function showElement(obj){
    var my_element = document.getElementById(obj);
    my_element.style.display = "block";
}

/*
 hideElement()
 hide DOM element
*/

function hideElement(obj){
    var my_element = document.getElementById(obj);
    my_element.style.display = "none";
}

function showHideAllDetails(total, status) {
	for (i = 1; i < (total + 1); i++) {
		var show_details = document.getElementById("show_details_" + i);
		var show_details_off = document.getElementById("show_details_off_" + i);
		var show_details_on = document.getElementById("show_details_on_" + i);
		if (status == "on") {
			show_details.style.display = "block";
			show_details_off.style.display = "none";
			show_details_on.style.display = "block";
		} else if (status == "off") {
			show_details.style.display = "none";
			show_details_off.style.display = "block";
			show_details_on.style.display = "none";
		}
	}
}

/************************************

	Custom Alert Demonstration
	version 1.0
	last revision: 02.02.2005
	steve@slayeroffice.com

	Should you improve upon this source please
	let me know so that I can update the version
	hosted at slayeroffice.

	Please leave this notice in tact!

************************************/

// constants to define the title of the alert and button text.
var ALERT_TITLE = "Unsigned.com Alert";
var ALERT_BUTTON_TEXT = "Ok";
var ALERT_CANCEL_TEXT = "Cancel";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
//if(document.getElementById) {
//	window.confirm = function(txt) {
//		createCustomAlert(txt);
//	}
//}

function createCustomAlert(txt, url) {
	
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.appendChild(d.createTextNode(txt));

	// create clearfix div
	clearfix = alertObj.appendChild(d.createElement("div"));
	clearfix.id = "clearfix";
	
	// ok button
	btn = clearfix.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "javascript:;";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { 
	    removeCustomAlert();
	    window.location = url;
	    return false;
	}
	
	// cancel button
	cancel = clearfix.appendChild(d.createElement("a"));
	cancel.id = "cancelBtn";
	cancel.appendChild(d.createTextNode(ALERT_CANCEL_TEXT));
	cancel.href = "javascript:;";
	cancel.focus();
	// set up the onclick event to remove the alert when the anchor is clicked
	cancel.onclick = function() { 
	    removeCustomAlert();
	    return false; 
	}
	
}

// For Page Builder
function createCustomAlertPB(txt, action) {
	
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.appendChild(d.createTextNode(txt));

	// create clearfix div
	clearfix = alertObj.appendChild(d.createElement("div"));
	clearfix.id = "clearfix";
	
	// ok button
	btn = clearfix.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "javascript:;";
	// set up the onclick event to remove the alert when the anchor is clicked
	if (action == 'save_confirm') {
		btn.onclick = function() { 		    
		    removeCustomAlert();
		    ColorScheme.applyChanges();
		}
	} else if (action == 'reset_confirm') {
		btn.onclick = function() { 		    
		    removeCustomAlert();
		    ColorScheme.resetChanges();
		}
	} else if (action == 'save_ok') {
		btn.onclick = function() { 
		    removeCustomAlert();	    
		}
	}
	
	// cancel button
	if (action == 'save_confirm' || action == 'reset_confirm') {
		cancel = clearfix.appendChild(d.createElement("a"));
		cancel.id = "cancelBtn";
		cancel.appendChild(d.createTextNode(ALERT_CANCEL_TEXT));
		cancel.href = "javascript:;";
		cancel.focus();
		// set up the onclick event to remove the alert when the anchor is clicked
		cancel.onclick = function() { 
		    removeCustomAlert();
		    return false; 
		}
	}
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}
