var AddUrlForm = 
{
    action : null,
    container: null,
    id_dom:null,
    temp:null,
    data_send:'',

    submit : function(){
		var aux = true;
        this.temp = $("#"+this.id_dom).find(':input');
        this.temp.each( function (){
                        if( this.type == 'radio' ){
                            if(this.checked == true){ AddUrlForm.data_send += this.name+'='+this.value+'&'; }
                            
                         }
                         else if(this.type == 'checkbox')
                         {
                            if(this.checked == true){ AddUrlForm.data_send += this.name+'='+this.value+'&'; }
                            else{ AddUrlForm.data_send += this.name+'='+0+'&';}
                         }else if(this.type == 'textarea')
                         {
                              AddUrlForm.data_send += this.name+'='+escape(this.value)+'&'; 
                         }                        
                         else
                         {  
                            AddUrlForm.data_send += this.name+'='+escape(this.value)+'&'; 
                         }
                         });
		
			this.send();
    },
    send : function(){
            $.post(this.action,AddUrlForm.data_send, function(data){ 
                                        AddUrlForm.callback(data);
                                    } ,'html');
    },
    callback:function(data){
			alert(data);
               $('#'+this.container).load("wishlist.php", "", AddUrlForm.init);
			   $("#shoppingcart").load("include/shoppingcart.php");
                this.init(); 
    },
    init: function(param){
       this.data_send = '';
        if(!this.action){
            this.action     = param.action;
            this.container  = param.container;
            this.id_dom     = param.id_dom;
         }
        

        $("#btnSend").click(function(){
			if(document.forms[1].empresa.value == "" || document.forms[1].nombre.value == "" || document.forms[1].telefono.value == "" || document.forms[1].email.value == "")
			{
				alert("Los campos del formulario son obligatorios");
			}
			else if(!ValidateEmail(document.forms[1].email.value))
			{
				alert("El campo e-mail es invalido");
			}
			else
			{
				$('#'+AddUrlForm.id_dom).submit();
			}
            
        }) ;
       
    /*    
         $("#aceptar_bottom").click(function(){
            $('#'+AddUrlForm.id_dom).submit();
        });
         */
        $('#'+AddUrlForm.id_dom).submit( function(){
			AddUrlForm.submit();
            $('#'+AddUrlForm.id_dom).unbind();
			return false;
          });
    }
}

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					
	}

function ValidateEmail(emailID){
	if (echeck(emailID)==false){
		return false
	}
	return true
 }

$(document).ready( 
	function()
	{
        AddUrlForm.init({"action":"send_wishlist.php","id_dom":"myForm","container":"centerContentDiv"});
	}
);
