
	//functions.js

	//Create a boolean variable to check for a valid MS instance.
	var xmlhttp = false;

	//Check if we are using IE.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using IE.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}

	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	//A variable used to distinguish whether to open or close the calendar.
	var showOrHide = true;

	function findPosX(obj){
		var curleft = 0;
		if (obj.offsetParent){
			while (obj.offsetParent){
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		} else if (obj.x){
			curleft += obj.x;
		}
		return curleft - 200;
	}

	function findPosY(obj){
		var curtop = 0;
		if (obj.offsetParent){
			while (obj.offsetParent){
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		} else if (obj.y){
			curtop += obj.y;
		}
		return curtop;
	}

	function autocompleteit(thevalue, e, language){

		theObject = document.getElementById("autocompletediv");

		theObject.style.visibility = "visible";
		theObject.style.width = "500px";

		var posx = 0;
		var posy = 0;

		posx = (findPosX (document.getElementById("searchCriteria")) + 1);
		posy = (findPosY (document.getElementById("searchCriteria")) + 23);

		theObject.style.left = posx + "px";
		theObject.style.top = posy + "px";

		var theextrachar = e.which;

		if (theextrachar == undefined){
			theextrachar = e.keyCode;
		}

		//The location we are loading the page into.
		var objID = "autocompletediv";

		//Take into account the backspace.
		if (theextrachar == 8){
			if (thevalue.length == 1){
				var serverPage = "ajaxscript/autocomp.php"+"&languages_id="+language;
			} else {
				var serverPage = "ajaxscript/autocomp.php" + "?sstring=" + thevalue.substr (0, (thevalue.length -1))+"&languages_id="+language;
			}
		} else {
			var serverPage = "ajaxscript/autocomp.php" + "?sstring=" + thevalue + String.fromCharCode (theextrachar)+"&languages_id="+language;
		}

		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				if (thevalue.length == 1 )
				{
					obj.innerHTML = "";
				}
				else
				{
					obj.innerHTML = xmlhttp.responseText;
				}
			}
		}
		xmlhttp.send(null);
	}

	function setvalue (thevalue){
		acObject = document.getElementById("autocompletediv");

		acObject.style.visibility = "hidden";
		acObject.style.height = "0px";
		acObject.style.width = "0px";

		document.getElementById("searchCriteria").value = thevalue;
	}


	function frontpageRefresh (thevalue, language){

		theObject = document.getElementById("frontapageNews");

		//The location we are loading the page into.
		var objID = "frontapageNews";

		//Take into account the backspace.
		var serverPage = "ajaxscript/frontpageRefresh.php?languages_id="+language;
		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
		//alert("frontpage");
	}
	function addToArena(itemNo,checked,ses){
		ses = "&" + ses;
		ses = "";

		if (checked)
		{
			var serverPage = "/ajaxscript/compare.php?arenaaction=enterarena&itemNo=" + itemNo;// + ses;
		}
		else
		{
			var serverPage = "/ajaxscript/compare.php?arenaaction=surrender&itemNo=" + itemNo;// + ses;
		}

		//var obj = document.getElementById("ajaxholder");
        xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function()
        {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                var json_result = eval('(' + xmlhttp.responseText  + ')');
                var maxContestors = json_result['checkCompare_maxContestors'];
                var productsPerPage = json_result['checkCompare_productsPerPage'];
                var count = json_result['checkCompare_count'];
                var maxContestorsText = json_result['checkCompare_maxContestorsText'];

                if(count >= maxContestors)
                {
                    alert(maxContestorsText);
                }

                for(var x = 1; x <= productsPerPage; x++)
                {
                    try
                    {
                        fieldObj = document.getElementById('item'+x);

                        if (fieldObj.checked == false)
                        {
                            if(count >= maxContestors)
                            {
                                fieldObj.disabled = true;
                            }
                            else
                            {
                                fieldObj.disabled = false;
                            }
                        }
                    }
                    catch(err) { }
                }

            }

            loopbreaker = false;
		}
		xmlhttp.send(null);
	}
	function fotoSearch(search){
        new Ajax.Request('/ajaxscript/fotosearch.php', {
            parameters: { search: search },
            method: 'get',
            onSuccess: function(transport) {
                    Modalbox.show('<div id="photosearch" style="text-align: left">' + transport.responseText + '</div>', 
                        { title: 'Photo Search' }
                    );      
                 }         
            });
	}
    
	function modalOpen (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.container.fadeIn('slow', function () {
				dialog.data.hide().slideDown('slow');
			});
		});
	}

	function nbNumbersOnly(e){
		var unicode=e.charCode? e.charCode : e.keyCode
	    if (unicode != 8 && unicode != 9 && unicode != 13) { //if the key isn't the backspace key (which we should allow)
	        if (unicode < 48 || unicode > 57) //if not a number
	            return false; //disable key press
	    }
	}
	function checkPostalCode(e){

		var serverPage = "/ajaxscript/postalcode.php?no=" + e;
		obj = document.getElementById('create_city');
		objCoun = document.getElementById('country');
		objCounName = document.getElementById('country_name');
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				//alert(obj.id);
				var tmp = new Array();
				var res = xmlhttp.responseText;
				tmp = res.split(';');

				if (tmp.length == 3)
				{

					obj.value = tmp[0];
					objCoun.value= tmp[1];
					objCounName.value = tmp[2];


/*
					for(i = 0; i < objCoun.length; i++)
					{


						if (Number(objCoun.options[i].value) == Number(tmp[1]))
						{
							objCoun.options[i].setAttribute("selected","selected");
							objCoun.options[i].selected = "selected";
							break;
						}
					}*/

				}




				//alert(xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);
    }
    
    //KNH 180509
    function getCityFromPostalCode(zip,target){
        var url = 'ajaxscript/city_from_zip.php';
        var param = {}
        param['zip'] = zip
        param['country'] = $('country').value
        new Ajax.Request(url, {
            method: 'post',
            parameters: param,
            onSuccess: function(transport) {
                $(target).value = transport.responseText.strip();
            }
        });        
    }
    
    var url = '/ajaxscript/stock_notifier.php';  
    function check_notify_form( form, products_id, products_name ) {
        var email_valid = false;
        var sms_valid = false;
        try {
            if( form.phone.checked ) {
                var validchars = "0123456789+"
                var isnumber = true;
                var c;
                
                for( i = 0; i < form.text_phone.value.length && isnumber; i++ ) {
                    c = form.text_phone.value.charAt(i);
                    if (validchars.indexOf(c) == -1) {
                        isnumber = false;
                    }
                }
                if( isnumber ) {
                    sms_valid = true;
                }
            }
        } catch(e) {
            sms_valid = true;
            //alert('Something went wrong...');
        } finally {
            try {
                try {
                    if( form.email.checked && form.text_email.value.indexOf('@') != -1 ) {
                        email_valid = true;
                    }
                } catch(e) {
                    if( form.text_email.value.indexOf('@') != -1 ) {
                        email_valid = true;
                    }
                }
            } catch(e) {
                email_valid = true;
                //alert('Something else went wrong...');
            } finally {
                if( !sms_valid && !email_valid ) {
                    document.getElementById('notify_error').style.display = 'block';   
                } else {
                    document.getElementById('notify_text').style.display = 'none';
                    document.getElementById('notify_error').style.display = 'none';
                    document.getElementById('notify_send').style.display = 'block';

                    document.getElementById('stock_notify').style.display = 'none';
                    document.getElementById('stock_notified').style.display = 'block';
                                                                                                                
                    try {
                        if( email_valid ) {
                            new Ajax.Request(url,
                            {
                                method: 'post',
                                parameters: {
                                    product: products_id,
                                    product_name: products_name, 
                                    email: form.text_email.value 
                                }
                            });
                        }
                    } catch(e) {}
                    
                    try {
                        if( sms_valid ) {
                            new Ajax.Request(url,
                            {
                                method: 'post',
                                parameters: {
                                    product: products_id, 
                                    product_name: products_name,
                                    sms: form.text_phone.value 
                                }
                            });
                        }
                    } catch(e) {}
                }
            }
        } 
        return false;   
    }