VISISHOP = new visishop();
var VAJX_PHP_URL='/visishop/public/ajx/visishop.ajx.php';
var VMEDIA_PATH = '/visishop/mediapool/';

// console est la pour emuler le console de firebug sous ie
if (!("console" in window) || !("firebug" in console)) 
{ 
	window.console =new visiConsole;
	//alert('Console = visiConsole');
} 

function _anythingInString(objet,prof)
{
	if (prof>50) return "...trop profond";
	prof++;
	switch(typeof(objet))
	{
	case 'string':
		return 'string('+objet.length+'): "'+objet+'"';
	
	case 'number':
		if(objet>=0 && String(objet).indexOf('.') == -1 && objet < 65536) return 'integer: '+objet;
		return 'decimal: '+objet;
	
	case 'boolean':
		return 'boolean: '+( (objet)?'1':'0' );
	
	case 'object':
		var i=0,prop,ret='',tab='\t';
		for (var it=0; it<prof; it++) { tab+="\t"; }
		for (var prop in objet)
		{
			//if(!isNaN(k))// { k = Number(k); }
				var p_txt = _anythingInString(prop,prof);
				var o_txt = "non defini";
				if (objet[prop])
				{
					o_txt = _anythingInString(objet[prop],prof);
				}
				ret += tab+p_txt+" = "+o_txt+"\n";
				i++;
		}
		return 'array['+i+']:\n'+ret+'\n';
	
	default:
		return 'type undefined\n';
	}
}

/*function _isIE()
{
	return navigator.userAgent.indexOf('MSIE') != -1;
	
}*/





function __unserialize(txt)
{
	var level=0;
	var arrlen=new Array();
	var del=0;
	var vFinal = new Array();
	var key=new Array();
	var save=txt;
	
	while(1)
	{
		switch(txt.substr(0,1)){
		case 'N':
			del = 2;
			ret = null;
		break;
		case 'b':
			del = txt.indexOf(';')+1;
			ret = (txt.substring(2,del-1) == '1')?true:false;
		break;
		case 'i':
			del = txt.indexOf(';')+1;
			ret = Number(txt.substring(2,del-1));
		break;
		case 'd':
			del = txt.indexOf(';')+1;
			ret = Number(txt.substring(2,del-1));
		break;
		case 's':
			del = txt.substr(2,txt.substr(2).indexOf(':'));
			ret = txt.substr( 1+txt.indexOf('"'),del);
			del = txt.indexOf('"')+ 1 + ret.length + 2;
		break;
		case 'a':
			del = txt.indexOf(':{')+2;
			ret = new Array();
			arrlen[level+1] = Number(txt.substring(txt.indexOf(':')+1, del-2))*2;
		break;
		case 'O':
			txt = txt.substr(2);
			var tmp = txt.indexOf(':"')+2;
			var nlen = Number(txt.substring(0, txt.indexOf(':')));
			name = txt.substring(tmp, tmp+nlen );
			txt = txt.substring(tmp+nlen+2);
			del = txt.indexOf(':{')+2;
			ret = new Object();
			arrlen[level+1] = Number(txt.substring(0, del-2))*2;
		break;
		case '}':
			txt = txt.substr(1);
			if(arrlen[level] != 0){alert('var missed : '+save); return undefined;};
			level--;
		continue;
		default:
			if(level==0) return vFinal;
			alert('syntax invalid(1) : '+save+"\nat\n"+txt+"level is at "+level);
			return undefined;
		}
		if(arrlen[level]%2 == 0){
			if(typeof(ret) == 'object'){alert('array index object no accepted : '+save);return undefined;}
			if(ret == undefined){alert('syntax invalid(2) : '+save);return undefined;}
			key[level] = ret;
		} else {
			var ev = '';
			for(var i=1;i<=level;i++){
				if(typeof(key[i]) == 'number'){
					ev += '['+key[i]+']';
				}else{
					ev += '["'+key[i]+'"]';
				}
			}
			eval('vFinal'+ev+'= ret;');
		}
		arrlen[level]--;//alert(arrlen[level]-1);
		if(typeof(ret) == 'object') level++;
		txt = txt.substr(del);
		continue;
	}
}

function __serialize($aData) 
{
	switch(typeof($aData))
	{
		case 'string':
				//return 's:'+$aData.length+':"'+$aData+'";';
				return 's:'+$aData.length+':"'+$aData+'";';
		case 'number':
				if($aData>=0 && String($aData).indexOf('.') == -1 && $aData < 65536) 
					return 'i:'+$aData+';';
				if (isNaN($aData)) $aData = 0; //sinon bug unserialize php
				return 'd:'+$aData+';';
		case 'boolean':
				return 'b:'+( ($aData)?'1':'0' )+';';
		case 'object':
				var $vI=0,$vK,$vResult='';
				for($vK in $aData)
				{
					if(!isNaN($vK)) 
					{ $vK = Number($vK); }
					$vResult += __serialize($vK)+__serialize($aData[$vK]);
					$vI++;
				}
				return 'a:'+$vI+':{'+$vResult+'}';
		default:
				return 'N;';
	}
}

