var bUrl
var car, usr

function init(base_uri){
  bUrl = base_uri
  
  car = new Carrello()
  usr = new User()
  car.loadCarr()
}

var cngPageArea
var cngPageFlash
function cngPage(href, area, flash, clearTop){
  if(clearTop != false) $('areaTop').innerHTML = ""
  cngPageArea = area
  cngPageFlash = flash
  href = href+"/_area_"+area
  href = href.replace("//", "/")
  href = href.replace("http:/", "http://")
  
  return ajUpdater(href, area, cngPageDone)
}

function cngPageDone(){
  if(cngPageFlash == undefined || cngPageFlash == false) return
  cngPage(bUrl+'configuratore.php/configuratore/flash/'+URLEncode(cngPageFlash), 'areaFlash', false, false)
}

function ajRequest(url, onCompleteFunction, pBody) {
  if(pBody == undefined) pBody = "";
  var myAjax = new Ajax.Request(
    url,
	{
	  onComplete: onCompleteFunction,
	  method: "post",
	  asynchronous: false,
	  evalScripts: true,
	  postBody: pBody
	}
  );
	
  return false;
}

function ajUpdater(url, element, onCompleteFunction, pBody){
  if(pBody == undefined) pBody = "";
  var myAjax = new Ajax.Updater(
    element,
	url,
	{
	  onComplete: onCompleteFunction,
	  method: "post",
	  asynchronous: false,
	  evalScripts: true,
	  postBody: pBody
	}
  );
	
  return false;
}

function date(){
  var my_date = new Date()
  
  return my_date.toString()
}

Array.prototype.merge = function(){
  for(var i = 0; i < arguments.length; i++)
    for(var property in arguments[i])
	  if(property*1 >= 0 && property*1 == property) this[this.length] = arguments[i][property]
	  else this[property] = arguments[i][property]

  return true
}

Array.prototype.kCopy = function(){
  var tmpArr = new Array()
  for(var key in this) tmpArr[key] = this[key]

  return tmpArr
}

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function URLEncode(str){
  // The Javascript escape and unescape functions do not correspond
  // with what browsers actually do...
  var SAFECHARS = "0123456789" +					// Numeric
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
				  "abcdefghijklmnopqrstuvwxyz" +
				  "-_.!~*'()";					// RFC2396 Mark characters
  var HEX = "0123456789ABCDEF";

  var plaintext = str; //document.URLForm.F1.value;
  var encoded = "";
  for(var i = 0; i < plaintext.length; i++){
    var ch = plaintext.charAt(i);
	if(ch == " "){
	  //encoded += "+";				// x-www-urlencoded, rather than %20
	  encoded += "%20";
	} else if (SAFECHARS.indexOf(ch) != -1){
	  encoded += ch;
	} else{
	  var charCode = ch.charCodeAt(0);
	  if (charCode > 255){
	    alert( "Unicode Character '" +
		       ch +
			   "' cannot be encoded using standard URL encoding.\n" +
			   "(URL encoding only supports 8-bit characters.)\n" +
			   "A space (+) will be substituted." );
		encoded += "+";
	  } else{
	    encoded += "%";
		encoded += HEX.charAt((charCode >> 4) & 0xF);
		encoded += HEX.charAt(charCode & 0xF);
	  }
	}
  } // for

  //document.URLForm.F2.value = encoded;
  //document.URLForm.F2.select();
  //return false;

  return encoded;
}

function URLDecode(str){
  // Replace + with ' '
  // Replace %xx with equivalent character
  // Put [ERROR] in output if %xx is invalid.
  var HEXCHARS = "0123456789ABCDEFabcdef"; 
  var encoded = str; //document.URLForm.F2.value;
  var plaintext = "";
  var i = 0;
  while(i < encoded.length){
    var ch = encoded.charAt(i);
	if(ch == "+"){
	  plaintext += " ";
	  i++;
	} else if(ch == "%"){
	  if(i < (encoded.length-2) &&
	     HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 &&
		 HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ){
		plaintext += unescape( encoded.substr(i,3) );
		i += 3;
	  } else{
	    alert( 'Bad escape combination near ...' + encoded.substr(i) );
		plaintext += "%[ERROR]";
		i++;
	  }
	} else{
	  plaintext += ch;
	  i++;
	}
  } // while

  //document.URLForm.F1.value = plaintext;
  //document.URLForm.F1.select();
  //return false;
  
  return plaintext;
}

function URL2Array(str){
  var arr = new Array()
  var couples = str.split("&")
  
  for(var x = 0; x < couples.length; x++){
    var key_val = couples[x].split("=")
	if(key_val.length == 2) arr[URLDecode(key_val[0])] = URLDecode(key_val[1])
  }
  
  return arr
}

/* -----------------------------------------------------------------------------------
------- Trim(), LTrim(), RTrim() -----------------------------------------------------
-------
------- Metodi per l'oggetto String, restituiscono la stringa cui sono applicati
------- senza spazi iniziali e/o finali:
-------
------- str_a = stringa.Trim();
------- str_a contiene il valore di stringa senza spazi iniziali ne' finali
-------
------- str_a = stringa.LTrim();
------- str_a contiene il valore di stringa senza spazi iniziali
-------
------- str_a = stringa.RTrim();
------- str_a contiene il valore di stringa senza spazi finali
-------
------- N.B.
------- [\s] nelle RegExp contiene sia gli spazi che i ritorni a capo, avanzamento riga
------- tabulatore, tabulatore verticale. Tutti questi caratteri, se presenti, verranno
------- eliminati.
------- --- */
function Trim() {
return this.replace(/\s+$|^\s+/g,"");
}

function LTrim() {
return this.replace(/^\s+/,"");
}

function RTrim() {
return this.replace(/\s+$/,"");
}
   
String.prototype.Trim=Trim;   
String.prototype.RTrim=RTrim;   
String.prototype.LTrim=LTrim;   
/* ----------------------------------------------------------------------------------- */

function doNothing(){
}

function roundTo(value, decimalpositions){
  var i = value * Math.pow(10,decimalpositions);
  i = Math.round(i);
  return i / Math.pow(10,decimalpositions);
}