function uploadImg(e) {
  //var post = $('newImgForm').serialize(true);
  $('uploadWait').show();
  Event.stopObserving('upload', 'click', uploadImg);  
  $('formUploadImg').submit();
}

function uploadImgEnd(error,msg,imgSrc,uid) {
  $('uploadWait').hide();
  $('inputImage').value = '';
  Event.observe('upload', 'click', uploadImg);
  if (error == 10) {
    alert(msg);
  } else {
    var random = Math.floor(Math.random()*1000);
    $('img_'+uid).src = imgSrc+'?rnd='+random;
  }
}

function profilDetectRegion(e) {
  var id = Event.element(e).id;
  if ($F('cp').length < 2) return false;
  
	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
		postBody: $H({
		  'mode': 'region',
		  'cp':$F('cp')
    }).toQueryString(),
		onComplete: function (xhr, json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
      } else {
        messageGlobal('valid','');
        $('selectRegion').value = xhr.responseText;
      }
	 }
  });
}

function submitSearchCompForm(e) {
	var myAjax = new Ajax.Request(_rootPath+'annonce.php', {
		parameters: $('formSearch').serialize(true),
		onComplete: function (xhr,json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
      } else {
        messageGlobal('valid','');
        //alert(window.location.pathname+"?search="+xhr.responseText);
        window.location.href = window.location.pathname+"?search="+xhr.responseText;
      }
		}
  });
}

function submitSearchAccount(e) {
	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
		parameters: $('formSearchProfil').serialize(true),
		onComplete: function (xhr,json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
      } else {
        messageGlobal('valid','');
        //alert(window.location.pathname+"?search="+xhr.responseText);
        //alert(window.location.pathname+"?mode=search&search="+xhr.responseText);
        window.location.href = window.location.pathname+"?mode=search&search="+xhr.responseText;
      }
		}
  });
}

function selectActiNiv2 (e) {
  var id = Event.element(e).id;
	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
		postBody: $H({
		  'mode': 'comp',
		  'action': 'selectAct2',
		  'pere':$F(id)
    }).toQueryString(),
    onCreate: function () {
      messageGlobal('valid',_lang['formLoading']);
    },
		onComplete: function (xhr,json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
      } else {
        messageGlobal('valid','');
        $('selectAct2').replace(xhr.responseText);
      }
		}
  });
}

function submitAbusForm() {
	var post = $('formAbus').serialize(true);
	var myAjax = new Ajax.Request(_rootPath+'annonce.php', {
		parameters: post,
		onComplete: function (xhr, json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
		  } else if (json.error == 5) {
  		  	eval(xhr.responseText);
        	$$('#replyForm div.error').each(function reset(id) {
          	$(id.id).hide();
          });
          error.each(function disp(pair) {
            if (pair.key == 'errorMsg') return;
            $(pair.key+'Error').update(pair.value);
            $(pair.key+'Error').show();
        	});
        	messageGlobal('error',error.get('errorMsg'));
     	} else {
       		Effect.SlideUp('abusForm', {duration:0.35});
       		messageGlobal('valid',xhr.responseText);
     	}
	 }
  });
}

//    INVITATION 
function submitInviteForm() {
  $('valid').disable();
	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
		parameters: $('formInvite').serialize(true),
		onComplete: function (xhr, json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
		  } else if (json.error == 5) {
  		  	eval(xhr.responseText);
        	$$('#replyForm div.error').each(function reset(id) {
          	$(id.id).hide();
          });
          error.each(function disp(pair) {
            if (pair.key == 'errorMsg') return;
            $(pair.key+'Error').update(pair.value);
            $(pair.key+'Error').show();
        	});
        	messageGlobal('error',error.get('errorMsg'));
        	$('valid').enable();
     	} else {
     	  $('mail').value='';
     	  $('text').value='';
        messageGlobal('valid',xhr.responseText);
        $('valid').enable();
      }
    }
  });
}


// PROPOSER UNE ACTIVITE
function submitFormPropose() {
  $('valid').disable();
	var myAjax = new Ajax.Request(_rootPath+'activites.php', {
		parameters:  $('formPropose').serialize(true),
		onComplete: function (xhr, json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
		  } else if (json.error == 5) {
  		  	eval(xhr.responseText);
        	$$('#formPropose div.error').each(function reset(item) {
          	$(item.id).hide();
          });
          error.each(function disp(pair) {
            if (pair.key == 'errorMsg') return;
            $(pair.key+'Error').update(pair.value);
            $(pair.key+'Error').show();
        	});
        	messageGlobal('error',error.get('errorMsg'));
     	} else window.location.href = xhr.responseText;
     	
     	$('valid').enable();
	  }
  });	
}