function visiConsole()
{
	var t= this;
	//window.onerror = function(m,h,l){alert(m+"\n"+h+"\n"+l);};
}

visiConsole.prototype.debug = function (aArgs)
{ this.log(aArgs);}
visiConsole.prototype.info = function (aArgs)
{ this.log(aArgs);}
visiConsole.prototype.warn = function (aArgs)
{ this.log(aArgs);}
visiConsole.prototype.error = function (aArgs)
{ this.log(aArgs);}
visiConsole.prototype.log = function (aArgs)
{ 
	alert(this.log.caller+"\n"+aArgs);
}





////////////////////////////////////////////////////////////////////////////////
function visiAjax()
{
	this.datas = "";
}

////////////////////////////////////////////////////////////////////////////////
visiAjax.prototype.get_xobject = function()
{
	if(window.XMLHttpRequest) // FIREFOX
    { return new XMLHttpRequest(); }
	else if(window.ActiveXObject) // IE
    { return new ActiveXObject("Microsoft.XMLHTTP"); }
    else return(false);
}

////////////////////////////////////////////////////////////////////////////////
visiAjax.prototype.sendAsyPostRequest = function()
{
	var $vXo=this.get_xobject();

	if ($vXo)
	{
		$vXo.open('POST', VAJX_PHP_URL, false);
		$vXo.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		$vXo.send(this.datas); // Format -> var1=truc&var2=bidule
     	if( $vXo.readyState == 4) 
		{ return( $vXo.responseText ); }
		else return(false);
	}
	return false;
}

////////////////////////////////////////////////////////////////////////////////
visiAjax.prototype.sendPostRequestAsy = function(aCallBackFnct)
{
	var vXo=this.get_xobject();

	if (vXo)
	{
		vXo.open('POST', VAJX_PHP_URL, true);
		vXo.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		vXo.onreadystatechange  = function()
    	{ 
         	if(vXo.readyState  == 4)
         	{
              if(vXo.status  == 200)
              { 
                 aCallBackFnct(vXo.responseText);
              } 
              else
              { 
                 alert("AJAX Error code " + xhr.status);
              }
         	}
    	}; 
		
		vXo.send(this.datas); // Format -> var1=truc&var2=bidule
		return true;
	}
	return false;
}

////////////////////////////////////////////////////////////////////////////////
visiAjax.prototype.call_function = function()
{
	var $vAjxObject = new Object();
	$vFuncName = arguments[0];
	$vParams = new Array();
	for (var $vI = 1; $vI < arguments.length; $vI++) 
	{
      $vParams[$vI-1] = arguments[$vI];
   	}
	   	
   	var $vSerial = encodeURIComponent(__serialize($vParams));
   	this.datas = "f="+$vFuncName+"&p="+$vSerial;
   	var $vResult = this.sendAsyPostRequest();
   	return $vResult;
	
}

////////////////////////////////////////////////////////////////////////////////
// Arg[0] = callBack function
// Arg[1] = Called VisiShop Ajax function name
visiAjax.prototype.callas = function()
{
	var vPrm = new Array();
	for (var vI = 2; vI < arguments.length; vI++) 
	{ vPrm[vI-2] = arguments[vI]; }
	var vSer = __serialize(vPrm);	
   	var vSer = encodeURIComponent(vSer);
   	this.datas = "f="+arguments[1]+"&p="+vSer;
   	return this.sendPostRequestAsy(arguments[0]);
}

////////////////////////////////////////////////////////////////////////////////
visiAjax.prototype.callsy = function()
{
	var vPrm = new Array();
	for (var vI = 1; vI < arguments.length; vI++) 
	{ vPrm[vI-1] = arguments[vI]; }
	var vSer = __serialize(vPrm);	
   	var vSer = encodeURIComponent(vSer);
   	this.datas = "f="+arguments[0]+"&p="+vSer;
   	var vR = this.sendAsyPostRequest();
   	return __unserialize(vR);
}


function visiCart()
{
	
}

visiCart.prototype.add = function(aRef,aType,aQ)
{
	
}

visiCart.prototype.submitForm = function(aForm,aRef,aType,aQ)
{
	return true;
}

//------------------------------------------------------------------------------
function visishop()
{
	if (typeof(VISICORE_loaded)=='undefined') alert("la librairie visicore.js n'est pas chargée!\n"+window.location);
	var t = this, d = document, w = window, na = navigator, ua = na.userAgent;
	//t.forms = new visiforms;
	t.ajax = new visiAjax;
	t.cart = new visiCart;
	//t.isOpera = w.opera && opera.buildNumber;
	//t.isWebKit = /WebKit/.test(ua);
	//t.isOldWebKit = t.isWebKit && !w.getSelection().getRangeAt;
	t.isIE = !t.isWebKit && !t.isOpera && (/MSIE/gi).test(ua) && (/Explorer/gi).test(na.appName);
	//t.isIE6 = t.isIE && /MSIE [56]/.test(ua);
	//t.isGecko = !t.isWebKit && /Gecko/.test(ua);
	//t.isMac = ua.indexOf('Mac') != -1;
}
visishop.prototype.opener = function()
{
	return window.dialogArguments || opener || parent || top;
}

