/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 185 2009-02-09 21:51:12Z emartin24 $
 *
 */

function openBox(url, height, width){
	// load the contact form using ajax
	$.get(url, function(data){
		
		$.modal(data,{
			zIndex: 1000
			//position: [10, (document.body.offsetWidth - 550) / 2]
		});


	});
}



$(document).ready(function () {

	$('#contatti').click(function (e) {
		e.preventDefault();
		inizializzaForm();
		$('#basicModalContent').modal({
//			position: [10, (document.body.offsetWidth - 550) / 2]
		});
	});
});

 

var baseCaptca = "/library/code/blockautosubmit/generateimage.php?BASimgcharstr=dzWo0";

function reloadCaptcha(){

		$("#BASentryField-IMAGE").attr("src", baseCaptca + '&' + (new Date()).getTime());
		
	}


  function getInput(){
  	// get all the inputs into an array.
    var $inputs = $('#contact-form input, #contact-form textarea, #contact-form select');

    // not sure if you wanted this, but I thought I'd add it.
    // get an associative array of just the values.
    var values = {};
    $inputs.each(function(i, el) {
        values[el.name] = $(el).val();
    });
	return values;

  }	
  
  	
  function generaRules( input, params ){
  	 for (i=0;i<input.length;i++){
	 	$("#"+input[i]).rules("add", params[input[i]]);
	 }
  }
  
  
  
var init = false;

function inizializzaForm(){
	var base_url = window.location;
	if (init == false) {
		$("#send_form").click(function(){
				
		 window.location = base_url + "#risposta_form";
		 if ($("#contact-form").valid())
		 var lang= $("#contatti_lingua").val() //document.getElementById("contatti_lingua").value;

		 $.post("/"+lang+"/sender_mail.php", getInput(), function(data){
			 $("#risposta_errata").css("display", "none");
			 $("#risposta_corretta").css("display", "none");
			 
			 if (data.ret == 1){
			 // non ho errori
			 $("#risposta_corretta").css("display", "block");
			 $("#form-space").css("display", "none");
			 }else{
			 // rigenero il codice
			 reloadCaptcha();
			 $("#risposta_errata").css("display", "block");
			 $("#lista_errori").html(data.ret);
			 }
			 
		 }, "json");	
		});
		init = true;
	}
}
//Higlight Background Form
function tdColor(elementId) {
	/*
	if (document.getElementById) {
		document.getElementById(elementId).style.backgroundColor="#0096D4"
	}*/
}
function tdColorOff(elementId) {
	/*
	if (document.getElementById) {
		document.getElementById(elementId).style.backgroundColor=""
	}*/
}
