
function openUrl(url) {
	window.location = url;	
}

function ifEnterSubmit(e, form) {
	
	if 	(getKeyCode(e)==13) {
		form.submit();	
	}
}

function getKeyCode(e) {

  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    var s = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    var s = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    var s = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  return s;
	
}

function toogleMenu() {
	ddiv =	document.getElementById('menufla');
	dimg =	document.getElementById('menuimg');
	if (ddiv) {
		if (ddiv.style.display == 'none' || ddiv.style.display == '') {
			ddiv.style.display = 'block';
			if (dimg) dimg.src = dimg.src.split('.gif').join('_.gif');
		}
		else {
			ddiv.style.display = 'none';
			if (dimg) dimg.src = dimg.src.split('_.gif').join('.gif');
		}
	}
};

var timeOut;
function showMenu() {
	var ddiv =	document.getElementById('menufla');
	var dimg =	document.getElementById('menuimg');
	ddiv.style.display = 'block';
	if (dimg) dimg.src = dimg.src.split('xxo.gif').join('xxo_.gif');
	waitToHide();
		
};

function waitToHide() {
	clearTimeout(timeOut);
	timeOut = setTimeout(hideMenu, 3000);
	
}

function enterMenu() {
	clearTimeout(timeOut);
}
function outMenu() {
	timeOut = setTimeout(hideMenu, 1000);
}
function hideMenu() {
	
	clearTimeout(timeOut);
	
	var ddiv =	document.getElementById('menufla');
	var dimg =	document.getElementById('menuimg');
	ddiv.style.display = 'none';
	if (dimg) dimg.src = dimg.src.split('xxo_.gif').join('xxo.gif');
		
};



function resize( width, height, posx, posy){
	window.focus();
	if (posx=='') posx=100;
	if (posy=='') posy=50;
	window.moveTo(posx, posy)
	window.resizeTo(width, height)
	
}

/* FORM */
  /* select a select option by value */
function rwnFormSelectSelByValue(id, value) {
  	
	if (el = document.getElementById(id) ) {
		if (el.options)
		{
			for (num=0; num<el.options.length; num++) {
				if (el.options[num].value == value) {
				
					el.options[num].selected = 1;
				}
			}
		}
		else {
			if(value == '1')
			{
				el.checked=true;
			}
			else
			{
				el.checked=false;
			}
		}
	}
}




function display(id) {
	dnxt =	document.getElementById('newsNext'+id);
	detc =	document.getElementById('newsEtc'+id);
	dimg =	document.getElementById('puce'+id);
	if (dnxt) {
		if (dnxt.style.display == 'none' || dnxt.style.display == '') {
			dnxt.style.display = 'block';
			if (detc)	detc.style.display = 'none';
			if (dimg) dimg.src = dimg.src.split('.gif').join('_.gif');
		}
		else {
			dnxt.style.display = 'none';
			if (detc) detc.style.display = 'block';
			if (dimg) dimg.src = dimg.src.split('_.gif').join('.gif');
		}
	}
};

var crediTimeout;
function showCredit () {
	
	changeClassName('credit','on');
	clearTimeout(crediTimeout);
	crediTimeout = setTimeout("changeClassName('credit','')",7000);
}

function changeClassName(id, classname) {

	Element.removeClassName(id, Element.classNames(id));
	Element.addClassName(id, classname);
	
}
function openForum() {
	window.open("http://www.wearephoenix.com/forum/","Forum","height=680,width=850,status=no,toolbar=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes");
};

function ajaxUpdateFunc(){
	//alert(this.settings.zoneid);
	if (this.settings.divid) $(this.settings.divid).innerHTML = this.XMLobject.responseText;
	
}

function showPhoto(number) {
	for (var i=1; i<=4; i++) {
	
		if (elmt = document.getElementById('photo'+i) ) {
			
			if (i==number) elmt.style.display = 'block';
			else elmt.style.display = 'none';
		}
		if (elmt = document.getElementById('thumb'+i) ) {
			
			if (i==number) elmt.className = 'sel';
			else elmt.className = '';
		}
		
	}
}

function over(strId) {
	if (elmt=document.getElementById(strId)) {
		elmt.className = 'zoom_';	
	}
	
}