visishop.prototype.unserialize = function (txt)
{
	return __unserialize(txt);
}

visishop.prototype.serialize = function ($aData) 
{
	return __serialize($aData);
}

visishop.prototype.clone = function (aData) 
{
	return __unserialize(__serialize(aData));
}

visishop.prototype.mysqlDateToFrenchShortDateTime = function($aMDate)
{
	$vRE = /([0-9]+)-([0-9]+)-([0-9]+)\s([0-9]+):([0-9]+):([0-9]+)/;
 	return $aMDate.replace($vRE, "$3/$2/$1 $4h$5");
}

visishop.prototype.debug =  function (txt)
{
	var r="";
	r = _anythingInString(txt,0);
	console.debug(r);
}
visishop.prototype.open_modal = function (aUrl,aW,aH,aName)
{
	var vA;
	try 
	{
		if (this.isIE)
		{
			vA = "dialogHeight:"+aH+"px; dialogWidth:"+aW+"px;"
			return window.showModalDialog(aUrl, window, vA);
		} 
		else
		{ 
			vA = 'width='+aW+',height='+aH+',resizable=yes,scrollbars=no';
			return window.open(aUrl,aName,vA);
		}
	} 
	catch (ex) 
	{
				// Ignore
	}
}

visishop.prototype.open_win = function (aUrl,aW,aH,aName,aResize,aScroll,aToolbar,aLocation,aStatus,aMenu)
{
	var carac="";
	if (aToolbar) carac+=',toolbar=yes';
	if (aLocation) carac+=',location=yes';
	if (aStatus) carac+=',status=yes';
	if (aMenu) carac+=',menubar=yes';
	if (aScroll) carac+=',scrollbars=yes';
	if (aResize) carac+=',resizable=yes';
	carac = 'width='+aW+',height='+aH+carac;
	return window.open(aUrl,aName,carac);
}

visishop.prototype.load_file = function ( aFilename )
{
    vXObject=this.get_xobject();
	if (vXObject)
	{
     	vXObject.open("GET", aFilename, false);
     	vXObject.send(null);
     	if( vXObject.readyState == 4) 
		{ return( vXObject.responseText ); }
		else return(false);
	}
	else 
	{
		//alert ('Objet XMLHttpRequest non assign�!');
		return(false); 
	}
}

visishop.prototype.div_load = function (aDivID,aFilename,aShow)
{
 	var vDiv = document.getElementById(aDivID);
 	if (vDiv)
 	{
 		this.div_write(aDivID,'Chargement en cours...');
		var vText=this.load_file(aFilename);
		this.div_write(aDivID,vText);
		if (aShow)
		{ this.show(aDivID); }
 	}
}


visishop.prototype.get_xobject = function ()
{
	if(window.XMLHttpRequest) // FIREFOX
    { return new XMLHttpRequest(); }
	else if(window.ActiveXObject) // IE
    { return new ActiveXObject("Microsoft.XMLHTTP"); }
    else return(false);
}

visishop.prototype.div_write = function ( aDivID , aText )
{
	if ( document.getElementById(aDivID) )
	{ document.getElementById(aDivID).innerHTML = aText; }
	else alert('Element '+aDivID+' non trouvé !');
}

visishop.prototype.show = function (ID)
{
	var d = getEl(ID);
	if (d) d.style.display = ''; 
}

visishop.prototype.hide = function (ID)
{
	var d = getEl(ID);
	if (d) d.style.display = 'none'; 
}

visishop.prototype.navigate = function (aUrl,aTarget)
{
	//location.href = aUrl;
	window.open(aUrl,aTarget,'');
}
visishop.prototype.expandObj = function(aEl,aBtn,ImgExp,ImgCol,CollapseIfExpanded)
{
	var el = getEl(aEl);
	var btn = getEl(aBtn);
	if ( el.style.display != 'none' && CollapseIfExpanded ) 
	{
		el.style.display = 'none';	
		if (ImgCol) btn.src = ImgCol;
	}
	else 
	{
		el.style.display = '';
		if (ImgExp) btn.src = ImgExp;
	}
}

visishop.prototype.confirmPageExit = function (aTxt)
{
	$CPE_TEXT = aTxt;
	addEvent(document,'click',function(){return confirm($CPE_TEXT);},false);
}

visishop.prototype.removeConfirmPageExit = function()
{
	removeEvent(document,'click',function(){return confirm($CPE_TEXT);},false);
}



function format_UPPER(s)
{ return s.toUpperCase(); }

function format_REF(s)
{
 	s=s.toUpperCase();
 	s=s.replace(/[^0-9,A-Z_]/g,'-');
 	s=s.replace(/-+/g,'-');
 	return s;	
}

function format_INT(s)
{
 	return s.replace(/[^0-9]/g,'');
}
function format_FLOAT(s)
{
	s=s.replace(/[^0-9.,]/g,'');
	s=s.replace(/[,.]+/g,'.');
	return s;
}




