/* --------------------------------------------------------
    Javascript Global Functions
    Created by: Guillaume - guillaume@tearsfromthesky.com
    Filename: js/global.js
   -------------------------------------------------------- */

/* --------------------------------------------------------
    Function:     open_window()
    Description:  Opens window (popup) with a set of params
    Params:       @page_url        - string
                  @page_name       - string
                  @window_width    - numeric, no quotes
                  @window_height   - numeric, no quotes
                  @scrollbar_value - boolean, 'yes' or 'no'
                  @is_center       - boolean, 'yes' or 'no'
   -------------------------------------------------------- */

    function open_window(page_url, page_name, window_width, window_height, scrollbar_value, is_center) {
        var window_pos_x = 20;
        var window_pos_y = 20;
        if (is_center == 'yes') {
            window_pos_x = (screen.width / 2) - (window_width / 2);
            window_pos_y = (screen.height / 2) - (window_height / 2);
        }
        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);
        popup_window.resizeTo(window_width + 6, window_height + 54);
        popup_window.focus();
    }

/* --------------------------------------------------------
    Function:     anti_spam_email()
    Description:  Protects email from spam bots
    Params:       @user     - string
                  @domain   - string
                  @linktext - string (not required)
   -------------------------------------------------------- */

    function anti_spam_email(user, domain, linktext) {
        var username = user;
        var hostname = domain;
        if (linktext == '') {
            linktext = username + "&#064;" + hostname;
        }
        document.write("<a href=" + "mail" + "to:" + username +"&#064;" + hostname + ">" + linktext + "</a>");
    }

/* --------------------------------------------------------
    Function:     show_hide_element()
    Description:  Shows and hides DOM element by ID
    Params:       @obj - string
   -------------------------------------------------------- */

    function show_hide_element(id){
        var my_element = document.getElementById(id);
        if (my_element.style.display == "none"){
            my_element.style.display = "block";
        } else {
            my_element.style.display = "none";
        }
    }
    
/* --------------------------------------------------------
    Rollover Function + Windows onLoad
   -------------------------------------------------------- */
   
    function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
    }
   
    window.onload = initRollovers;    

/* --------------------------------------------------------
    Ajax functions
   -------------------------------------------------------- */
   
	function GetXmlHttpObject() {
		var xmlHttp=null;
		try { // Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) { // Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}

	function account_refresh() {
		for (var i = 0; i < document.frm_account.elements.length; i++) {
			if (document.frm_account.elements[i].name == "intLocation" && document.frm_account.elements[i].checked == true) {
				intLocation = document.frm_account.elements[i].value;
			}
		}
		xmlHttp = GetXmlHttpObject()
		if (xmlHttp == null) {
			return;
		} 
		var strUrl = "../account/order.php?strAction=refresh&strPostalcode1="+document.getElementById("strPostalcode1").value+"&strPostalcode2="+document.getElementById("strPostalcode2").value+"&intOrdertype="+document.getElementById("intOrdertype").value+"&intLocation="+intLocation;
		xmlHttp.onreadystatechange = account_show;
		xmlHttp.open("GET",strUrl,true);
		xmlHttp.send(null);
	}

	function account_show()  { 
		if (xmlHttp.readyState == 4) {
			var strResponseText = xmlHttp.responseText;
			document.getElementById("table_locations").innerHTML = strResponseText;
		}
	}

	function order_add() {
		arrArgs = order_add.arguments;
		xmlHttp = GetXmlHttpObject()
		if (xmlHttp == null) {
			return;
		} 
		var strUrl = "../menu/order.php?strAction=add&intProduct="+arrArgs[0]+"&intFormat="+arrArgs[1]+"&strNote="+arrArgs[2];
		xmlHttp.onreadystatechange = order_show;
		xmlHttp.open("GET",strUrl,true);
		xmlHttp.send(null);
		show_hide_element('div_confirm_'+arrArgs[0]);
		var timer = setTimeout("show_hide_element('div_confirm_"+arrArgs[0]+"')",2000)
	}

	function order_datetime() {
		arrArgs = order_add.arguments;
		xmlHttp = GetXmlHttpObject()
		if (xmlHttp == null) {
			return;
		} 
		var strUrl = "../menu/order.php?strAction=datetime&dateOrder="+document.getElementById("dateOrder").value+"&timeOrder="+document.getElementById("timeOrder").value+"&intWeekday="+document.getElementById("intWeekday").value+"&datePrevious="+document.getElementById("datePrevious").value;
		xmlHttp.onreadystatechange = order_show;
		xmlHttp.open("GET",strUrl,true);
		xmlHttp.send(null);
	}

	function order_refresh(intItems) {
		strQuery = "strAction=save";
		for (i = 1; i <= intItems; i++) {
			strQuery = strQuery+"&arrItems["+document.getElementById("ipt_items_"+i).value+"]="+document.getElementById("cbx_items_"+i).checked;
		}
		xmlHttp = GetXmlHttpObject()
		if (xmlHttp == null) {
			return;
		} 
		var strUrl = "../menu/order.php?"+strQuery;
		xmlHttp.onreadystatechange = order_show;
		xmlHttp.open("GET",strUrl,true);
		xmlHttp.send(null);
	}

	function order_show()  { 
		if (xmlHttp.readyState == 4) {
			var strResponseText = xmlHttp.responseText;
			if (strResponseText == "refresh") {
				window.location.reload();
			} else {
				document.getElementById("div_order").innerHTML = strResponseText;
			}
		}
	}


/* --------------------------------------------------------
    Forms functions
   -------------------------------------------------------- */
	function checkall(strForm,strName,boolChecked) {
		for (var i = 0; i < strForm.elements.length; i++) {
			while (strForm.elements[i].name != strName && strForm.elements[i].name != strName+"[]") {
				i++;
				if (!strForm.elements[i]) {
					return;
				}
			}
			strForm.elements[i].checked = boolChecked;
		}
	}