function out(strId) {
	if (elmt=document.getElementById(strId)) {
		elmt.className = 'zoom';	
	}	
}
/*
- - - - - - - - - -
COMMANDE
Require prototype.js
- - - - - - - - - -
*/
var currentRef;
var cadAddLoaded = function(t) {
	
	//arr = querystringToArray(t.responseText);
	var a = t.responseText.parseJSON();
	
	var objectId = a['userWish']['id'];
	var type = a['type'];
	
	if (a['content'+type][objectId]!=undefined)	var nb = a['content'+type][objectId]['nb'];
	else var nb = 0;
	
	if (nb>0) {
		changeClassName('bt_'+objectId, 'btajt_');
		
		Element.addClassName('barrePage', 'active');
		Element.addClassName('barrePage2', 'active');
	}
	else {
		
		changeClassName('bt_'+objectId, 'btajt');
		
		if (a['empty']=='true') {
			
			Element.removeClassName('barrePage', 'active');
			Element.removeClassName('barrePage2', 'active');
		}
	}
	
	document.getElementById('inp_'+objectId).value = nb;
	
	if (a['msg']==undefined) {
		if (nb==0)  a['msg'] = new Array('validation','Le produit a bien été retiré de votre sélection.');
		else a['msg'] = new Array('validation','Le produit a bien été ajouté à votre sélection.');
	}
	displayMessage('msg', a['msg']);
	
	// alert(t.responseText);
}

var errFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

var cadDelLoaded = function(t) {
	
	//arr = querystringToArray(t.responseText);
	
  	var a = t.responseText.parseJSON();
	//var objectId = a['userWish']['id'];
	var type = a['type'];
	
	window.location.reload();
	
	
	/*	
	if (elt = document.getElementById('selection')) {
		//selectionRrefresh(type);
	}
	
	if (a['msg']==undefined) {
		if (nb==0)  a['msg'] = new Array('validation','Le produit a bien été retiré de votre sélection.');
		else a['msg'] = new Array('validation','Le produit a bien été ajouté à votre sélection.');
	}
	displayMessage('msg', a['msg']);
	
	// alert(t.responseText);
	*/
}

var errFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

//For an XML response:
/*
var handlerFunc = function(t) {
    var xmlDoc = t.responseXML.documentElement;
    //Handle data.
}
*/

