crossfading=0;
var crossfadeOpen;
var crossfadeLast;
var googlemapRequest=0;
var Last_AjaxDiv;
var Last_AjaxUrl;
var Last_AjaxTimestamp;
var VisibleLoadingDiv = 0;
var KeyPress;
var runAjaxTmr = new Array();
var EnableSelect;

function crossFade(showDiv){
	if(!crossfadeOpen){
		if(document.getElementById('main_image_wrapper') && document.getElementById('main_image_wrapper').style.display!=='none'){
			crossfadeOpen = 'main_image_wrapper';
		}
		else {
			if(document.getElementById('blankdiv')){
				crossfadeOpen='blankdiv';
			}
			else {
				crossfadeOpen='google-map';
			}
		}
	}
	
	if(crossfadeOpen==showDiv){
		showDiv=crossfadeLast;
		return 1;
	}

	if(!document.getElementById(showDiv) || document.getElementById(showDiv).style.display=='none' || !showDiv){
		return 0;	
	}
	
	fadeTime = 1000;
	if(!crossfading){
		crossfading=1;
		setTimeout("crossfading=0;",fadeTime);
		if(document.getElementById(showDiv).style.visibility == 'hidden' || !document.getElementById(showDiv).style.visibility){
			var fader1 = new Fx.Style(crossfadeOpen,'opacity', {duration:fadeTime});
			fader1.start(1,0);
		
			var fader2 = new Fx.Style(showDiv,'opacity', {duration:fadeTime});
			fader2.start(0,1);
		}
		else {
			var fader1 = new Fx.Style(showDiv,'opacity', {duration:fadeTime});
			fader1.start(1,0);
		
			var fader2 = new Fx.Style(crossfadeOpen,'opacity', {duration:fadeTime});
			fader2.start(0,1);
		}
		crossfadeLast=crossfadeOpen;
		crossfadeOpen=showDiv;
	}
	return 1;
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 20;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	r_Timestamp = new Date();
	return randomstring + '-' + r_Timestamp.getHours() + '-' + r_Timestamp.getMinutes() + '-' + r_Timestamp.getSeconds();
}


function AjaxData(AjaxDiv,FullURL,isDropdown){
	if(!document.getElementById(AjaxDiv)){
		return;	
	}
	
	CurrentTimestamp = new Date();
	if(Last_AjaxDiv !== AjaxDiv || Last_AjaxUrl !== FullURL || (CurrentTimestamp-Last_AjaxTimestamp)>2000){
		Last_AjaxDiv = AjaxDiv;
		Last_AjaxUrl = FullURL;
		Last_AjaxTimestamp = new Date();
	}
	else {
//Do not execute this function again. It was run with exactly the same variables within the last 2 seconds.
		return;
	}
	
	if(VisibleLoadingDiv){
//stop the loading bar from disappearing
		clearTimeout(VisibleLoadingDiv);
	}
	if(document.getElementById('LoadingPage')){
		document.getElementById('LoadingPage').style.display = 'block';
	}
	
	if(runAjaxTmr[AjaxDiv]){
		clearTimeout(runAjaxTmr[AjaxDiv]);
		
		if(EnableSelect && isDropdown){
			clearTimeout(EnableSelect);	
		}
	}
	
	if(isDropdown){
		document.getElementById(AjaxDiv).disabled=true;
		document.getElementById(AjaxDiv).options.length = 1;
		document.getElementById(AjaxDiv).options[0].value = '0';
		document.getElementById(AjaxDiv).options[0].text = 'Loading...';
		runAjaxTmr[AjaxDiv] = setTimeout("AjaxSelect('"+AjaxDiv+"','"+FullURL+"')",250);
	}
	else {
		runAjaxTmr[AjaxDiv] = setTimeout("runAjax('"+AjaxDiv+"','"+FullURL+"')",250);
	}
}

function runAjax(AjaxDiv,FullURL){
	var xmlhttp=false;
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP');
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest();
        }
    xmlhttp.open('GET', FullURL + "&RandomString=" + randomString(), true);
    xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			var content = xmlhttp.responseText;
			if( content ){
				if(document.getElementById(AjaxDiv)){
//hide the loading bar after timer = 0;
					if(document.getElementById('LoadingPage')){
						VisibleLoadingDiv = setTimeout("document.getElementById('LoadingPage').style.display = 'none';",500);
					}
//display content inside div
					document.getElementById(AjaxDiv).innerHTML = content;
				}
			}
		}
	}
    xmlhttp.send(null)
	return;	
}

function AjaxSelect(Select,FullURL){
	var xmlhttp=false;
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP');
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest();
        }
    xmlhttp.open('GET', FullURL + "&RandomString=" + randomString(), true);
    xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			var content = xmlhttp.responseText;
			if( content && document.getElementById(Select) ){
				document.getElementById(Select).options.length = 0;
				items = content.split("[|]");
				document.getElementById(Select).options.length = items.length;
				for(i = 0; i < items.length; i++){
					if(items[i]){
						data = items[i].split("[-]");
						if(data[0] && data[1]){
							if(data[0].indexOf("[|selected|]")>0){
								data[0] = data[0].replace("[|selected|]","")
								document.getElementById(Select).options[i].selected = true;
							}
							document.getElementById(Select).options[i].value = data[0];
							document.getElementById(Select).options[i].text = data[1];
						}
					}
				}
				if(document.getElementById('LoadingPage')){
					VisibleLoadingDiv = setTimeout("document.getElementById('"+Select+"').disabled=false;document.getElementById('LoadingPage').style.display = 'none';",500);
				}
					EnableSelect = setTimeout("document.getElementById('"+Select+"').disabled=false;",100);
			}
		}
	}
    xmlhttp.send(null)
	return;
}

