

function splashPop(page) {
	window.open(page,'','width=800,height=500,resizable=yes,toolbars=no,scrollbars=yes');
}

function validateFormOnSubmit(theForm) {
  var reason = "";

  reason += validateEmpty(theForm.frm_nome, 'Nome');
  reason += validateEmpty(theForm.frm_cognome, 'Cognome');
  reason += validateEmpty(theForm.frm_indirizzo, 'Indirizzo');
  reason += validateEmpty(theForm.frm_cap, 'CAP');
  reason += validateEmpty(theForm.frm_citta, 'Città');
  reason += validateEmpty(theForm.frm_provincia, 'Provincia');
  reason += validateEmpty(theForm.frm_telefono, 'Telefono');
  reason += validateEmail(theForm.frm_email);
      
  if (reason != "") {
    alert("Attenzione, inserisci tutti i campi obbligatori.");
    return false;
  }
  
  $.ajax({
   type: "POST",
   url: "http://www.operamage.com/offerta-olio-extravergine/salva",
   data: $("#buy-now").serialize(),
   success: function(e){
   }
 });
  
  var pagamento=new Array(); // regular array (add an optional integer
  pagamento['cs']="Contrassegno";
  pagamento['cc']="Carta di credito";
  pagamento['bb']="Bonifico Bancario";
  pagamento['pp']="Paypal";
  
  message = '<ul>';
  message += '<li>Nome: ' + $("input[name*='frm_nome']").val() + '</li>' + "\n"; 
  message += '<li>Cognome: ' + $("input[name*='frm_cognome']").val() + '</li>' + "\n"; 
  message += '<li>Indirizzo: ' + $("input[name*='frm_indirizzo']").val() + '</li>' + "\n";  
  message += '<li>CAP: ' + $("input[name*='frm_cap']").val() + '</li>' + "\n";  
  message += '<li>Città: ' + $("input[name*='frm_citta']").val() + '</li>' + "\n"; 
  message += '<li>Provincia: ' + $("input[name*='frm_provincia']").val() + '</li>' + "\n"; 
  message += '<li>Telefono: ' + $("input[name*='frm_telefono']").val() + '</li>' + "\n"; 
  message += '<li>e-Mail: ' + $("input[name*='frm_email']").val() + '</li>' + "\n"; 
  message += '<li class="pagamento">Modalità di pagamento: ' + pagamento[$("input[name*='tipo_pagamento']:checked").val()] + '</li>' + "\n"; 
  message += '</ul>';

  confirm(message, function () {
	$("#buy-now").submit();
  	return true;
  });
  return false;
}

function validateEmpty(fld, name) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.backgroundImage = 'url('+error_bg+')'; 
        error = 'Non hai inserito il campo ' + name + "\n"
    } else {
        fld.style.backgroundImage = 'url('+default_bg+')'; 
    }
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.backgroundImage = 'url('+error_bg+')'; 
        error = "Non hai inserito l'indirizzo e-Mail.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.backgroundImage = 'url('+error_bg+')'; 
        error = "Indirizzo e-Mail non corretto.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.backgroundImage = 'url('+error_bg+')'; 
        error = "L'indirizzo e-Mail contiene caratteri non consentiti.\n";
    } else {
        fld.style.backgroundImage = 'url('+default_bg+')'; 
    }
    return error;
}

function confirm(message, callback) {
	$('#confirm').modal({
		closeHTML:"<a href='#' title='Chiudi e annulla' class='modal-close'>x</a>",
		position: ["25%",],
		overlayId:'confirm-overlay',
		containerId:'confirm-container', 
		onShow: function (dialog) {
			$('.message', dialog.data[0]).append(message);

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
}

