/*
 * Main function Call configuration. prepare submits and get Stored Data.
 */
 
 
 function DoAutoIntegration(){
 
 if(typeof(CustomConfigure)=="function"){
 	
 	CustomConfigure();
 }
 
 
 else{
 	//Associations = getCookieValues("INTEGRATION_cookie");
 	Cookies      = getCookieValues("INTEGRATION_cookie");
 	//esto quita la cookie de configuracion
 	//Config       = getCookieValues("INTEGRATION_CONFIG_cookie");
 
 }
 
 
 /*document.write("Javascript has:<br>");
 for(name in Config){ document.write("Config value:"+name+" value "+Config[name]+"\n<br>"); }
 for(name in Associations){ 	document.write("Associations value:"+name+" value "+Associations[name]+"\n<br>"); }
 for(name in Cookies){ document.write("Cookies value:"+name+" value "+Cookies[name]+"\n<br>"); }
 document.write("End config Javascript<br>");*/
 
 ConfigDocument();
 LookupDocument();
 
 LookupDocumentPreFilled();
 
 }
 
 
 
 function DoAutoRecovery()
 {
    var CookieData;
 
    if(typeof(CustomConfigure)=="function"){
       CustomConfigure();
    }
    else{
       Associations = getCookieValues("INTEGRATION_cookie");
       //Cookies      = getCookieValues("INTEGRATION_cookie");
       Config       = getCookieValues("INTEGRATION_CONFIG_cookie");
       Style        = getCookieValues("INTEGRATION_STYLE_cookie");
     }
 
 
    /*document.write("Recovering:<br>");
 
    for(name in Config)
    {
       document.write("Config value:"+name+" value "+Config[name]+"\n<br>");
    }
 
    for(name in Associations)
    {
       document.write("Associations value:"+name+" value "+Associations[name]+"\n<br>");
    }
 
    for(name in Style)
    {
       document.write("Style value:"+name+" value "+Style[name]+"\n<br>");
    }
 */
    /*for(name in Cookies)
    {
       document.write("Associations value:"+name+" value "+Cookies[name]+"\n<br>");
    }*/
 
    //document.write("End config Javascript<br>");
 
    MakeFromCookieSelfConf();
 
 
 }
 
 
 function _getCookie(name){
    var cookies;
    var ret_val=null;
 
    cookies=_getCookies();
    if(cookies)
       ret_val=cookies[name];
 
    return(ret_val);
 }
 
 
 function _getCookies()
 {
   var str_array;
 //   document.write("La cookie grande: "+document.cookie);
   str_array=_splitValuesCookies(document.cookie,";");
   for(cookie_name in str_array)
   {
      str_array[cookie_name]=unescape(str_array[cookie_name]);
 //       document.write("Cogida una cookie "+cookie_name+" unscaped <b>"+str_array[cookie_name]+"</b><br>");
   }
   return(str_array);
 }
 
 
 function _splitValuesCookies(str, split_char)
 {
   var val_array=null;
   var i;
   var string_array;
   var name_val_array;
 
   string_array=str.split(split_char);
   if(string_array.length>0)
     {
       val_array = new Object();
       for(i=0;i<string_array.length;i++)
 	{
 	  /* Split name=value pair */
 	  name_val_array=string_array[i].split("=");
 	  val_array[Trim(name_val_array[0])]=Trim(name_val_array[1]);
 	}
 
     }
 
   return(val_array);
 }
 
 
 
 
 function getCookieValues(name){
 	var a_cookie;
 	var a_values=null;
 	var a_pairs,a_name_value;
 	var i;
 	a_cookie=_getCookie(name);
 
 	if(a_cookie){
 		a_values=new Object();
 		a_pairs=a_cookie.split("#");
 		for(i=0;i<a_pairs.length;i++){
 			//a_name_value=a_pairs[i].split(":");
 			a_name_value=a_pairs[i].split(":::");
 			a_values[a_name_value[0]]=a_name_value[1];
 
 		}
 	}
 	return(a_values);
 }
 
 
 function ConfigDocument(){
 var i,j;
 var form_ourdata;
 
 //document.write("Configurando..." + document.forms.length);
 
 /* Need to search all forms */
 /*for(i=0; i < document.forms.length; i++){
 	// What fields are in this form?
 	for(j=0; j < document.forms[i].elements.length; j++,form_ourdata=document.forms[i]){
 		if(document.forms[i].elements[j].type == "submit"){
 			if(document.forms[i].elements[j].name == Config["ClientSubmitButton"]){
 				//document.write("We have the client button "+ document.forms[i].elements[j].name +" == "+ Config["ClientSubmitButton"] +"\n<br>");
 				SetSubmitFunction(form_ourdata,document.forms[i].elements[j]);
 			}
 		}
 	}
 }*/
 
 var name_our_form = Config["FormName"];
 
 //document.forms[name_our_form].onsubmit();
 if (document.forms[name_our_form] && document.forms[name_our_form].onsubmit)
  document.forms[name_our_form].origbksubmit = document.forms[name_our_form].onsubmit;
 SetSubmitFunction(document.forms[name_our_form],document.forms[0].elements[0]);
 
 }
 
 function ConfigCatSubCatSubmit(){
 var i,j;
 var form_ourdata;
 
 /* Need to search all forms */
 for(i=0; i < document.forms.length; i++){
 	/* What fields are in this form? */
 	for(j=0; j < document.forms[i].elements.length; j++,form_ourdata=document.forms[i]){
 		if(document.forms[i].elements[j].type == "submit"){
 			//if(document.forms[i].elements[j].name == Config["ClientSubmitButton"]){
 				//document.write("We have the client button "+ document.forms[i].elements[j].name +" == "+ Config["ClientSubmitButton"] +"\n<br>");
 				//alert(Config["SubmitCatSubCatUrl"]);
 				SetSubmitFunction(form_ourdata,document.forms[i].elements[j]);
 
 			//}
 		}
 	}
 }
 }
 
 
 
 function LookupDocument(){
 var i,j;
 var form_ourdata;
 var MatchedElement;
 var AssociatedElement;
 var RemoveElement;
 var SearchElement;
 var Cookies=null;
 
 Data = new Object();
 
 
 Cookies=getCookieValues("INTEGRATION_cookie");
 /*for(name in Cookies){
 	document.write("Cookie " + name + " value "+ Cookies[name]+ "\n<br>");
 }*/
 
 for(ElemName in Associations){
 	//document.write("Buscando en el documento..." + ElemName + "\n<br>");
 	AssociatedElement=Associations[ElemName];
 	if(AssociatedElement!="" && AssociatedElement!=null && AssociatedElement!="No association"){
 		SearchElement=AssociatedElement;
 		//document.write("<b>Associated element:</b>"+AssociatedElement);
 		/* We will try to remove unused fields */
 		//document.write("<b>Erase repeated is:</b>"+Config["EraseRepeated"]+"<br>");
 		if(Config["EraseRepeated"]=="true"){
 			RemoveElement=_getElement(ElemName);
 			if(RemoveElement[0]){
 				//document.write("<b>This is what we have:</b> "+ElemName+" => "+SearchElement+"<br>");
 				/* Removie it? */
 				document.write("Erasing elements "+RemoveElement.length+"<br>");
 				if(!EraseFormInputs(RemoveElement))
 					document.write("<b>Cannot remove:<br> "+RemoveElement[0].name+"<br>");
 			}
 		}
 	}
 	else{
 		SearchElement=ElemName;
 		//document.write("<b>Element: </b>"+SearchElement);
 	}
 
 	MatchedElement=_getElement(SearchElement);
 	if(MatchedElement){
 		Data[ElemName]=MatchedElement;
 		for(i=0;i<MatchedElement.length;i++)
 		   //document.write("Encontrado: ["+i+"] " + MatchedElement[i].name + "\n<br>");
 		if(Cookies!=null){
 			if(typeof(Cookies[ElemName])=="undefined"){
 				//document.write("No cookie for: ["+ElemName + "]\n<br>");
 				ElemName="";
 			}
 	        else{
 				//document.write("Cookie found for: "+ElemName+" value "+Cookies[ElemName]+ "\n<br>");
 				_setValue(MatchedElement,Cookies[ElemName]);
 			}
 		}
 	}
 	/*else
 		document.write("NO Encontrado\n<br>");*/
 }
 
 // La parte de INTEGRATION_PERSONAL se aņade al objeto Data
 for(ElemName in Personal){
 	//document.write("Buscando en el documento... " + ElemName + "\n<br>");
 	AssociatedElement=Personal[ElemName];
 	if(AssociatedElement!="" && AssociatedElement!=null && AssociatedElement!="No association"){
 		SearchElement=AssociatedElement;
 		//document.write("<b>Associated element: </b>"+AssociatedElement);
 		/* We will try to remove unused fields */
 		//document.write("<b>Erase repeated is: </b>"+Config["EraseRepeated"]+"<br>");
 		if(Config["EraseRepeated"]=="true"){
 			RemoveElement=_getElement(ElemName);
 			if(RemoveElement[0]){
 				//document.write("<b>This is what we have: </b> "+ElemName+" => "+SearchElement+"<br>");
 				/* Removie it? */
 				//document.write("Erasing elements "+RemoveElement.length+"<br>");
 				if(!EraseFormInputs(RemoveElement))
 					document.write("<b>Cannot remove:<br> "+RemoveElement[0].name+"<br>");
 			}
 		}
 	}
 	else{
 		SearchElement=ElemName;
 		//document.write("<b>Element:</b>"+SearchElement);
 	}
 
 	MatchedElement=_getElement(SearchElement);
 	if(MatchedElement){
 		Data[ElemName]=MatchedElement;
 		for(i=0;i<MatchedElement.length;i++)
 		//document.write("Encontrado: ["+i+"] " + MatchedElement[i].name + "\n<br>");
 		if(Cookies!=null){
 			if(typeof(Cookies[ElemName])=="undefined"){
 				//document.write("No cookie for: ["+ElemName + "]\n<br>");
 				ElemName="";
 			}
 	      else{
 				//document.write("Cookie found for: "+ElemName+" value "+Cookies[ElemName]+ "\n<br>");
 				_setValue(MatchedElement,Cookies[ElemName]);
 			}
 		}
 	}
 	/*else
 		document.write("NO Encontrado\n<br>");*/
 }
 
 }
 
 
 
 function LookupDocumentPreFilled()
 {
 var i,j;
 var MatchedElement;
 var RemoveElement;
 var SearchElement;
 var Cookies=null;
 
 // Si existe el formulario prerellenado, ejecutamos la busqueda.
 if(typeof(document.forms.INTEGRATION_PreFilled)=="object")
 {
 
 	Cookies=getCookieValues("INTEGRATION_cookie");
 
 	for(ElemName in Cookies){
 		//document.write("<br><br>Buscando en el documento..." + ElemName + "\n<br>");
 		//CookiesElement=Cookies[ElemName];
 		CookiesElement=ElemName;
 		if(CookiesElement!="" && CookiesElement!=null && CookiesElement!="No association"){
 			SearchElement=CookiesElement;
 			//document.write("<b>CookiesElement element:</b> "+CookiesElement + " NameElement: "+ElemName+"<br>");
 		}
 		else{
 			SearchElement=ElemName;
 		}
 
 		MatchedElement=_getElement(SearchElement);
 		//document.write("<i> Matched cookie " + SearchElement + " element:</i> "+MatchedElement);
 		if(MatchedElement){
 			/*Data[ElemName]=MatchedElement;
 			for(i=0;i<MatchedElement.length;i++)
 			   document.write("Encontrado: ["+i+"] " + MatchedElement[i].name + "\n<br>");
 			*/
 			if(Cookies!=null){
 				if(typeof(Cookies[ElemName])=="undefined"){
 					//document.write("No cookie for: ["+ElemName + "]\n<br>");
 					ElemName="";
 				}
 				else{
 					//document.write(" Cookie found for: "+ElemName+" value "+Cookies[ElemName]+ "\n<br>");
 					_setValue(MatchedElement,Cookies[ElemName]);
 				}
 			}
 		}
 
 	}
 }
 }
 
 
 function SaveInCookies(Form)
 {
   var i=0;
   var cookieData = "";
   var cookieConfig = "";
   var cookieStyle = "";
 
   var Cookies = null;
   var NewCookie = null;
   var CookiesStyle = null;
   var NewCookieStyle = null;
 
   NewCookie      = new Object();
   NewCookieStyle = new Object();
 
   Cookies=getCookieValues("INTEGRATION_cookie");
   for(name in Cookies)
   {
      NewCookie[name]=Cookies[name];
      //alert("Cogiend viejas cookies " + name + " " + NewCookie[name]);
   }
 
 
   //return false;
   if(Config!=null)
   {
     for(name in Config)
  	{
  	  //if(i==0) { cookieConfig+=name + ":" + Config[name]; }
  	  //else     { cookieConfig+="#" + name + ":" + Config[name];  }
  	  if(i==0) { cookieConfig+=name + ":::" + Config[name]; }
  	  else     { cookieConfig+="#" + name + ":::" + Config[name];  }
  	  i++;
 	}
 
   }
 
 
   if(Data!=null)
   {
       /* Meter en NewCookie los valores correctos de Data */
       /* Utilizar NewCookie en lugar de Data en este script */
      for(name in Data)
   	 {
 	    if(name!=null && Data[name]!="")
 	    {
 		   NewCookie[name] = Trim(_getValue(Data[name]));
 
 	       //if(i==0) { cookieData+=name + ":" + Trim(_getValue(Data[name])); }
 	       //else     { cookieData+="#" + name + ":" + Trim(_getValue(Data[name])); }
 	       //i++;
 	    }
 	 }
 
   }
 
   for(name in Categories)
   {
      if(name!=null && (Categories[name]!="" && name!="ItemperRow"))
 	 {
 	 	switch(name){
 		    	case 'c24': name='c155';		    	
 		    		break;
 		    	case 'c25': name='c198';
 		    		break;
 		    	case 'c26': name='c199';
 		    		break;
 		    	case 'c27': name='c200';
 		    		break;
 		    }
 		//value=_getValue(Categories[name]);
 		s_categorie_checked = "Form." + name + '.checked';
 		if(eval(s_categorie_checked)==true)
 		{
 		   //value=Trim(value);
 		   NewCookie[name] = "1";
 		   //if(i==0) { NewCookie[name] = "1"; }
 		   //else     { cookieData+="#" + name + ":" +"=1"; }
 		   //i++;
 
 		}
 	 }
 
   }
 
   i=0;
   if(Style!=null)
   {
     for(name in Style)
  	{
 
  	  //if(i==0) { cookieStyle+=name + ":" + Style[name]; }
  	  //else     { cookieStyle+="#" + name + ":" + Style[name];  }
  	  if(i==0) { cookieStyle+=name + ":::" + Style[name]; }
  	  else     { cookieStyle+="#" + name + ":::" + Style[name];  }
 
  	  i++;
 
 	}
 
   }
 
   else
   {
 	  CookiesStyle=getCookieValues("INTEGRATION_STYLE_cookie");
 	  for(name in CookiesStyle)
 	  {
 		 NewCookieStyle[name]=CookiesStyle[name];
 
  	     if(i==0) { cookieStyle+=name + ":::" + NewCookieStyle[name]; }
  	     else     { cookieStyle+="#" + name + ":::" + NewCookieStyle[name];  }
 
  	     //if(i==0) { cookieStyle+=name + ":" + NewCookieStyle[name]; }
  	     //else     { cookieStyle+="#" + name + ":" + NewCookieStyle[name];  }
  	     i++;
 	  }
   }
 
   i=0;
   for(name in NewCookie)
   {
      if(name!=null && NewCookie[name]!="")
      {
 		//if(i==0) { cookieData+=name + ":" + NewCookie[name]; }
         //else     { cookieData+="#" + name + ":" + NewCookie[name]; }
 		if(i==0) { cookieData+=name + ":::" + NewCookie[name]; }
         else     { cookieData+="#" + name + ":::" + NewCookie[name]; }
         i++;
      }
   }
 
 
   //cookieConfig+="#INTEGRATION_DataSent:yes";
   cookieConfig+="#INTEGRATION_DataSent:::yes";
 
   document.cookie="INTEGRATION_cookie="+escape(cookieData);
   document.cookie="INTEGRATION_CONFIG_cookie="+escape(cookieConfig);
   document.cookie="INTEGRATION_STYLE_cookie="+escape(cookieStyle);
 
   //Form.submitOld();
   return(false);
 
 }
 
 var lastSubmitClicked = null;
 
 function HandleSubmitsOnclick(Form){
    for(i=0; i<Form.elements.length; i++){
       if(Form.elements[i].type == "submit" || Form.elements[i].type == "image") {
          if(typeof Form.elements[i].onclick == "function"){
             Form.elements[i].onclickOld = Form.elements[i].onclick;
          }
          Form.elements[i].onclick = function (){
             lastSubmitClicked = this;
             if(typeof this.onclickOld == "function"){
                var result=this.onclickOld();
                if(!result) lastSubmitClicked = null;
                return result;
             }
             return true;
          }
       }
    }
 }
 
 
 function PrepareData(Form){
   var i=0;
   var urlData="?";
   //var url;
   //var scriptName="JoinMemberDebug.php";
   var image,value,readytosubmit=false;
   var submit_func=null;
   var s_categorie_checked = false;
 
   if(Data!=null)
   {
     if(Config['OnSubmitCall'])
 	{
 	  //alert("We have a submit call "+Config['OnSubmitCall']);
 	  submit_func=eval(Config['OnSubmitCall']);
           //alert(Config['OnSubmitCall']);
           if(submit_func)
 	  {
 	     if(submit_func()) {  readytosubmit=true; }
 	     else  { readytosubmit=false; return false; }
 	  }
 
 	}
     else readytosubmit=true;
 
     // aqui la funcion de JS de CD
     readytosubmit = chequeo(Form);
     if (readytosubmit && Form.origbksubmit) {readytosubmit = readytosubmit && Form.origbksubmit()};
 
     if(readytosubmit)
 	{
 	  for(name in Data)
 	  {
 	     if(name!=null && Data[name]!="")
 		 {		 //alert('name:'+name);	
 			//en el obj. Associations esta el nombre del formulario
 			s_var = Associations[name];
 			if (typeof(s_var)!="undefined") s_var = s_var.replace(/[\[\]]/gi,'_');
 			s_var = "Compat_" + s_var;
             //alert(Associations[name]);
             // Si existe una funcion de conversion la ejecutamos en lugar de coger el valor
             // del objeto.
 			if(Config[s_var]!=null || typeof(Config[s_var])!="undefined")
 			{ 
  			   //alert(Config[s_var]); 
 			   value = eval(Config[s_var]);
 		    }
 
 		    else
 		    {
 			   value=_getValue(Data[name]);
 		    }
 
 		    if(typeof(value)!="undefined" && value!="")
 		    {
 		       value=Trim(value);
 		       if(i==0) { urlData=urlData + name + "=" + escape(value); }
 		       else     { urlData=urlData + "&" + name + "=" + escape(value); }
 		       i++;
 		    }
 		 }
 	  }
 	
 	  for(name in Categories)
 	  {
 
 	     if(name!=null && (Categories[name]!="" && name!="ItemperRow"))
 		 {	
 		    switch(name){
 		    	case 'c24': name='c155';		    	
 		    		break;
 		    	case 'c25': name='c198';
 		    		break;
 		    	case 'c26': name='c199';
 		    		break;
 		    	case 'c27': name='c200';
 		    		break;
 		    }
 		    //value=_getValue(Categories[name]);
 		    s_categorie_checked = "Form." + name + '.checked';
 		    //if(typeof(value)!="undefined" && value!="")
 		    if(eval(s_categorie_checked)==true)
 		    {
 		       //value=Trim(value);
 		       if(i==0) { urlData=urlData + name + "=1"; }
 		       else     { urlData=urlData + "&" + name + "=1"; }
 		       i++;
 
 		    }
 		 }
 	  }
 			
       // Le concatenamos el afiliado.
       if (Config["Affiliate"]=="") Config["Affiliate"] = 0;
       if(i==0) { urlData=urlData + "Affiliate="  + Config["Affiliate"]; }
       else     { urlData=urlData + "&Affiliate=" + Config["Affiliate"]; }
 	
       /*alert("Submitting...");
 	  url="http://desarrollo.correodirect.com/membmanager/"+scriptName+urlData+"&debugEmail="+escape("gaguilar@i-network.com");
 	  
 	  //AddFormInput(Form,"MAGIC_DataSent","true");
 	  //Form.action="Magic.php?MAGIC_DataSent=true;
 	  image=new Image();
 	  if(Form) { image.onload=function(){Form.submitOld();};  }
 	  image.src=url;*/
 		
 	  return urlData;
 	}
 
   }
   return false;
 }
 
 function SaveCatSubCatAndSubmit(Form)
 {
    //alert("%%%n SaveCatSubCatAndSubmit%%%");
 
    var i = 0;
    var urlData = "";
    var urlCategories = "";
 
    if (chequeoForm(Form)==false) return (false);
 
    // se recogen las variables que se le pasan al script que graba los datos de las
    // subcategorias: email y Affiliate. Por si acaso no funcionara el traspaso de cookies
    // en el navegador del cliente las cogemos directamente del formulario.
    if(Associations["email"]=="undefined") { var email=Form.email.value; urlData+="?email="+email; }
    else { urlData+="?email="+Associations["email"]; }
 
    if(Config["Affiliate"]=="undefined") { var Affiliate=Form.Affiliate.value; urlData+="&Affiliate="+Affiliate; }
    else { urlData+="&Affiliate="+Config["Affiliate"]; }
 
 
    // Recuperamos los elementos chequeados recorriendo todo el formulario.
    for(i=0; i < Form.length; i++){
 	   switch(Form.elements[i].type)
 	   {
 		   // solo las que empiezan por c y con value=1 (categorias ocultas)
 		   case 'hidden':
 		      if(Form.elements[i].name.indexOf("c")==0 && Form.elements[i].value==1)
 		         urlCategories+="&"+Form.elements[i].name+"=1";
 		      break;
 		   case 'radio':
 		   case 'checkbox':
 		      if (Form.elements[i].checked==true) urlCategories+="&"+Form.elements[i].name+"=1";
 		      break;
 
        }
 
    }
 
    url = urlDataSent+urlDataScriptSent+urlData+urlCategories;
    image=new Image();
    image.src=url;
    //alert(url);
    //alert(urlRedirect);
    Form.action = urlRedirect;
 
    if(Form)
    {
 	     image.onload=function(){ Form.submitOld();};
    }
 
 }
 
 function SendData(Form)
 {
 //alert('senddata');
   var i=0;
   var urlData="";
   var url;
   //var scriptName="JoinMemberDebug.php";
   var image,value,readytosubmit=false;
   var submit_func=null;
 
   urlData=PrepareData(Form);
 
   if(urlData!=false)
   {
      //alert("Submitting...");
      //url="http://desarrollo.correodirect.com/membmanager/"+scriptName+urlData+"&debugEmail="+escape("gaguilar@i-network.com");
      //AddFormInput(Form,"MAGIC_DataSent","true");
 	 //Form.action="Magic.php?MAGIC_DataSent=true;
 
      url=urlDataSent+urlDataScriptSent+urlData;
      
 
      // si esta asignada la variable de configuracion DebugEmail, asumimos que esta en modo Debug.
      // si se toma otra decision, hay que borrar esta linea y cambiar la funcion _SetJoinMemberDestination()
      if(Config["DebugEmail"]!="" && Config["DebugEmail"]!=null && typeof(Config["DebugEmail"])!="undefined") url+="&debugEmail="+escape(Config["DebugEmail"]);
 	
      image=new Image();
 	 if(Form)
 	 {
 	    image.onload=function(){ Form.submitOld(); };
 	 }
 	 image.src=url;
 //alert(url);
   }
 
 
 }
 
 
 function SaveInCookiesSendDataAndSubmit(Form)
 {
 	
   var i=0;
   var urlData="";
   var url;
   //var scriptName="JoinMemberDebug.php";
   var image,value,readytosubmit=false;
   var submit_func=null;
   var obligatory_coreg = 1;
 
   if ((Config["CoregMandatory"]=='false')||(Config["CoregMandatory"]=='0')) obligatory_coreg=0;
 
   urlData=PrepareData(Form);	
 
   //alert(Form.infocomercial.checked);	
   //alert(obligatory_coreg);
 
   if ((obligatory_coreg)&&(!Form.infocomercial.checked)) return false; 
 
   if ((!obligatory_coreg)&&(!Form.infocomercial.checked)) { 
     if (urlData==false) return false; else return true; 
   }
 
 
   if(urlData!=false)
   {
 
      // si esta asignada la variable de configuracion DebugEmail, asumimos que esta en modo Debug.
      // si se toma otra decision, hay que borrar esta linea y cambiar la funcion _SetJoinMemberDestination()
 
       url=urlDataSent+urlDataScriptSent+urlData;      
       if(Config["DebugEmail"]!="" && Config["DebugEmail"]!=null && typeof(Config["DebugEmail"])!="undefined") url+="&debugEmail="+escape(Config["DebugEmail"]);
 
       // To know the user came from this implementation type
       url+= "&regtype=1";
 
 	  //image=new Image();
 	  //if(Form) {  SaveInCookies(Form); }
       //SaveInCookies(Form);
       image=new Image();
       //image.onload = Form.origbksubmit();
       image.src=url;
 //alert(url);
 //document.write(url);
 //Form.origbksubmit();
 
       /* pixel de wunderloop */
       var a_trans_prof_nueva = {'p1':'10047', 'p2':'10048', 'p5':'10049', 'p13':'10050', 'p18':'10051', 'p19':'10052', 'p21':'10053', 'p25':'10054', 'p0':'10055', 'p6':'10056', 'p7':'10057', 'p8':'10058', 'p10':'10059', 'p11':'10060', 'p12':'10061', 'p14':'10062', 'p15':'10063', 'p16':'10064', 'p20':'10065','p22':'10066', 'p23':'10067', 'p24':'10068', 'p26':'10069', 'p27':'10070', 'p28':'10071', 'p29':'10072', 'p31':'10073', 'p33':'10074', 'p35':'10075', 'p37':'10076', 'p30':'10077', 'p32':'10078', 'p34':'10079', 'p36':'10080'};
 
       var a_trans_prof = {'p1':'10007', 'p2':'10008', 'p5':'10009', 'p13':'10010', 'p18':'10011', 'p19':'10012', 'p21':'10013', 'p25':'10014', 'p0':'10015', 'p6':'10016', 'p7':'10017', 'p8':'10018', 'p10':'10019', 'p11':'10020', 'p12':'10021', 'p14':'10022', 'p15':'10023', 'p16':'10024', 'p20':'10025','p22':'10026', 'p23':'10027', 'p24':'10028', 'p26':'10029', 'p27':'10030', 'p28':'10031', 'p29':'10032', 'p31':'10033', 'p33':'10034', 'p35':'10035', 'p37':'10036', 'p30':'10037', 'p32':'10038', 'p34':'10039', 'p36':'10040'};
 
       var a_trans_edu = {'e1':'10082','e2':'10083','e3':'10084','e4':'10085','e5':'10086','e6':'10087','e0':'10088'};
 
       var wltmpsex = _getValue(Data['Sex']); var wlsex='';
       var randomnum = Math.floor(Math.random()*99999); 
       var wlbirthyear = _getValue(Data['BirthYear']);
       var wlzipcode = _getValue(Data['ZipCode']);
 
       if (wltmpsex=='F') {wlsex='10044';} else {wlsex='10043';}
 
       var wltmpprof = _getValue(Data['Profession']);
       var wlprof = a_trans_prof['p'+wltmpprof];
 
       var wltmpedu = _getValue(Data['Education']);
       var wledu = a_trans_edu['e'+wltmpedu];
 
       var wlurl = 'http://tr.alant.antevenio.com/Cnt/antevenio/CD/questionnaire?d='+randomnum+'&ANT01=10041='+wlbirthyear+';10045='+wlzipcode+';'+wlsex+'=1;'+wlprof+'=1;'+wledu+'=1;';
 
       var wlimage = new Image();
       wlimage.src = wlurl;
 
       var coRegister='';
       //var coRegister='Alico';
 
       if ((typeof(coRegister)!='undefined')&&(coRegister!='')) { 
         // Send Coregister Pixel 
         var urlCoRegister = PrepareCoRegister(coRegister);
 	if (urlCoRegister!='') {
           var coimage = new Image();
           coimage.src = urlCoRegister;
 	}
       }
 
 
       // ejecuta el submit original del formulario
 	  if(Form)
 	  {
 
              image.Form = Form;
 	     	 image.onload=function() {
 				 if(lastSubmitClicked){
 					   Form.onsubmit = function () { return true; };
 					   lastSubmitClicked.onclick = function () { return true; };
 					   setTimeout("lastSubmitClicked.click();",200);
 				 }
 				 else{
 					   this.Form.submitOld();
 				 }
              }
 
              image.onerror=function(){
                 this.Form.submitOld();
 	         }
 
 	          SaveInCookies(Form);
 	   }
 
 
 
   }
 
   return false;
 
 
 }
 
 function _getValue(input){
   var i;
   var input_type;
 
 
   if(input.length>0)
   {
      input_type=input[0].type;
      for(i=0;i<input.length;i++)
 	   if(input_type!=input[i].type)
 	   {
 	      //document.write("BAD type in input\n<br>");
 	      return(false);
 	   }
   }
   else
   {
       //document.write("Not elements in input\n<br>");
       return(false);
   }
 
   //alert("_getValue input longitud" + input.length + " y type " + input[0].type);
 
   switch(input_type)
   {
     case 'text':
     case 'hidden':
     case 'textarea':
     case 'password':
       return input[0].value; /* Devolvemos el valor de la primera solamente */
       break;
 
     case 'select-one': /* Solamente la primera */
       if(input[0].selectedIndex>=0)
 	     return input[0].options[input[0].selectedIndex].value;
       else
 	     return "None selected";
       break;
 
     case 'checkbox':
       for(i=0;i<input.length;i++)
 	  {
 	     if(input[i].checked==true) return input[i].value;
 	  }
       break;
 
     case 'submit':
     case 'button':
     case 'reset':
     case 'image':
     case 'file':
       return 'Unhandled';
       break;
 
     case 'radio':
       for(i=0;i<input.length;i++)
 	  {
 	     if(input[i].checked==true) return input[i].value;
 	  }
 
       break;
 
     default:
       if((input[0] != null) && (input[0].type == 'radio')){
 	  for(i=0; i<input.length; i++){
 	     if(input[i].checked) return input[i].value;
 	  }
       }
       else{
 	  return input.value;
       }
     }
   return '';
 }
 
 
 function _setValue(input,input_value){
   var i,j;
   var ret_val=false;
   var input_type;
 
   if(input.length>0)
     {
       input_type=input[0].type;
       for(i=0;i<input.length;i++)
 	     if(input_type!=input[i].type) return(false);
     }
   else
     return(false);
 
 
   switch(input_type)
     {
     case 'text':
     case 'hidden':
     case 'textarea':
     case 'password':
 
       for(i=0;i<input.length;i++) input[i].value=input_value;
       ret_val=true;
       break;
 
     case 'select-one':
       for(i=0;i<input.length;i++)
 	  for(j=0;j<input[i].options.length;j++)
 	  {
 	    if(input[i].options[j].value==input_value)
 	    {
 		   //document.write("Select: "+input[i].options[j].value+"=="+input_value+" VALOR: "+input[i].options[j].text+"\n<br>");
 		   input[i].selectedIndex=j;
 		   ret_val=true;
 	    }
 	  }
       break;
 
     case 'checkbox':
       for(i=0;i<input.length;i++)
 	  input[i].checked=input_value;
       ret_val=true;
       break;
 
     case 'submit':
     case 'button':
     case 'reset':
     case 'image':
     case 'file':
       ret_val=false;
       break;
 
     case 'radio':
     for(i=0;i<input.length;i++)
 	{
 	  //document.write("Input Value: "+input[i].value+"=="+input_value+"\n<br>");
 	  //alert("Esto es un radioooo " + input[i].value +" " + input_value);
 	  if(input[i].value==input_value)
 	    {
 	      input[i].checked=true;
 	      ret_val=true;
 	    }
 	}
 
       break;
     default:
       document.write(input_type+" NOT HANDLED!!!<br>\n");
     }
   return(ret_val);
 }
 
 function _getElement(Name)
 {
   var i,j;
   var found_element=new Array();
   var type;
   //document.write("==>>BUSCANDO "+Name+" en el documento..." + document.forms.length+ "\n<br>");
   found_element.length=0;
   /* Need to search all forms */
    for(i=0; i < document.forms.length; i++)
      {
 
        //comentado cris.if(document.forms[i].name != "INTEGRATION_ConfigForm" && document.forms[i].name != "INTEGRATION_Data" && document.forms[i].name != "INTEGRATION_Personal")
        if(document.forms[i] != document.forms["INTEGRATION_ConfigForm"] && document.forms[i] != document.forms["INTEGRATION_Data"] && document.forms[i] != document.forms["INTEGRATION_Personal"] && document.forms[i] != document.forms["INTEGRATION_PreFilled"])
 	 {
   	   //document.write("Examinando formulario " + document.forms[i].name + "\n<br>");
   	   //document.write("El formulario tiene " + document.forms[i].elements.length + " campos\n<br>");
   	   //document.write("Examinando... \n<br>");
 	   /* What fields are in this form? */
 	   for(j=0; j < document.forms[i].elements.length; j++)
 	     {
  	       //document.write("Buscando " + document.forms[i].elements[j].name + " => " + Name +"\n<br>");
 	       if(document.forms[i].elements[j].type != "submit" && document.forms[i].elements[j].name==Name)
 		 {
   		   //document.write("Encontrado " + document.forms[i].elements[j].name + "\n<br>");
 		   if(found_element.length==0)
 		     {
 		       //document.write("Primer elemento del tipo "  + document.forms[i].elements[j].type +"\n<br>");
 		       found_element[found_element.length]=document.forms[i].elements[j];
 		       type=document.forms[i].elements[j].type;
 		     }
 		   else
 		     {
 		       if(document.forms[i].elements[j].type==type)
 			 {
 			   found_element[found_element.length]=document.forms[i].elements[j];
 
 			 }
 		     }
 		 }
 	     }
 	 }
      }
 
    //document.write("==>ENCONTRADO " + Name + " with " +found_element.length + " elementos\n<br>");
    return(found_element);
 }
 
 
 /*
    PURPOSE: Remove leading blanks from our string.
    IN: str - the string we want to LTrim
 */
 function LTrim(str)
 {
    var whitespace = new String(" \t\n\r");
 
    var s = new String(str);
 
    if (whitespace.indexOf(s.charAt(0)) != -1) {
       // We have a string with leading blank(s)...
 
       var j=0, i = s.length;
 
       // Iterate from the far left of string until we
       // don't have any more whitespace...
       while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
          j++;
 
       // Get the substring from the first non-whitespace
       // character to the end of the string...
       s = s.substring(j, i);
    }
    return s;
 }
 
 
 /*
    PURPOSE: Remove trailing blanks from our string.
    IN: str - the string we want to RTrim
 
 */
 function RTrim(str)
 {
    // We don't want to trip JUST spaces, but also tabs,
    // line feeds, etc.  Add anything else you want to
    // "trim" here in Whitespace
    var whitespace = new String(" \t\n\r");
 
    var s = new String(str);
 
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
       // We have a string with trailing blank(s)...
 
       var i = s.length - 1;       // Get length of string
 
       // Iterate from the far right of string until we
       // don't have any more whitespace...
       while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
          i--;
 
 
       // Get the substring from the front of the string to
       // where the last non-whitespace character is...
       s = s.substring(0, i+1);
    }
 
    return s;
 }
 
 /*
    PURPOSE: Remove trailing and leading blanks from our string.
    IN: str - the string we want to Trim
    RETVAL: A Trimmed string!
 */
 function Trim(str)
 {
    return RTrim(LTrim(str));
 }
 
 
 /*****************************************************************************
 NUEVO COREGISTRO */
 
 function PrepareCoRegister(coRegister) {
   var urlCoRegister='';
   var CoRegisterFunction='';
 
   // con el typeof vemos si existe la variable evitando un error en JS en el caso que el form no tenga coregistro.
   if ((typeof(coRegister)!='undefined')&&(coRegister!='')) { 
     CoRegisterFunction = eval('CoRegisterGetUrl_'+coRegister);
     //alert(CoRegisterFunction);
     if (CoRegisterFunction) { urlCoRegister = CoRegisterFunction(); }
   }
   return urlCoRegister;
 }
 
 function CoRegisterGetUrl_Alico() {
 
   var urlDataAlico ='http://www.correodirect.com/formsv2/CoRegister/alico/rec.php';
   var urlData='';
   
   urlData += '?email='+_getValue(Data['email']);
   urlData += '&fname='+_getValue(Data['Fname']);
   urlData += '&lname='+_getValue(Data['Lname']);
   urlData += '&sex='+_getValue(Data['Sex']);
   urlData += '&zipcode='+_getValue(Data['ZipCode']);
   urlData += '&birthyear='+_getValue(Data['BirthYear']);
   urlData += '&telefono='+document.getElementById('telefono').value;
   urlDataAlico = urlDataAlico + urlData;
 
 //if(_getValue(Data['coRegister_checkbox'])=='1')
   return urlDataAlico;
 
 }
 
 
 


		function SetSubmitFunction(Form,SubmitButton){

		 //if (typeof Form.Country != "undefined"){
		 //cd_Country indica que el campo Country es el de CD, por si la web tiene un campo Country
		 if (typeof Form.cd_Country != "undefined"){
		 	selectedcountry(21);
		 }
		 //if (typeof Form.submit == "undefined"){
		 if(Form && SubmitButton)
		 {
		 	//AQUII
                        //Form.submitOld=Form.submit;
			//alert (typeof Form.submit.name);
			if(typeof Form.submit.name == "undefined") Form.submit=function(){return(false);};
			Form.onsubmit=function(){ return SaveInCookiesSendDataAndSubmit(Form);}
                        HandleSubmitsOnclick(Form);
		 }
		 //}
		}

DoAutoIntegration();