addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();

function getBaseURL(){
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }
}

function pausecomp(ms) {
	ms += new Date().getTime();
	while (new Date() < ms){}
}

function submitenter(myfield,e,page) {
	var keycode;
	
	if (window.event){
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}

	if (keycode == 13) {
   		submitSearch(page);
   		return false;
	} else {
		return true;
	}
}

function submitSearch(page){
	var keyword = document.getElementById('input-keyword').value;
	var location = document.getElementById('input-location').value;

	if (keyword.toLowerCase() == translation_keyword.toLowerCase() && location.toLowerCase() == translation_location.toLowerCase()){
		alert(translation_no_keyword);
		document.getElementById('input-keyword').focus();
		return false;
	}

	if (keyword == '' && location.toLowerCase() == translation_location.toLowerCase()){
		alert(translation_no_keyword);
		document.getElementById('input-keyword').focus();
		return false;
	}

	if (keyword == '' && location == ''){
		alert(translation_no_keyword);
		document.getElementById('input-keyword').focus();
		return false;
	}

	if (keyword == '' && location.toLowerCase() != translation_location.toLowerCase()){
		alert(translation_no_keyword);
		document.getElementById('input-keyword').focus();
		return false;
	}

	if (document.getElementById('input-keyword').value.toLowerCase() == translation_keyword.toLowerCase()){
		document.getElementById('input-keyword').value = '';
	}

	if (document.getElementById('input-location').value.toLowerCase() == translation_location.toLowerCase()){
		document.getElementById('input-location').value = '';
	}
	
	saveClick('',page, 'searchsection', 'searchbutton');
	pausecomp(500);
	document.getElementById('frm-search').submit();
}

function advancedSearch(page){
	saveClick('',page, 'searchsection', 'toadvancedbutton');
	alert('Go advanced search');
}

function emptyKeyword(){
	if (document.getElementById('input-keyword').value.toLowerCase() == translation_keyword.toLowerCase()){
		document.getElementById('input-keyword').value = '';
		return false;
	}
}

function checkKeyword(){
	if (document.getElementById('input-keyword').value == ''){
		document.getElementById('input-keyword').value = translation_keyword;
		return false;
	} else if (document.getElementById('input-keyword').value.toLowerCase() == translation_keyword.toLowerCase()){
		document.getElementById('input-keyword').value = translation_keyword;
		return false;		
	}
}

function emptyLocation(){
	if (document.getElementById('input-location').value.toLowerCase() == translation_location.toLowerCase()){
		document.getElementById('input-location').value = '';
		return false;
	}
}

function checkLocation(){
	if (document.getElementById('input-location').value == ''){
		document.getElementById('input-location').value = translation_location;
		return false;
	} else if (document.getElementById('input-location').value.toLowerCase() == translation_location.toLowerCase()){
		document.getElementById('input-location').value = translation_location;
		return false;		
	}
}

function CheckEmail(obj){
	email = obj.value;
	AtPos = email.indexOf("@");
	StopPos = email.lastIndexOf(".");
	Message = "";

	if (email == "") {
		Message = translation_email_error + "\n";
	}

	if (AtPos == -1 || StopPos == -1) {
		Message = translation_email_error + "\n";
	}

	if (StopPos < AtPos) {
		Message = translation_email_error + "\n";
	}

	if (StopPos - AtPos == 1) {
		Message = translation_email_error + "\n";
	}
	
	if (Message != ''){
		alert(Message);
		return false;
	} else {
		return true;
	}
}

function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
		while(1){
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
        		break;
        		obj = obj.offsetParent;
        	}
	else if(obj.x)
		curleft += obj.x;
		return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1){
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
		return curtop;
}

function gup(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );

	if( results == null )
		return "";
	else
		return results[1];
}

