/****/

function parseJSON(json)
{
	try {
		if(JSON)
			return JSON.parse(json);
		else
			return {};
	} catch(e) {}
}

function ajax(url, callback, data)
{
	var method = data ? 'POST' : 'GET';
	var httpRequest;
	
	try {
		if(window.XMLHttpRequest) {
			httpRequest = new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try{
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) { }
			}
		}

		if(httpRequest) {
			httpRequest.onreadystatechange = function() {
				if(this.readyState == 4 && this.status == 200) {
					callback(parseJSON(this.responseText));
				}
			};
		
			httpRequest.open(method, url, true);
			
			if(method == 'POST')
				httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
			
			httpRequest.send(data);
		}
	} catch (e) { 
		alert(e) 
	}
}


function imgNavigation(imgObj, titleObj, descObj, basePath){
	this.pictures = [];
	this.basePath = basePath;
	this.pointer = 0;
	this.imgObj = imgObj;
	this.titleObj = titleObj;
	this.descObj = descObj;
	this.loopingDrive = true;

	this.next = function(){
		if(!this.pictures.length){
			//alert("לא נמצאו תמונות");
			return false;
		}

		if(this.pointer+1 == this.pictures.length){
			if(this.loopingDrive)
				this.showImg(0);
			else{
				//alert("אין תמונות נוספות בגלריה זו, לחץ/י על \"הקודם\" על מנת לעבור חזרה לתמונה הקודמת.");
				return false;
			}
		}else
			this.showImg(this.pointer+1);
	}

	this.previous = function(){
		if(!this.pictures.length){
			alert("לא נמצאו תמונות");
			return false;
		}

		if(this.pointer == 0){
			if(this.loopingDrive)
				this.showImg(this.pictures.length-1);
			else{
				//alert("הנך צופה בתמונה הראשונה שבגלריה זו, לחץ/י על \"הבא\" על מנת לעבור לתמונה הבאה.");
				return false;
			}
		}else
			this.showImg(this.pointer-1);
	}

	this.showImg = function(picIndex){
		this.pointer = picIndex;

		this.imgObj.src = this.basePath + this.pictures[picIndex].file;
		this.titleObj.innerHTML = this.pictures[picIndex].title;
		this.descObj.innerHTML = this.pictures[picIndex].desc;
	}

	this.addPic = function(fileName, title, description){
		this.pictures.push({file: fileName, title: title, desc: description});
	}
}


function formValidation(formId, config)
{
	this.config = config[formId];
	
	this.check = function() {
		this.form = $(formId)
		
		if(!this.config)
			return true;
		
		for(var key in this.config) {
			for(var key2 in this.config[key]) {
				var action = this[key2];
				
				if(action && !action($(key), this.config[key][key2][1])) {
					alert(this.config[key][key2][0])
					$(key).focus();
					return false;
				}
			}
		}
	}
	
	this.empty = function(element) {
		return !element.value ? false : true;
	}
	
	this.length = function(element, length) {
		return element.value.length < length ? false : true;
	}
	
	this.match = function(element, passId) {
		return element.value != $(passId).value ? false : true;
	}
	
	this.email = function(element) {
		var regex = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		return regex.test(element.value);
	}
	
	this.atLeast = function(element, otherElementId) {
		var e2 = $(otherElementId);
		return element.value || e2.value ? true : false;
	}
}

function validateForm(formId, validationConfig)
{
	var validationObj = new formValidation(formId, validationConfig);
	
	return validationObj.check();
}

/****/

var baseUrl = "";

if(location.host == "localhost") {
	baseUrl = "/lj/projects/gancenter/web";
}

/* EOF */

