/* */
function show_result(data){
    if (data['status'] == 200) {
	$('#contact_form_new').hide();
	$('#contact_form_new').after('<h2>Gracias por contactar con nosotros</h2> <p>Nos pondremos en contacto con usted lo antes posible</p>');
    }
    else {
	$('#contact_form_new').before('<p class="advert">Ha ocurrido un error, int&eacute;ntelo de nuevo por favor</p>');
    }
}

function echeck(str) {
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
	return false;
    }
    
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	return false;
    }
    
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	return false;
    }
    
    if (str.indexOf(at,(lat+1))!=-1){
	return false;
    }
    
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	return false;
    }
    
    if (str.indexOf(dot,(lat+2))==-1){
	return false;
    }
    
    if (str.indexOf(" ")!=-1){
	return false;
    }
    return true;				
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = " ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 9;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length == minDigitsInIPhoneNumber);
}

function checkForm(){
    var list = new Array('#id_name_lastname', '#id_email', '#id_phone', '#accept_terms', '#id_address', '#id_cp', '#id_city', '#id_body');
    $('.error').each(function(){$(this).attr('class', '');});
    $('.errors').remove();
    var errors = $('<div class="errors"><h2>Hay un error en los siguientes campos:</h2><div class="inner"><ul></ul></div></div>')
    errors.css('display', 'none')
    $('p.required_note').after(errors);

    var error_signal = 0;

    if ($('#id_name_lastname').attr('value') == ""){
	$('#id_name_lastname').parent().attr('class', 'error');
	$('.errors ul').append('<li>El <a href="#id_name_lastname">nombre</a> no ha sido completado</li>');
	error_signal = 1;
    }
    if ($('#id_email').attr('value') == ""){
	$('#id_email').parent().attr('class', 'error');
	$('.errors ul').append('<li>El <a href="#id_email">email</a> no ha sido completado</li>');
	error_signal = 1;
    }
    if (($('#id_email').attr('value') != "") && (echeck($('#id_email').attr('value')) == false)){
	$('#id_email').parent().attr('class', 'error');
	$('.errors ul').append('<li>El <a href="#id_email">email</a> introducido no es válido</li>');
	error_signal = 1;
    }

    if ($('#id_phone').attr('value') == ""){
	$('#id_phone').parent().attr('class', 'error');
	$('.errors ul').append('<li>El <a href="#id_phone">teléfono</a> no ha sido completado</li>');
	error_signal = 1;
    }
    if (($('#id_phone').attr('value') != "") && (checkInternationalPhone($('#id_phone').attr('value')) == false)){
	$('#id_phone').parent().attr('class', 'error');
	$('.errors ul').append('<li>El <a href="#id_phone">teléfono</a> introducido no es válido</li>');
	error_signal = 1;
    }

    if ($('#id_address').attr('value') == ""){
	$('#id_address').parent().attr('class', 'error');
	$('.errors ul').append('<li>La <a href="#id_address">dirección</a> no ha sido completada</li>');
	error_signal = 1;
    }
    if ($('#id_cp').attr('value') == ""){
	$('#id_cp').parent().attr('class', 'error');
	$('.errors ul').append('<li>El <a href="#id_cp">código postal</a> no ha sido completado</li>');
	error_signal = 1;
    }
    if ($('#id_city').attr('value') == ""){
	$('#id_city').parent().attr('class', 'error');
	$('.errors ul').append('<li>La <a href="#id_city">ciudad</a> no ha sido completada</li>');
	error_signal = 1;
    }
    if ($('#id_body').attr('value') == ""){
	$('#id_body').parent().attr('class', 'error');
	$('.errors ul').append('<li>El <a href="#id_body">texto</a> no ha sido completado</li>');
	error_signal = 1;
    }

    if (error_signal == 0){
 	return true;
    }
    else {
	errors.css('display', 'block');
	return false;
    }

}


/*
$(document).ready(function(){
    $("#service_petition").submit(checkForm);
});
*/


