/*--------------------------------------------
 * Standard Javascript file
 * 
 * Copyright (c) 2009 by Jan VL
 * http://www.designrequest.be
 *
--------------------------------------------*/


/*--------------------------------
 	AJAX
-------------------------------*/

function makeRequest(url, div) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/html');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {}
            }
        }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.open('GET', url, true);
        httpRequest.onreadystatechange = function() { showContents(httpRequest, div); };
        httpRequest.send('');

}

function showContents(httpRequest, div) {

	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById(div).innerHTML = httpRequest.responseText;
			//document.getElementById("notify").style.visibility = "visible";		
            //} else {
                //alert('There was a problem with the request.');
		}
	}

}

/*--------------------------------
	Floating DIV
-------------------------------*/

function showBox(box, evt, text) {

	/*simple browser detection in order to choose between using pageX (does not work in IE) and clientX*/
	var IE=false;
	if(navigator.appName == "Microsoft Internet Explorer")
	IE = true;

	if(IE)
	{

		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;

	}
	else
	{
		x = evt.pageX;
		y = evt.pageY;
	}

	/* Set the text in the box */
	document.getElementById(box).innerHTML = text;

	/*display the box at 15px from the mouse pointer*/
	obj = document.getElementById(box);
	obj.style.left = (x + 15) + 'px';
	obj.style.top = (y + 15) + 'px';
	obj.style.visibility = 'visible';
}

function closeBox()
{
	/*Hide the box on close event*/
	obj = document.getElementById("box_err");
	obj.style.visibility = 'hidden';
	obj = document.getElementById("box_info");
	obj.style.visibility = 'hidden';
	obj = document.getElementById("box_promo");
	obj.style.visibility = 'hidden';
}

function setOK(field) {
	document.getElementById(field).childNodes[0].src = '/img/button_ok.png';
}
/*--------------------------------
 	Count textarea chars
-------------------------------*/
function checkChars(textfield, countfield, maxchars) {

	typedchars = textfield.value.length;
	countfield.value = maxchars - typedchars;

	if (typedchars > maxchars) {
		countfield.style.background = "#F00";
		textfield.value = textfield.value.substring(0, maxchars);
	} else {
		countfield.style.background = "#FFF"
	}
	
}



/*--------------------------------
 	Form loading
-------------------------------*/
function lockForm() {
	document.getElementById("submitbutton").style.display = "none";
	document.getElementById("progress").style.display = "block";

	ProgressImage = document.getElementById('progress_image');		
	setTimeout("ProgressImage.src = ProgressImage.src",100);
	return true;

}

/*--------------------------------
 	Confirm delete
-------------------------------*/
function confirmSubmit(pre, name, post) {
        if (confirm(pre + "\n\n" + name + "\n\n" + post))
                return true;
        return false;
}

function confirmDelete(url, pre, name, post) {
        if (confirm(pre + "\n\n" + name + "\n\n" + post))
                window.location = url;
}
/*--------------------------------
 	Panels
-------------------------------*/
function showPanel(evt, panel) {

	ls = document.getElementById(panel);
	if (evt) {
		/*simple browser detection in order to choose between using pageX (does not work in IE) and clientX*/
		var IE=false;
		if(navigator.appName == "Microsoft Internet Explorer")
		IE = true;

		if(IE) {
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;

		} else {
			x = evt.pageX;
			y = evt.pageY;
		}

		ls.style.left = (x + 5) + 'px';
		ls.style.top = (y + 5) + 'px';	
	}

	if (ls.style.display == 'block') {
		ls.style.display = 'none';
	} else {
		ls.style.display = 'block';
	}

}

function hidePanel(panel) {
	document.getElementById(panel).style.display = 'none';
}

/*--------------------------------
 	Live search box
-------------------------------*/
function liveSearch(q, shop_id) {
        document.getElementById('livesearch').style.display = 'block';
                
        if (q.length == 0) {
                document.getElementById('livesearch').style.display = 'none';
        } else {
                makeRequest('/ajax/search.php?shop_id=' + shop_id + '&q=' + q, 'livesearch');
        }
}

/*--------------------------------
 	Hide suggestboxes
-------------------------------*/

document.onmouseup = function hideBoxes(e){
        var target = (e && e.target) || (event && event.srcElement);
        var obj = document.getElementsByClassName('suggestbox');

        for (i = 0; i < obj.length; i++) {
                if (obj[i].style.display == 'block' && target.parentNode.className != 'suggestarea') {
                        obj[i].style.display = 'none';
                }
        }
}

document.getElementsByClassName = function (c) {
        var r = new Array();
        var j = 0;
        var o = document.getElementsByTagName('*');

        for(i = 0; i< o.length; i++) {
                if(o[i].className == c) {
                        r[j]=o[i];
                        j++;
                }
        }
        return r;
}

/*--------------------------------
 	Subnavivation
-------------------------------*/
function showSub(obj) {
        hideSubs();
        document.getElementById('subnav' + obj).style.display = 'block';
        document.getElementById('navli' + obj).style.backgroundImage = 'url(/img/layout/nav_hover.png)';
}

function hideSubs() {
        var obj = document.getElementsByClassName('subnav');
        for (i = 0; i < obj.length; i++) {
                if (obj[i].style.display == 'block')
                        obj[i].style.display = 'none';
        }
        var obj = document.getElementsByClassName('navli');
        for (i = 0; i < obj.length; i++) {
                obj[i].style.backgroundImage = 'url(/img/layout/nav_bar.gif)';
        }
}

