function newurl(url) {
	
	document.location.href=url;

}

function newwindow(serverurl, request, title, width, height, posX, posY, location, menubar, toolbar, scrollbar, resize, status) {
/*	receives the following parameters:
	(mandatory) string server, string url 
	(optional)  [, string title, int width, int height, 
				int/string X position, int/string Y position,
				boolean location, boolean menubar, boolean toolbar, boolean scrollbar, 
				boolean resizable, boolean status ]
	(note : X position and Y position can be string center)
	
	! beware that IExplorer needs a title without any space
*/
	var x,y;
	var threshold = (document.all)? -100 : 200; //compensate menubar and toolbar in Mozilla
	if (self.screenX) {// all except Explorer, Safari, Opera
		x = self.screenX;
		y = self.screenY;
	} else if (self.screenLeft) { // all other	
		x = self.screenLeft;
		y = self.screenTop;
	}
	if(typeof(posX) == "string") {
		if(posX == "center") posX = x + (document.body.offsetWidth - ((width)? width : 800)) / 2;
		else posX = 800;
	}
	if(typeof(posY) == "string") {
		if(posY == "middle") posY = y + (document.body.offsetHeight - ((height)? height - threshold : 400)) / 2;
		else posY = 600;
	}
	
	var link = serverurl + '?window=new&' + request;		
	newWindow = window.open(link, ((title)? title : "detail"),"width=" + ((width)? width : 800) + ",height=" + ((height)? height : 600) + ",left=" + ((posX)? posX : 100) + ",top=" + ((posY)? posY : 100) + ",location=" + ((location)? location : "no") + ",menubar=" + ((menubar)? menubar : "no") + ",directories=no,toolbar=" + ((toolbar)? toolbar : "no") + ",scrollbars=" + ((scrollbar)? scrollbar : "yes") + ",resizable=" + ((resize)? resize : "yes") + ",status=" + ((status)? status : "no"), true);
	if (newWindow) newWindow.focus();

} 

/**
 *	Modify specified get var in url and return new url name
 *
 * @return string modified url
 */
function changeUrl(url, name, value) {


	var getname = name + "=";
	var namelength = name.length;
	
	var pos=url.indexOf(getname);
	
	var ln_pos 	= url.indexOf('#');
	var ln 		= '';
	
	if (ln_pos > 0 ){

		ln	= url.substr(ln_pos, url.length);	
		url = url.substr(0, ln_pos);
		
	}
	
	
	var newurl='';
	
	if (pos>=0) {
	
		var before = url.substr(0, pos);
		var tmp  = url.substr(namelength+pos+1, url.length);
		
		var pos2 = tmp.indexOf('&');
		
		if (pos2 >= 0) {
		
			var after = url.substr(pos2+pos+namelength+1, url.length);
			
		} else {
		
			var after = '';
		}
		
		
		newurl = before + getname + value + after + ln;
		
	} else {

		
		var glue='?';
		if (url.indexOf('?') > 0) glue = '&';

		
		newurl = url + glue + getname + value + ln;
			
	}

	return newurl;

}

/**
 *	Set (or change) the specified get var in current url 
 *  and load resulting url
 */
function setGetVar(name, value) {

	document.location.href=changeUrl(document.location.href, name, value); 
	
}


function setLanguage(id_lang) {
	setGetVar('id_lang',id_lang); 
}

function gotoPage(id_page, args) {
	if (!args) {
		setGetVar ('id_page',id_page);
	} else {
		var gets = args.split("&");
		var url = document.location.href;
		url = changeUrl(url, 'id_page', id_page); 
		for(var idx in gets) {
			var v = gets[idx];
			var gs = v.split('=');
			url = changeUrl(url, gs[0], gs[1]); 
		}
		
		document.location.href = url;
	}
}

function gotoURL(url) {

	if(url.indexOf('http://') == -1)
		newurl('http://' + url);
	else newurl(url);

}

function showComponent(id) {
	var obj = document.getElementById("id"+id);
	if (obj) {
		obj.style.visibility = 'visible';
	}
}

function hideComponent(id) {
	var obj = document.getElementById("id"+id);
	if (obj) {
		//alert(obj.style.display);
		obj.style.visibility = 'hidden';
	}
}

