/************************/
/*   VOCALITY scripts	*/
/************************/


	/*******************/
	/* popup functions */
	/*******************/

	//
	// show popup callback
	//
	function show_popup_callback(accountid,fromhostname,referrer,protocol,title,secret,frmCallbackType) {
	  var frm = '';
	  if (frmCallbackType == 0) {
	    frm = 'callback_form.php';
	  } else if (frmCallbackType == 1) {
	    frm = 'callback_form_b2b.php';
	  }
	  //callbackwindow = dhtmlmodal.open('callback_box', 'iframe', 'http://callback.vocality.fr/platform/callback_form.php?accountid=' + accountid + '&fromhostname=' + fromhostname + '&secret=' + secret + '&protocol=' + protocol + '&referrer=' + referrer, 'Rappel gratuit', 'width=660px,height=538px,center=1,resize=0,scrolling=0');
	  callbackwindow = dhtmlmodal.open('callback_box', 'iframe', 'http://callback.vocality.fr/platform/' + frm + '?accountid=' + accountid + '&fromhostname=' + fromhostname + '&secret=' + secret + '&protocol=' + protocol + '&referrer=' + referrer, 'Rappel gratuit', 'width=660px,height=492px,center=1,resize=0,scrolling=0');
	  
	  
	/*
	  callbackwindow.onclose=function(){
	    var theform = this.contentDoc.forms[0];
	    alert("we close");
	    return true;
	  }
	  */
	}

	//
	//Function to run when buttons within modal window is clicked on. Directly embedded inside hidden DIV, bypassing "onclose" event:
	//
	function processCall(form) {
	  isFormValid = validateForm(form);
	  if (isFormValid) {
	    cbacFrm.submit();
	    //callback_window.load('ajax', 'http://callback.vocality.fr/callback_processing.php','Rappel gratuit');
	  }
	}


	/*******************/
	/* Form validation */
	/*******************/

	function validateForm(form) {
	  var reason = '';

	  //reason += validateEmpty(form);
	  reason += validatePhoneNumber(form);

	  if (reason != '') {
	    alert("Certains champs sont incorrects:\n" + reason);
	    return false;
	  }

	  return true;
	}

	function validateEmpty(form) {
	  var error = '';

	  if (form.calldest.value.length == 0) {
	    error = "- Le numéro de téléphone requis.\n";

	    form.calldest.style.background = '#FF8080';
	    form.calldest.focus();
	  } else {
	    form.calldest.style.background = '#FFFFFF';
	  }
	  return error;
	}

	function validatePhoneNumber(form) {
	  var error = '';
	  var stripped = form.calldest.value.replace(/[\(\)\.\-\ ]/g, '');
	  var regexp_fixe = /0[1-5]\d\d\d\d\d\d\d\d/;
	  var regexp_fixe_gsm = /0[1-6]\d\d\d\d\d\d\d\d/;
	  var curRegexp = regexp_fixe_gsm;
	  var matchPhoneNumber = stripped.match(curRegexp);

	  if (stripped.length != 10) {
	    error = "- La longueur du numéro de téléphone est insuffisante\n";
	  } else if (isNaN(stripped)) {
	    error = "- Certains caractères ne sont pas des nombres\n";
	  } else if (matchPhoneNumber == null) {
	    error = "- Le numéro " + form.calldest.value + " n'est pas autorisé\n";
	  } else {
	    form.calldest.style.background = '#FFFFFF';
	  }

	  if (error != '') {
	    form.calldest.style.background = '#FF8080';
	    form.calldest.focus();
	  }
	  return error;
	}
