/*****************************
/* Global Object
/*****************************/
var ajaxObj = null;

/*****************************
/*
/*****************************/
function ajaxRequest(url, feedback) {

	this.ajaxImage = "http://www.photolife.lv/auto/images/ajax-loader.gif";
	var preloadAjaxImage = new Image();
	preloadAjaxImage.src = this.ajaxImage;

	//var ajaxCursor = "http://design.svnets.lv/ads/skins/default/cursors/ajax.cur";
	//this.ajaxResult = "";
	//this.ajaxResultXML = null;
	this.xmlhttp = null;
	this.url = url ? url : "";
	this.postVars = new Array();
	this.feedback = feedback ? feedback : null;

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.init = function() {

	if (window.XMLHttpRequest) {

		/*****************************
		/* Firefox, Opera
		/*****************************/

		this.xmlhttp = new XMLHttpRequest();

	} else if (window.ActiveXObject) {

		/*****************************
		/* IE
		/*****************************/

		try {

			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {

			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {

			}

		}

	}

	if (!this.xmlhttp) {
		//alert('Cannot create XMLHTTP instance');
		return false;
	}

	this.imageInit();

	this.xmlhttp.onreadystatechange = this.onreadystatechange;

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.process = function( url, method ) {

	if (!this.xmlhttp) {

		this.init();
		//alert('Loaded from Process');

	}

	if (this.readyToProcess()) {

		this.showAjaxImage();

		method = method == "POST" ? "POST" : "GET";
		url = url ? url : this.url;
		url += "&ajax=1";

		this.xmlhttp.open( method, url, true );
		this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		if (method == "GET") { //GET
			this.xmlhttp.send( null );
		} else { //POST
			var postString = this.getPostString();
			this.xmlhttp.send( postString );
		}

		return true;

	}

	return false;

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.readyToProcess = function() {

	return !( this.xmlhttp.readyState && ( this.xmlhttp.readyState < 4 ) );

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.responseState = function() {

	return ( this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200 ) ? true : false;

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.onreadystatechange = function( ) {

	if (ajaxObj.responseState()) {
		hide( 'ajaxImage' );
		this.postVars = new Array();
		if ( typeof( ajaxObj.feedback ) == 'function' ) {
			ajaxObj.feedback();
		}
		this.feedback = null;
	}

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.getByXML = function( tagName, tagNum ) {

	if ( !tagName ) return;
	if ( !tagNum ) tagNum = 0;

	var node = this.xmlhttp.responseXML.getElementsByTagName( tagName )[ tagNum ];

	if (node)
		return node.firstChild.nodeValue;
	else return false;

}

ajaxRequest.prototype.getNodeByXML = function( tagName, tagNum ) {

	if ( !tagName ) return;
	if ( !tagNum ) tagNum = 0;

	return this.xmlhttp.responseXML.getElementsByTagName( tagName )[ tagNum ];

}

ajaxRequest.prototype.getText = function() {

	return this.xmlhttp.responseText;

}

ajaxRequest.prototype.getJson = function() {

	return eval( "(" + this.getText() + ")" );

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.imageInit = function () {

	if (_obj('ajaxImage')) return;

	var objBody = document.getElementsByTagName("body").item(0);

	//var imgPreloader = new Image();
	//imgPreloader.src = this.ajaxImage;

		var objLoadingImage = document.createElement("img");
		//objLoadingImage.src = ajaxObj.ajaxImage;
		objLoadingImage.setAttribute('id','ajaxImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objLoadingImage.style.display = 'none';
		objLoadingImage.setAttribute('src', this.ajaxImage );
		//objLoadingImage.style.background = 'transparent';
		//objLoadingImage.style.Color = 'transparent';
		objBody.insertBefore(objLoadingImage, objBody.firstChild);

		//objLoadingImage.onload = function() { };	//	clear onLoad, as IE will flip out w/animated gifs

		return false;

	//}

    //imgPreloader.src = this.ajaxImage;

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.showAjaxImage = function() {

	obj = _obj('ajaxImage');
	if (!obj.src) obj.src = this.ajaxImage;
	yScroll = getPageScroll();
	yScroll = yScroll[1];
	obj.style.top = (yScroll + 5) + 'px';
	if (is_opera)
		var returnLeft = 90;
	else
		var returnLeft = 15;
	obj.style.left = (document.body.clientWidth - obj.width - returnLeft) + 'px';
	show('ajaxImage');
	//_obj('ajaxImage').style.display = 'block';
	/*if (this.ajaxCursor != "") {
		document.body.style.cursor = 'wait';//'url("'+ajaxCursor+'");';
	}*/

}

/*****************************
/*
/*****************************/
ajaxRequest.prototype.parseForm = function( formName ) {

	var oForm = document.getElementsByName( formName );
	if (!oForm) return false;
	if (oForm.length > 1) {
		alert("Engine error! Dublicate Form Name!");
		return false;
	}
	oForm = oForm[0]; //now we get form object

	for (var i = 0; i < oForm.elements.length; i++) {

		var el = oForm.elements[i];
		if (el.type == "text" || el.type == "select-one" || el.type == "hidden") {

			if (el.value != "") {
				this.addPostVar( el.name, el.value );
			}

		} else if (el.type == "radio" || el.type == "checkbox") {

			if (el.checked) {
				this.addPostVar( el.name, el.value );
			}

		}  else if (el.type == "textarea") {

			if (el.value != "") {
				this.addPostVar( el.name, el.value );
			}

		}
		//alert(oForm.elements[i].type+" : "+oForm.elements[i].value);

	}

	return oForm.action;

}

ajaxRequest.prototype.addPostVar = function( name, value ) {

	if (!this.postVars[ name ]) {
		this.postVars[ name ] = value;
	} else if (typeof (this.postVars[ name ]) == "string") {
		var tmpValue = this.postVars[ name ];
		this.postVars[ name ] = new Array();
		this.postVars[ name ][ 0 ] = tmpValue;
		this.postVars[ name ][ 1 ] = value;
	} else {
		this.postVats[ name ][ this.postVars[ name ].length ] = value;
	}

}

ajaxRequest.prototype.getPostString = function() {

	var str = "";
	for ( var i in this.postVars ) {
		if (typeof (this.postVars[i]) == "object") {
			for (var j = 0; j < this.postVars[i].length; j++) {
				str += encodeURI(i)+"="+encodeURI( this.postVars[i][j] )+"&";
								//	^ no need here [] (array squares). it will be here by itself.
			}
		} else {
			str += encodeURI(i)+"="+encodeURI(this.postVars[i])+"&";
		}
	}

	return str;

}

ajaxObj = new ajaxRequest();

/*### Simple Function ###*/

function ajaxPostForm( formName, feedback ) {

	ajaxObj = new ajaxRequest();
	ajaxObj.feedback = feedback;
	url = ajaxObj.parseForm( formName );
	ajaxObj.process(url, "POST");
	//alert(formName);

	return false;

}