function downloadMedia(id_media) {
	newwindow('./downloadMedia.php?id='+id_media);	
}

function setImage(mediaId, id_c) {
	var obj = document.getElementById("id"+id_c);
	if (obj) obj.style.backgroundImage = "url(requestMedia.php?id="+mediaId+")";
}

function changeStyle(obj, property, value) {

	obj.style.backgroundColor = 'yellow';

}

var ShopCart = {
	
	addToCart: function(idShopcart, idArticle) {
		//alert('addToCart:'+idArticle);
		
		var useRemote = true;
		if (useRemote) {
			Remote.callMethod('ShopCart.addProductToCart', {shopcartId: idShopcart, productId:idArticle, callback:'ShopCart.addToCartResponse'});
		} else {
			var form = document.forms.ShopCartForm;
			form.formAction.value = 'addProduct';
			form.id.value = idArticle;
			form.submit();
		}
	},
	addToCartResponse: function(data) {
		if (data.result==true) {
			// display confirm
			alert(data.msg);
		}
	},
	removeItem: function(idShopcart, idArticle) {
		//alert('Removing:'+idArticle);
		var form = document.forms.ShopCartForm;
		form.formAction.value = 'removeProduct';
		form.id.value = idArticle;
		form.submit();
	},
	setQty: function(idShopcart, idArticle, qty) {
		//alert('SetQty:'+idArticle+", "+qty);
		var form = document.forms.ShopCartForm;
		form.formAction.value = 'setProductQty';
		form.id.value = idArticle;
		form.params.value = qty;
		form.submit();
	},
	gotoNextPage: function(idShopcart) {
		var form = document.forms.ShopCartForm;
		form.formAction.value = 'gotoNextPage';
		form.submit();
	},
	gotoPreviousPage: function(idShopcart) {
		var form = document.forms.ShopCartForm;
		form.formAction.value = 'gotoPreviousPage';
		form.submit();
	},
	gotoOgonePage: function(formId) {
		var form = document.getElementById(formId);
		form.submit();
	
	},
	
	getDeliveryFee: function(countryId) {
		Remote.callMethod('ShopCart.getDeliveryFee', {countryId:countryId, callback:'ShopCart.receiveDeliveryFee'});
	},
	
	receiveDeliveryFee: function(v) {
		var f = document.getElementById('shipping_costs');
		f.value = v;
	}
	
}

var Form = {

	submitAction: function(obj, action, redirect) {
		var f = obj.form;
		if (!f) {
		 	// assume f is form name
		 	f = document.forms[obj];
		 	if (!f) alert("Undefined form '"+obj+"'");
		}
		f.formAction.value = action;
		
		if (f.formRedirect) {
			if (redirect != undefined && redirect!='') f.formRedirect.value= redirect;
		}
		f.submit();
	}
}

function addClass(obj, name) {
	if (obj.className.search(name)<0) obj.className += ' '+name;
}

function removeClass(obj, name) {
	obj.className = obj.className.replace(name,'');
}

var Remote = {

	callMethod: function(m, args) {

		if (typeof window.ActiveXObject != 'undefined' ) {
			xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		    xmlDoc.onreadystatechange = Remote.callMethod_result ;
		}
        else {
	        xmlDoc = new XMLHttpRequest();
	        xmlDoc.onload =  Remote.callMethod_result ;
		}

		var queryString = Remote.buildQueryString(args);

		xmlDoc.open( "GET", "remoting/index.php?m=" + m + queryString, true );
		xmlDoc.send("");
	},

	buildQueryString: function(args) {
		var txt = "";
		for(var k in args) {
			var v = args[k]; 
			if (k=='value') k='v';
			if (k=='callback') k='cb';
			
			txt += "&" + k + "=" + v;
		}
		return txt;
	},

	callMethod_result: function() {

		if ( xmlDoc.readyState != 4 ) return ;
        
        var res = xmlDoc.responseText;
    
    	// text to js
    	var res = eval('(' + res + ')');

		if (res.result=='Exception') {
			alert("Remoting Exception: "+res.data);
			return;
		}
    	
    	var cb=res.callback;

       	if (cb) {
	       	var data = res.data;
    	   	eval(cb+'(data)');
       	} else {
       		alert('no callback specified');
		}       	
       	
	}

}
