﻿function $m(theVar){
	return document.getElementById(theVar)
}
function remove(theVar){
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
}
function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	    obj.addEventListener(evType, fn, true)
	if(obj.attachEvent)
	    obj.attachEvent("on"+evType, fn)
}
function removeEvent(obj, type, fn){
	if(obj.detachEvent){
		obj.detachEvent('on'+type, fn);
	}else{
		obj.removeEventListener(type, fn, false);
	}
}

// browser detection
function isWebKit(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}

function AfficherChampsDeValeur(idChamp)
{
	if(idChamp == 'pass0' || idChamp == 'pass1')
		if(document.getElementById('pass0label').innerHTML.match(/Cliquez pour/))
			document.getElementById('pass0label').innerHTML = "Tapez votre nouveau mot de passe : ";
	if(!document.getElementById(idChamp).disabled)
	{
		document.getElementById(idChamp).style.display = '';
		if(idChamp != 'pass0' && idChamp != 'pass1')
			document.getElementById(idChamp).focus();
		document.getElementById('show_'+idChamp).style.display = 'none';
	}
}

function CacherChampsDeValeur(idChamp)
{
	if(document.getElementById(idChamp) == '[object HTMLInputElement]')
	{
		if(document.getElementById(idChamp).type == 'password')
		{
			document.getElementById('show_'+idChamp).innerHTML = '';
			for(var i = 0 ; i < document.getElementById(idChamp).value.length ; i++)
				document.getElementById('show_'+idChamp).innerHTML += '*';
			document.getElementById('pass0label').innerHTML = "Cliquez pour modifier votre mot de passe : ";
		}
		else
			document.getElementById('show_'+idChamp).innerHTML = document.getElementById(idChamp).value;
	}
	else if(document.getElementById(idChamp) == '[object HTMLSelectElement]')
	{
		document.getElementById('show_'+idChamp).innerHTML = document.getElementById(idChamp).options[document.getElementById(idChamp).selectedIndex].text;
	}
	else
		alert(document.getElementById(idChamp));
	
	
	document.getElementById(idChamp).style.display = 'none';
	document.getElementById('show_'+idChamp).style.display = '';
}




//RAJOUTE PAR ALEX POUR JCROP


			// Our simple event handler, called from onChange and onSelect
			// event handlers, as per the Jcrop invocation above
			function updateCoords(c)
			{
				$('#x').val(c.x);
				$('#y').val(c.y);
				$('#w').val(c.w);
				$('#h').val(c.h);
			};

			function checkCoords()
			{
				if (parseInt($('#w').val())) return true;
				alert('Veuillez sélectionner une zone dans l\'image.');
				return false;
			};
			function showPreview(coords)
			{
				if (parseInt(coords.w) > 0)
				{
					var rx = 130 / coords.w;
					var ry = 160 / coords.h;

					jQuery('#preview').css({
						width: Math.round(rx * document.getElementById('cropbox').width) + 'px',
						height: Math.round(ry * document.getElementById('cropbox').height) + 'px',
						marginLeft: '-' + Math.round(rx * coords.x) + 'px',
						marginTop: '-' + Math.round(ry * coords.y) + 'px'
					});
				}
			}
//FIN RAJOUT ALEX


			

