var original_page = true;

function pageLoad(hash,option) {
	// callback for handling ajax history/bookmarking if required
	if (hash.substring(0,1)=='/') {
		hash = hash.substring(1);
		if (option=='init') {
			var uri = parseUri(document.location.href);
			ajaxPage(uri.protocol+"://"+uri.host+uri.directory+hash,'',true);
		} else if (option=='check') {
			var uri = parseUri(document.location.href);
			ajaxPage(uri.protocol+"://"+uri.host+uri.directory+hash,'',true);
		}
	} else if (!original_page && document.location.href.indexOf('#') > -1 && hash == '') {
		var uri = parseUri(document.location.href);
		ajaxPage(uri.protocol+"://"+uri.host+uri.directory+uri.file+(uri.query!=''?'?'+uri.query:''),'',true);
	}
	original_page = false;
}

function showIndicator() {
	$('#indicator').defaultIndicator();
	$("#indicator").show();
}

function ajaxPage(href,reqtype,history,ni) {
	if (history==''||history==undefined) history=false;
	if (reqtype==''||reqtype==undefined) reqtype='default';
	if (ni==''||ni==undefined) ni=false;
	if (false && !history && (parseUri(href).protocol != parseUri(document.location.href).protocol)) {
		// switching protocol
		document.location.href=href;
	} else {
		if (ni!=true) showIndicator();
		$.ajax({
		    url: href,
		    type: 'POST',
			data: {ajax:1,ajaxtype:reqtype},
		    dataType: 'xml',
		    timeout: 10000,
		    error: function(){
				document.location.href=href;
		    },
		    success: function(xml){
				if (processResult(xml)) {
					if (history) {
						var uri = parseUri(href);
						var uri2 = parseUri(document.location.href);
						if (uri.file!=uri2.file || uri.query!=uri2.query) {
							$.historyLoad('/'+uri.file+(uri.query!=''?'?'+uri.query:''));
						} else {
							original_page = true;
							$.historyLoad('');
						}
					}
			    	$("#indicator").hide(200);
				}
		    }
		});
	}
}

function ajaxPageNI(href,reqtype,history) {
	if (history==''||history==undefined) history=false;
	if (reqtype==''||reqtype==undefined) reqtype='default';
	ajaxPage(href,reqtype,history,true);
}

function ajaxForm(oform,ni,wt,nhi) {
	if (wt==''||wt==undefined) {
		if (oform.id=='theLongForm') {
			wt=120000;
		} else {
			wt=10000;
		}
	}
	if (ni==''||ni==undefined) ni=false;
	if (nhi==''||nhi==undefined) nhi=false;
	if (ni!=true) showIndicator();
	$('#'+oform.id).ajaxSubmit({
	    url: oform.action,
	    type: 'POST',
	    dataType: 'xml',
	    timeout: wt,
	    error: function(){
			document.forms[oform.id].ajax.value='x';
			$('#'+oform.id).submit();

	    },
	    success: function(xml){
			if (processResult(xml)) {
		    	if (nhi!=true) $("#indicator").hide(200);
			}
	    }
	});
	return false
}

function ajaxFormNI(oform) {
	ajaxForm(oform,true);
	return false
}

function ajaxReq(href,reqtype,ni) {
	// no history required
	if (reqtype==''||reqtype==undefined) reqtype='default';
	if (ni==''||ni==undefined) ni=false;
	if (false && parseUri(href).protocol != parseUri(document.location.href).protocol) {
		// switching protocol
		document.location.href=href;
	} else {
		if (ni!=true) showIndicator();
		$.ajax({
		    url: href,
		    type: 'POST',
			data: {ajax:1,ajaxtype:reqtype},
		    dataType: 'xml',
		    timeout: 10000,
		    error: function(){
				document.location.href=href;
		    },
		    success: function(xml){
				if (processResult(xml)) {
			    	$("#indicator").hide(200);
				}
		    }
		});
	}
}

function ajaxReqNI(href,reqtype) {
	if (reqtype==''||reqtype==undefined) reqtype='default';
	ajaxReq(href,reqtype,true);
}

function processResult(xml) {
	var redirect = '';var aj_redirect = '';
	$(xml).find('item').each(function(){
		var elem_id = $(this).find('name').text();
		var item_opt = $(this).find('option').text();
		var item_text = $(this).find('data').text();
		if (elem_id=='#redirect') {
			redirect = item_text;
		}else if (elem_id=='#aj_redirect') {
			aj_redirect = item_text;
		} else if (elem_id.substring(0,4)=='#js_') {
			$.globalEval(item_text);
		} else {
			if (item_opt!='general') {
				eval('\$(elem_id).'+item_opt+'(item_text);');
			} else {
				$(elem_id).html(item_text);
				loadAutoClass(elem_id);
			}
		}
    });
	if (redirect) {
		document.location.href=redirect;
		return false;
	}else if (aj_redirect) {
		ajaxReq(aj_redirect);
		return false;
	} else {
		return true;
	}
}

function preProcess(history) {
	if($.browser.safari) {
		// not very reliable, so don't use it
	} else {
		if (history) $.historyInit(pageLoad);
	}
	loadAutoClass('');
	browserFix();
}

function loadAutoClass(id) {
	$(id + (id!=''?' ':'') + 'a.ajax').bind('click', function() {
		ajaxPage(this.href,'',($.browser.safari?false:true));
		return false;
	});
	$(id + (id!=''?' ':'') + 'a.ajax2').bind('click', function() {
		ajaxReq(this.href);
		return false;
	});
	$(id + (id!=''?' ':'') + 'a.ipopup').bind('click', function() {
		popup2(this.href);
		return false;
	});
	$(id + (id!=''?' ':'') + 'form.ajaxform').submit(function() {
		if (this.ajax.value == 'x') {
			// previous attempt failed lets not use ajax this time around
			this.ajax.value=0;
			return true;
		} else {
			this.ajax.value=1;
			return(ajaxForm(this))
		}
	});
}

function browserFix() {
}

function goTo(href) {
	document.location.href=href;
	return false;
}

function sendForm(mvar,oform,ret,mval) {
	document.forms[oform].iaction.name='iaction';
	document.forms[oform].iaction.value='';
	if (mval!=undefined) document.forms[oform].iaction.value=mval;
	if (mvar!='') document.forms[oform].iaction.name=mvar;
	$('#'+oform).submit();
	return (ret!='' && ret!=undefined ? ret : false);
}