// CREATION D'UNE ANNONCE
function dispRemu(e) {
	var id=Event.element(e).id;
	if ($F(id)=='remuneration') {
		$('divTarif').show();
//		$('divStatut').show();
	} else {
		$('divTarif').hide();
//		$('divStatut').show();
	}
}
function dispStatut(e) {
  var id=Event.element(e).id;
  // Change le select Offre  
	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
		postBody: $H({
		  'mode': 'comp',
		  'action': 'selectOffre',
		  'sens':$F(id)
    }).toQueryString(),
    onCreate: function () {
      messageGlobal('valid',_lang['formLoading']);
    },
		onComplete: function (xhr,json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		    return false;
      } else {
        messageGlobal('valid','');
        Event.stopObserving('selectOffre','change',dispRemu);
        $('selectOffre').replace(xhr.responseText);
        Event.observe('selectOffre','change',dispRemu);
      }
		}
  });  
  
  // Affiche ou cache le statut
/*  if ($F(id)=='teach') $('divStatut').show();
    else {
      $('divStatut').hide();
      $('divTarif').hide();
    }
*/
}

// ENVOYER UN CONTACT
function submitContact() {
  $('submit').disable();
	var myAjax = new Ajax.Request(_rootPath+'contact.php', {
		parameters:  $('formContact').serialize(true),
		onComplete: function (xhr, json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		  } else if (json.error == 5) {
  		  	eval(xhr.responseText);
        	$$('#formContact div.error').each(function reset(item) {
          	$(item.id).hide();
          });
          error.each(function disp(pair) {
            if (pair.key == 'errorMsg') return;
            $(pair.key+'Error').update(pair.value);
            $(pair.key+'Error').show();
        	});
        	messageGlobal('error',error.get('errorMsg'));
     	} else window.location.href = xhr.responseText;
     	$('submit').enable();
	  }
  });	
}

/* ------     CONTRAT     ------- */
function contratSee(e) {
  var id=Event.element(e).id;
  var infos = id.split(_reg);
  
  $$('#contratDetail input.patouche').each(function (item) {
    Event.stopObserving(item.id,'click',contratSign);
  });

	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
		postBody: $H({
		  'mode': 'contrats',
		  'action': 'voir',
		  'contrat':infos[1]
    }).toQueryString(),
		onComplete: function (xhr, json) {
		  if (json.error > 9) {
		    messageGlobal('error',xhr.responseText);
		  } else {
        $('contratDetail').update(xhr.responseText);
      }
	  }
  });	
}
function contratDelete(e) {
  if (confirm(_lang['contratAskDelete'])) {
    var id=Event.element(e).id;
    var infos = id.split(_reg);
    
  	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
  		postBody: $H({
  		  'mode': 'contrats',
  		  'action': 'delete',
  		  'sens': infos[0],
  		  'contrat':infos[1]
      }).toQueryString(),
  		onComplete: function (xhr, json) {
  		  if (json.error > 9) {
  		    messageGlobal('error',xhr.responseText);
  		  } else {
          // détruit visuellement le contrat
          Event.stopObserving(id,'click',contratDelete);
          $('contrat_'+infos[1]).replace('');
          messageGlobal('valid',xhr.responseText);
        }
  	  }
    });	
  }}
function contratSign(e) {
  if (confirm(_lang['contratAskSign'])) {
    var id=Event.element(e).id;
    var infos = id.split(_reg);
    
  	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
  		postBody: $H({
  		  'mode': 'contrats',
  		  'action': 'sign',
  		  'sens': infos[0],
  		  'contrat':infos[1]
      }).toQueryString(),
  		onComplete: function (xhr, json) {
  		  if (json.error > 9) {
  		    messageGlobal('error',xhr.responseText);
  		  } else {
          $(infos[0]+'Sign').update(xhr.responseText);
          // détruit le bouton
          Event.stopObserving(id,'click',contratSign);
          $(id).replace('');
        }
  	  }
    });	
  }
}

function contratNote(e) {
  var id=Event.element(e).id;
  if (confirm(_lang['contratAskNote']+"\n\n"+$(id).value)) {
    var infos = id.split(_reg);
    
  	var myAjax = new Ajax.Request(_rootPath+'profil.php', {
  		postBody: $H({
  		  'mode': 'contrats',
  		  'action': 'note',
  		  'contrat':infos[1]
      }).toQueryString(),
  		onComplete: function (xhr, json) {
  		  if (json.error > 9) {
  		    messageGlobal('error',xhr.responseText);
  		  } else {
          $('teacherNote').update(xhr.responseText);
          // détruit le select
          Event.stopObserving(id,'change',contratNote);
          $(id).replace('');
        }
  	  }
    });	
  }
}