function querystringToArray(str) {
	
	//str = str.replace("?","");
	var arr = str.split('&');
	var retour = new Array();
	for (i=0; i<arr.length; i++) {
		couple = arr[i].split('=');
		retour[couple[0]] = couple[1];		
	}
	//alert(retour['name']);
	return retour;
}
function cadAdd(obj, type, caddieValue) {
	var nb = false;
	if (!caddieValue)
	{
		caddieValue = 0;
	}
	if (elt=document.getElementById('inp_'+obj.ref))
	{
		if (elt.value > caddieValue)
		{
			nb= elt.value - caddieValue;
		}
	}
	if (nb==false)
	{
		nb = 1;
	}
	var postData = '&ref='+ obj.ref + '&name=' + obj.name + '&qte=' + obj.qte + '&price=' + obj.price + '&location=' + obj.location + '&achat=' + obj.achat +'&nb='+nb+'&type='+type;

	currentRef = obj.ref ;
	
	new Ajax.Updater('msg', '../ajax/cad-add.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadAddLoaded, onFailure:errFunc});
	
}

function cadDel(ref, type) {
	var postData = '&ref='+ ref + '&type='+type;
	new Ajax.Request('../ajax/cad-del.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadDelLoaded, onFailure:errFunc});
	
}

function cadUpd(obj, elt, type) {
	var postData = '&ref='+ obj.ref + '&name=' + obj.name + '&qte=' + obj.qte + '&price=' + obj.price + '&location=' + obj.location + '&achat=' + obj.achat +'&nb='+ parseInt(elt.value)+'&type='+type;
	
	currentRef = obj.ref ;
	
	new Ajax.Updater('msg', '../ajax/cad-update.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadAddLoaded, onFailure:errFunc});
	
}

var timerId;
function doCadUpd(obj, elt, type){
clearTimeout(timerId);
timerId = window.setTimeout(function () {cadUpd(obj, elt, type);}, 1000);

}


function selectionRrefresh(type) {
	
	var postData = '&type='+ type;
	new Ajax.Updater('selection', '../ajax/cad-selection-refresh.php', {asynchronous:true, parameters:postData, method:'post',  onComplete:cadAddLoaded, onFailure:errFunc});
	
}


//new Ajax.Updater('div_to_be_updated', '/action/here', {asynchronous:true, parameters:Form.serialize(this)});

/*
- - - - - - - - - -
MSG EFFECT
- - - - - - - - - -
*/
var msgType = {validation:'Confirmation', attention:'Attention'};
var timeout;
function displayMessage(strId, arrMsg) {
	if (elt = document.getElementById(strId)) { 
			//elt.innerHTML = '<div class="msgIn"><p><strong>'+msgType[arrMsg[0]]+' :</strong> '+arrMsg[1]+'</p></div>';
			elt.innerHTML = '';
			var div = document.createElement('div');
			div.className = 'msgIn';
			
			var p = document.createElement('p');
			p.innerHTML = '<strong>'+msgType[arrMsg[0]]+' :</strong> '+arrMsg[1]+'';
			
			div.appendChild(p);
			elt.appendChild(div);
			changeClassName(strId, arrMsg[0]);
			elt.style.display = 'block';
			elt.style.height = '';
			elt.startHeight = undefined;
			clearTimeout(timeout);
			timeout = setTimeout("hideBlock('" + strId + "')",10000);
	}
}

function getElementHeight(elem) {
		var xPos = elem.offsetHeight;
		return xPos;
}

function hideBlock (strId) {
	if (elt = document.getElementById(strId)) {
		if (elt.startHeight == undefined) {
			elt.startHeight = getElementHeight(elt) ;
			elt.style.overflow = 'hidden';
		}
		if (getElementHeight(elt)>2) {
			elt.startHeight -= 8;
			if (elt.startHeight<0) elt.startHeight = 1;
			elt.style.height = String(elt.startHeight)+'px';
			elt.style.backgroundImage = 'none';
			setTimeout("hideBlock('" + strId + "')",50);
		}
		else {
			elt.style.display = 'none';
			elt.startHeight = undefined;
		}
		
	}
}


// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function( f ) {
 var i = this.length, j, l = this.length;
 for( i=0; i<l; i++ ) { if( ( j = this[i] ) ) { f( j ); } }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function( i, v ) {
 if( i>=0 ) {
  var a = this.slice(), b = a.splice( i );
  a[i] = v;
  return a.concat( b );
 }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length; while(i-->b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

// Array.walk() - Change each value according to a callback function
Array.prototype.walk = function( f ) {
 var a = [], i = this.length;
 while(i--) { a.push( f( this[i] ) ); }
 return a.reverse();
};


/*
- - - - - - - - - -
GALERY
- - - - - - - - - -
*/
function showImage(image, divid) {
	
	//new Ajax.Updater(divid, '../ajax/showImage.php', {asynchronous:true, parameters:'image='+image, method:'post'});
	new Ajax.Updater(divid, '../ajax/showImage.php', {asynchronous:true, parameters:'image='+image.split('.').join('|'), method:'post'});
}

function firstSpecificChild(elt, type) {
	for (i=0; i<elt.childNodes.length; i++) {
		if (type == elt.childNodes[i].nodeName) return elt.childNodes[i];
	}
	return false;
}



function rwnPhotoNext(divId) {
		// Get img element
		if (elt = document.getElementById(divId)) {
			var img = firstSpecificChild(elt, 'IMG');
		}
		if (img) {
			// get Array Index
			var imgName = img.getAttribute('src').split('/');
			var index = rwnPhotoArray[divId].arr.indexOf(imgName[imgName.length-1]);
			index++;
			if (index>=rwnPhotoArray[divId].arr.length) index = 0;
			showImage(rwnPhotoArray[divId].path+rwnPhotoArray[divId].arr[index], divId);
		}
	
}

function rwnPhotoPrevious(divId) {
		// Get img element
		if (elt = document.getElementById(divId)) {
			var img = firstSpecificChild(elt, 'IMG');
		}
		if (img) {
			// get Array Index
			var imgName = img.getAttribute('src').split('/');
			var index = rwnPhotoArray[divId].arr.indexOf(imgName[imgName.length-1]);
			index--;
			if (index<0) index = rwnPhotoArray[divId].arr.length-1;
			showImage(rwnPhotoArray[divId].path+rwnPhotoArray[divId].arr[index], divId);
		}
}

/*
- - - - - - - - - -
FLASH DETECT
- - - - - - - - - -
*/

// Major version of Flash required
var requiredMajorVersion = 8;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;


var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;

var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;

var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;



function ControlVersion()

{

	var version;

	var axo;

	var e;



	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry



	try {

		// version will be set for 7.X or greater players

		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");

		version = axo.GetVariable("$version");

	} catch (e) {

	}



	if (!version)

	{

		try {

			// version will be set for 6.X players only

			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");

			

			// installed player is some revision of 6.0

			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,

			// so we have to be careful. 

			

			// default to the first public version

			version = "WIN 6,0,21,0";



			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		

			axo.AllowScriptAccess = "always";



			// safe to call for 6.0r47 or greater

			version = axo.GetVariable("$version");



		} catch (e) {

		}

	}



	if (!version)

	{

		try {

			// version will be set for 4.X or 5.X player

			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");

			version = axo.GetVariable("$version");

		} catch (e) {

		}

	}



	if (!version)

	{

		try {

			// version will be set for 3.X player

			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");

			version = "WIN 3,0,18,0";

		} catch (e) {

		}

	}



	if (!version)

	{

		try {

			// version will be set for 2.X player

			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");

			version = "WIN 2,0,0,11";

		} catch (e) {

			version = -1;

		}

	}

	

	return version;

}



// JavaScript helper required to detect Flash Player PlugIn version information

function GetSwfVer(){

	// NS/Opera version >= 3 check for Flash plugin in plugin array

	var flashVer = -1;

	

	if (navigator.plugins != null && navigator.plugins.length > 0) {

		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {

			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";

			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;			

			var descArray = flashDescription.split(" ");

			var tempArrayMajor = descArray[2].split(".");

			var versionMajor = tempArrayMajor[0];

			var versionMinor = tempArrayMajor[1];

			if ( descArray[3] != "" ) {

				tempArrayMinor = descArray[3].split("r");

			} else {

				tempArrayMinor = descArray[4].split("r");

			}

			var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;

			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;

		}

	}

	// MSN/WebTV 2.6 supports Flash 4

	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;

	// WebTV 2.5 supports Flash 3

	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;

	// older WebTV supports Flash 2

	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;

	else if ( isIE && isWin && !isOpera ) {

		flashVer = ControlVersion();

	}	

	return flashVer;

}



// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available

function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)

{

	versionStr = GetSwfVer();

	if (versionStr == -1 ) {

		return false;

	} else if (versionStr != 0) {

		if(isIE && isWin && !isOpera) {

			// Given "WIN 2,0,0,11"

			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]

			tempString        = tempArray[1];			// "2,0,0,11"

			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']

		} else {

			versionArray      = versionStr.split(".");

		}

		var versionMajor      = versionArray[0];

		var versionMinor      = versionArray[1];

		var versionRevision   = versionArray[2];



        	// is the major.revision >= requested major.revision AND the minor version >= requested minor

		if (versionMajor > parseFloat(reqMajorVer)) {

			return true;

		} else if (versionMajor == parseFloat(reqMajorVer)) {

			if (versionMinor > parseFloat(reqMinorVer))

				return true;

			else if (versionMinor == parseFloat(reqMinorVer)) {

				if (versionRevision >= parseFloat(reqRevision))

					return true;

			}

		}

		return false;

	}

}
/*
- - - - - - - - - -
SWF
- - - - - - - - - -
*/
function rwnSwf() {
	arr = new Array();
	for (var i=0; i < arguments.length; i++) {
		arr[i] = arguments[i];
	}
	for (var i=0; i < arr.length; i=i+2) {
		if (arr[i].toLowerCase()=='src') arr.push('movie', arr[i+1]);
	}
	arr.push('quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer');
 	var ret =
    AC_GetArgs
    (  arr, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
	AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
function AC_AddExtension(src, ext) {
  if (src.indexOf('?') != -1) return src.replace(/\?/, ext+'?');
  else return src + ext;
}
function AC_Generateobj(objAttrs, params, embedAttrs) {
  var str = '<object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';
  document.write(str);
}
function AC_FL_RunContent() {
  var ret =
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();
    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace":
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}