// send data
function ajaxUpload(form){
	var detectWebKit = isWebKit();
	var get_url = '/crop.php';// php file
	var div_id = 'upload_area';// div id where to show uploaded image
	var show_loading = '<img src="/images/loading.gif" />';// loading image
	var html_error = '<img src="/images/error.png" />';// error image

	// create iframe
	var sendForm = document.createElement("iframe");
	sendForm.setAttribute("id","uploadform-temp");
	sendForm.setAttribute("name","uploadform-temp");
	sendForm.setAttribute("width","0");
	sendForm.setAttribute("height","0");
	sendForm.setAttribute("border","0");
	sendForm.setAttribute("style","width: 0; height: 0; border: none;");

	// add to document
	form.parentNode.appendChild(sendForm);
	window.frames['uploadform-temp'].name="uploadform-temp";

	// add event
	var doUpload = function(){
		removeEvent($m('uploadform-temp'),"load", doUpload);
		var cross = "javascript: ";
		cross += "window.parent.$m('"+div_id+"').innerHTML = document.body.innerHTML; void(0);";
		$m(div_id).innerHTML = html_error;
		$m('uploadform-temp').src = cross;
		if(detectWebKit){
        	remove($m('uploadform-temp'));
        }else{
			document.getElementById('formsignup').style.display= 'none';
			document.getElementById('uploadfiletohide').style.display= 'none';
        	setTimeout(function(){ remove($m('uploadform-temp'))}, 250);
        }
		
		//PARTIE RAJOUTEE PAR ALEX POUR JCROP
		var functionLoadJcropSurImage = function()
		{
			if(document.getElementById('cropbox'))
			{
				jQuery('#cropbox').Jcrop({
					onChange: showPreview,
					onSelect: showPreview,
					aspectRatio: 65/80,
					onSelect: updateCoords
				});
			}
			else
			{
				setTimeout(functionLoadJcropSurImage, 250);
			}
		};
		
		setTimeout(functionLoadJcropSurImage, 250);
		//FIN PARTIE RAJOUTEE PAR ALEX POUR JCROP
    }
	addEvent($m('uploadform-temp'),"load", doUpload);

	// form proprietes
	form.setAttribute("target","uploadform-temp");
	form.setAttribute("action",get_url);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");

	// loading
	if(show_loading.length > 0){
		$m(div_id).innerHTML = show_loading;
	}
	// submit
	form.submit();
	//formDisable();
  return false;
}

// disable form
  function formDisable() {
    var limit = document.forms[1].elements.length;
    for (i=0;i<limit;i++) {
      document.forms[1].elements[i].disabled = true;
    }
  }

// enable form
  function formEnable() {
    var limit = document.forms[1].elements.length;
    for (i=0;i<limit;i++) {
      document.forms[1].elements[i].disabled = false;
    }
  }

// when from is submited
// disable form and after 5sec
//reload captcha, reset form, then enable it
function formDone() {
	//formDisable();
//setTimeout ( "formEnable()", 5000 );
}


	    function uniqid()
        {
        var newDate = new Date;
        return newDate.getTime();
        }




	//ZuzuBooth
	function onTakeSnapshotPressed(userId){
		//alert("onTakeSnapshotPressed("+userId+")");
		//userId is the value of the userId flash var (XXY by default)
		//the user clicked the [TAKE SNAPSHOT] button inside the app 
	}

    function onSaveJpgOk(photoName,userId){
		//alert("onSaveJpgOk("+photoName+","+userId+")");
		//photoName is the filename of the image including the .jpg extension
		//userId is the value of the userId flash var (XXY by default)
		//the user clicked the [TAKE SNAPSHOT] button inside the app and the jpg_encoder_download.php script returned save=ok
	}
	
	function onSaveJpgFailed(photoName,userId){
		//alert("onSaveJpgFailed("+photoName+","+userId+")");
		//photoName is the filename of the image including the .jpg extension
		//userId is the value of the userId flash var (XXY by default)
		//the user pressed the [take snapshot] button inside the app but the jpg_encoder_download.php script returned save=fail
	}
	
	function onSavePressed(userId,photoName,width,height){
		//alert("onSavePressed("+userId+","+photoName+","+width+",+height+"")");
		//userId is the value of the userId flash var (XXY by default)
		//photoName is the filename of the image including the .jpg extension
		//width is the width of the picture in pixels
		//height is the height of the picture in pixels
		
		//this function is called when the user clicked the [SAVE] button inside the app OR, when the [SAVE] button is hidden (saveVisible=0) it is called automatically after each snapshot taken 
		//document.getElementById('divimageprofil').style.background = 'none';
		setTimeout('document.getElementById(\'divimageprofil\').style.backgroundImage = \'url("/donneesutilisateurs/'+userId+'/photo/photoprofil.jpg?uniqid=\'+uniqid()+\'")\';', 1000);
		//setTimeout('alert(\'test\');', 1000);
	}
	
	function onTakeAnotherPressed(userId){
		//alert("onTakeAnotherPressed("+userId+")");
		//userId is the value of the userId flash var (XXY by default)
		//the user clicked the [TAKE ANOTHER] button inside the app 
	}
