/*##################################################################################
 *##
 *## Initialization code: It starts data capture
 *##
 *##
 **********************************************************************************/
 var Cookies=null;
 var Config=null;
 var Associations=null;
 var Data=null;
 var Personal=null;
 
 
 
 /*
 * Main function Get configuration, parse all forms and send socket to print form
 * You must begin to read here
 */
 function GetAndSendConf(){
 
 Configure();
 
 /*if(Config["FormName"])
    document.write("Client form name: <b>"+Config["FormName"]+"</b>\n<br>");*/
 ParseData();  		//Datos
 ParsePersonalConf();	//Datos perosonales
 ParseCatConf(); 	//Categorias
 ParseCondConf();	//Terminos y Condiciones
 
 if (Config["OrderPrint"]=='all'){
 	ParseAllConf();
 }
 
 ParseStyleConf();	//Estilos
 //alert("Parsed names, ok");
 
 ParsePreFilled();
 
 
 MakeSelfConf();
 //alert("Make self conf, ok");
 
 
 }
 
 
 /*
 * Inicializates configuration
 */
 function Configure(){
 /* Init variables */
 Config = new Object();
 Config["ClientSubmitButton"]="Envio";
 Config["ConfigDataForm"]="INTEGRATION_Data";
 ParseConfig();
 }
 
 /*
 * Parse Configuration Form
 */
 function ParseConfig(){
 var i,j;
 var form_ourdata;
 var form_current;
 
 form_current = document.forms["INTEGRATION_ConfigForm"];
 if (form_current) {
 
 //alert(form_current.name);
 //alert(form_current.elements.length);
 
 //document.write("Examinando formulario de configuracion " + form_current.name + "\n<br>");
 /* What fields are in this form? */
  for(j=0; j < form_current.elements.length; j++,form_ourdata=form_current){
    switch(form_current.elements[j].type){
        case "hidden":
           switch(form_current.elements[j].name){
 		case "Affiliate":
 		    	Config["Affiliate"]=form_current.elements[j].value;
             	    	break;
             	case "ClientSubmitButton":
             		Config["ClientSubmitButton"]=form_current.elements[j].value;
             		break;
             	case "Language":
             		Config["Language"]=form_current.elements[j].value;
             		break;
 		case "OrderPrint":
 			Config["OrderPrint"]=form_current.elements[j].value;
 			break;
             	/*case "Compat":
             	    document.write("<b>Parsing Compat " + form_current.elements[j].value +"</b><br>");
             		ParseCompat(form_current.elements[j].value);
             		break;*/
             	/*case "ProccessingType":
             		switch(form_current.elements[j].value){
             			case "CoPixel": // Co-Registro por pixel transparente
             			case "PostPixel": // Post-Registro por pixel transparente
             				Config["ProccessingType"]=form_current.elements[j].value;
             				break;
                   }
 						break;*/
 				default:
 				   s_var = form_current.elements[j].name;
 				   // se compara en minususculas
 				   if(s_var.toLowerCase().indexOf("compat")!=-1)
 				   //if(s_var.indexOf("Compat")!=-1)
 				   {
 					  //alert("<b>Parsing Compat " + form_current.elements[j].value +"</b><br>");
                       //document.write("<b>Parsing Compat " + form_current.elements[j].value +"</b><br>");
             		  //antes ParseCompat(form_current.elements[j].value);
             		  ParseCompat(form_current.elements[j].value, s_var);
 			       }
 			       else
 			       {
                       /*DebugEmail OrderPrint*/
                       //document.write("<b>"+form_current.elements[j].name+" "+form_current.elements[j].value+"</b><br>");
                       if(form_current.elements[j].name!="")
                     	Config[form_current.elements[j].name]=form_current.elements[j].value;
 			        }
             }
           }
 }
 }
 }
 
 
 function ParsePreFilled(){
 var i,j;
 Cookies = new Object();
 var cookieData = "";
 
 var form_current;
 
 form_current = document.forms["INTEGRATION_PreFilled"];
 if (form_current) {
 
 //alert(form_current.name);
 //alert(form_current.elements.length);
 
 	// si existe el formulario de prefilled entonces metemos en cookies los valores.
 		   for(j=0; j < form_current.elements.length; j++,form_ourdata=form_current){
 			   //alert(form_current.elements[j].name + "=>" + form_current.elements[j].value);
 			   Cookies[form_current.elements[j].name] = form_current.elements[j].value;
 
 			   name = form_current.elements[j].name;
 
 			   if(j==0) { cookieData+=name + ":::" + form_current.elements[j].value; }
 			   else     { cookieData+="#" + name + ":::" + form_current.elements[j].value; }
 
 		   }
 
 		   document.cookie="INTEGRATION_cookie="+escape(cookieData);
 
 }
 }
 
 
 
 
 
 /*
 * Parse Any Compatibility Form
 */
 function ParseCompat(nameForm, s_var){
 var i,j;
 var typeCompat;
 var aux = "";
 
 
 for(i=0; i < document.forms.length; i++){
 	//if(document.forms[i].name == "COMPAT_"+nameForm){
 	// Se compara todo en minusculas por si se equivoca al poner el formulario
 
     if(typeof(document.forms[i].name)=='string')
     if(document.forms[i].name.toLowerCase() == s_var.toLowerCase()){
       //document.write("Examinando formulario de compatibilidad " + document.forms[i].name  + "   Compat_"+ nameForm + " " + s_var + "\n<br>");
       /*Begin Function*/i
       //alert("function Integration"+nameForm+"Translation\(value\)\{ switch\(value\)");
       document.write("\<script>function Integration"+nameForm.replace(/[\[\]]/gi,'_')+"Translation\(value\)\{ switch\(value\)\{ ");
       //lo comento document.write("\"function Integration"+nameForm+"Translation\(value\)\{ switch\(value\)\{ ");
        /* What fields are in this form? */
       s_var="Compat_" + nameForm.replace(/[\[\]]/gi,'_');
       //Config[s_var]="document." + Config["FormName"] + "." + nameForm + ".value = Integration"+nameForm+"Translation(_getValue(document."+ Config["FormName"] + "." + nameForm +"));";
       Config[s_var]="Integration"+nameForm.replace(/[\[\]]/gi,'_')+"Translation(_getValue(document."+ Config["FormName"] + ".elements['" + nameForm +"']));";
       for(j=0; j < document.forms[i].elements.length; j++){
       switch(document.forms[i].elements[j].type){
          case "submit":
             /* We don't want submit buttons in our config form */
             EraseFormInput(document.forms[i].elements[j]);
             break;
          case "hidden":
          	WriteCaseClause(document.forms[i].elements[j].name,document.forms[i].elements[j].value);
             break;
    	}
    	}
    	/*End Function*/
    	// lo comento document.write("\}return \"\";\} ");
     document.write("\} return \"\";\} \</script\>");
 	}
 }
 
 }
 
 
 /*
 * Parse Any Compatibility Form
 */
 function ParseCompat_noname(nameForm, s_var){
 var i,j;
 var typeCompat;
 var aux = "";
 
 var form_current;
 
 form_current = document.forms[s_var];
 if (form_current) {
 
 //alert('VARIABLE: '+form_current.name);
 //alert('VARIABLE: '+form_current.elements.length);
 
 
       //document.write("Examinando formulario de compatibilidad " + form_current.name  + "   Compat_"+ nameForm + " " + s_var + "\n<br>");
       /*Begin Function*/
       document.write("\<script>function Integration"+nameForm.replace(/[\[\]]/gi,'_')+"Translation\(value\)\{ switch\(value\)\{ ");
       //lo comento document.write("\"function Integration"+nameForm+"Translation\(value\)\{ switch\(value\)\{ ");
        /* What fields are in this form? */
       s_var="Compat_" + nameForm;
       //Config[s_var]="document." + Config["FormName"] + "." + nameForm + ".value = Integration"+nameForm+"Translation(_getValue(document."+ Config["FormName"] + "." + nameForm +"));";
       Config[s_var]="Integration"+nameForm+"Translation(_getValue(document."+ Config["FormName"] + "." + nameForm +"));";
       //alert('CONFIG['+s_var+']: '+Config[s_var]);
       //alert(document.forms["mosForm"].sexo);
 
       for(j=0; j < form_current.elements.length; j++){
       switch(form_current.elements[j].type){
          case "submit":
             /* We don't want submit buttons in our config form */
             EraseFormInput(form_current.elements[j]);
             break;
          case "hidden":
          	WriteCaseClause(form_current.elements[j].name,form_current.elements[j].value);
             break;
    	}
    	}
    	/*End Function*/
    	// lo comento document.write("\}return \"\";\} ");
     document.write("\} return \"\";\} \</script\>");
 }
 }
 
 
 
 
 function WriteCaseClause(ourvalue,origvalue){
    document.write(" case \""+origvalue+"\"\: return \""+ourvalue+"\";");
 }
 
 
 function ParseData(){
 var i,j;
 Associations = new Object();
 var form_current;
 
 form_current = document.forms["INTEGRATION_Data"];
 if (form_current) {
 
 //alert(form_current.name);
 //alert(form_current.elements.length);
 
 
 		//document.write("Examinando formulario de configuracion " + form_current.name + "\n<br>");
 		/* What fields are in this form? */
 		for(j=0; j < form_current.elements.length; j++){
 			switch(form_current.elements[j].type){
 				case "hidden":
 					if(form_current.elements[j].name!=""){
    					if(form_current.elements[j].value!=""){
 		              /* Must seek for a matching field on the client form */
 		              /* .name is client value */
 		              /* .value is our value   */
 			      //alert(form_current.elements[j].name);
 		              Associations[form_current.elements[j].value]=form_current.elements[j].name;
 		              //document.write("Asociacion "+form_current.elements[j].name + " => "+ form_current.elements[j].value +"\n<br>");		              
 		            }
    	          }
    		       break;
 				default:
 	           /* We don't want other input type in our config form */
 	           EraseFormInput(form_current.elements[j]);
 	           break;
 			}
 
 		}
  }
 }
 
 function ParseAllConf(){
 var i,j,num_form;
 
 if (!Associations['Fname']){ Personal['Fname']='Fname';}
 if (!Associations['Lname']){ Personal['Lname'] = 'Lname';}
 if (!Associations['Country']){ Personal['Country'] = 'Country';}
 if (!Associations['Country']&&!Associations['State']){Personal['State'] = 'State';}
 if (!Associations['address']){ Personal['address'] = 'address';	}
 if (!Associations['ZipCode']){ Personal['ZipCode'] = 'ZipCode';}
 if (!Associations['Sex']){ Personal['Sex'] = 'Sex';}
 if (!Associations['BirthYear']){ Personal['BirthYear'] = 'BirthYear';}
 if (!Associations['email']){ Personal['email'] = 'email';}
 if (!Associations['Profession']){ Personal['Profession'] = 'Profession';}
 if (!Associations['Education']){ Personal['Education'] = 'Education';}
 if (!Associations['Industry']){ Personal['Industry'] = 'Industry';}
 if (!Personal['ItemperRow']){ Personal['ItemperRow']='2';}
 
 
 for(i=1; i<=23;i++){
 	if (!Categories['c'+i]){Categories['c'+i]='print';}
 }
 if ((Config["Language"]).toLowerCase()=='es'){	
 	if (!Categories['c24']){Categories['c24'] = 'print';}
 	if (!Categories['c25']){Categories['c25'] = 'print';}
 	if (!Categories['c26']){Categories['c26'] = 'print';}
 	if (!Categories['c27']){Categories['c27'] = 'print';}
 }
 if (!Categories['ItemperRow']){Categories['ItemperRow']='3';}
 
 if (!Conditions['logo']){Conditions['logo']='print';}
 if (!Conditions['TextBox']){Conditions['TextBox']='print';}
 }
 
 
 function ParsePersonalConf(){
 var i,j;
 Personal = new Object();
 var form_current;
 
 form_current = document.forms["INTEGRATION_Personal"];
 if (form_current) {
 
 //alert(form_current.name);
 //alert(form_current.elements.length);
 
 		//document.write("Examinando formulario de configuracion " + form_current.name + "\n<br>");
 		/* What fields are in this form? */
 		for(j=0; j < form_current.elements.length; j++){
 			switch(form_current.elements[j].type){
 				case "hidden":
 					if(form_current.elements[j].name!=""){
    					if(form_current.elements[j].value!=""){
 		              Personal[form_current.elements[j].value]=form_current.elements[j].name;
 		              //document.write("Personal "+form_current.elements[j].name + " => "+ form_current.elements[j].value +"\n<br>");
 		            }
    	          }
    		       break;
 				default:
 	           /* We don't want other input type in our config form */
 	           EraseFormInput(form_current.elements[j]);
 	           break;
 			}
 
 		}
  }
 }
 
 function ParseCondConf(){
 var i,j;
 Conditions = new Object();
 var form_current;
 
 form_current = document.forms["INTEGRATION_Conditions"];
 if (form_current) {
 
 //alert(form_current.name);
 //alert(form_current.elements.length);
 
 		//document.write("Examinando formulario de configuracion " + form_current.name + "\n<br>");
 		/* What fields are in this form? */
 		for(j=0; j < form_current.elements.length; j++){
 			switch(form_current.elements[j].type){
 				case "hidden":
 					if(form_current.elements[j].name!=""){
    					if(form_current.elements[j].value!=""){
 		              Conditions[form_current.elements[j].value]=form_current.elements[j].name;
 		              //document.write("Conditions "+form_current.elements[j].name + " => "+ form_current.elements[j].value +"\n<br>");
 		            }
    	          }
    		       break;
 				default:
 	           /* We don't want other input type in our config form */
 	           EraseFormInput(form_current.elements[j]);
 	           break;
 			}
 
 		}
  }
 }
 
 function ParseCatConf(){
 var i,j;
 Categories = new Object();
 var form_current;
 
 form_current = document.forms["INTEGRATION_Categories"];
 if (form_current) {
 
 //alert(form_current.name);
 //alert(form_current.elements.length);
 
 		//document.write("Examinando formulario de configuracion " + form_current.name + "\n<br>");
 		/* What fields are in this form? */
 		for(j=0; j < form_current.elements.length; j++){
 			switch(form_current.elements[j].type){
 				case "hidden":
 					if(form_current.elements[j].name!=""){
    					if(form_current.elements[j].value!=""){
 		              Categories[form_current.elements[j].value]=form_current.elements[j].name;
 		              //document.write("Categories "+form_current.elements[j].name + " => "+ form_current.elements[j].value +"\n<br>");
 		            }
    	          }
    		       break;
 				default:
 	           /* We don't want other input type in our config form */
 	           EraseFormInput(form_current.elements[j]);
 	           break;
 			}
 
 		}
  }
 }
 
 function ParseStyleConf(){
 var i,j;
 Style = new Object();
 var form_current;
 
 form_current = document.forms["INTEGRATION_Styles"];
 if (form_current) {
 
 //alert(form_current.name);
 //alert(form_current.elements.length);
 
 		//document.write("Examinando formulario de configuracion " + form_current.name + "\n<br>");
 		/* What fields are in this form? */
 		for(j=0; j < form_current.elements.length; j++){
 			switch(form_current.elements[j].type){
 				case "hidden":
 					if(form_current.elements[j].name!=""){
    					if(form_current.elements[j].value!=""){
 		              Style[form_current.elements[j].value]=form_current.elements[j].name;
 		              //document.write("Styles "+form_current.elements[j].name + " => "+ form_current.elements[j].value +"\n<br>");
 		            }
    	          }
    		       break;
 				default:
 	           /* We don't want other input type in our config form */
 	           EraseFormInput(form_current.elements[j]);
 	           break;
 			}
 
 		}
  }
 }
 
 
 
 function MakeSelfConf(){
 
 var urlScript="http://www.correodirect.com/formsv2/integration.php?Status=ConfigureForm";
 var name;
 //var urlScript="http://desarrollo.correodirect.com/forms/integration.php?Status=ConfigureForm";
 var urlData="";
 var CookieData;
 
 for(name in Associations){
 	urlData+="&CFORM_"+name+"="+Associations[name];
 }
 for(name in Config){
 	urlData+="&CCONF_"+name+"="+Config[name];
 }
 
 for(name in Categories){
 	urlData+="&CCATE_"+name+"="+Categories[name];	
 }
 for(name in Conditions){
 	urlData+="&CCOND_"+name+"="+Conditions[name];
 }
 for(name in Personal){
 	urlData+="&CPERS_"+name+"="+Personal[name];
 }
 for(name in Style){
 	urlData+="&CSTYL_"+name+"="+Style[name];
 }
 /* Cookie value also added */
 //   alert("Have cookie config?");
 
 //   CookieData=getConfigCookieValues();
 //   for(name in CookieData)
 //     {
 //       alert("Have cookie "+name);
 //       urlData+="&CCKIE_"+name+"="+CookieData[name];
 //     }
 
 
 getlength=urlData.length;
 //alert("This will be how we'll call the script ["+urlScript+urlData+"]\nIt has "+getlength+" chars, maxchars=2000?");
 //document.write ("This will be how we'll call the script ["+urlScript+urlData+"]\nIt has "+getlength+" chars, maxchars=2000?");
 document.write("\<script src=\"" + urlScript + urlData + "\"\>\</script\>");
 
 
 }
 
 
 
 function MakeFromCookieSelfConf(){
 var urlScript="http://www.correodirect.com/formsv2/integration.php?Status=ConfigureForm";
 var name;
 //var urlScript="http://desarrollo.correodirect.com/forms/integration.php?Status=ConfigureForm";
 var urlData="";
 var CookieData;
 
 if(Associations!=null){
 	for(name in Associations){
 		urlData+="&CFORM_"+name+"="+Associations[name];
 	}
 }
 
 if(Config!=null){
 	for(name in Config){
 		urlData+="&CCONF_"+name+"="+Config[name];
 	}
 }
 
 if(Style!=null){
 	for(name in Style){
 		urlData+="&CSTYL_"+name+"="+Style[name];
 	}
 }
 
 
 /*if(Categories!=null){
 	for(name in Categories){
 		urlData+="&CCATE_"+name+"="+Categories[name];
 	}
 }
 
 for(name in Conditions){
 	urlData+="&CCOND_"+name+"="+Conditions[name];
 }
 for(name in Personal){
 	urlData+="&CPERS_"+name+"="+Personal[name];
 }
 */
 
 getlength=urlData.length;
 //alert("This will be how we'll call the script ["+urlScript+urlData+"]\nIt has "+getlength+" chars, maxchars=2000?");
 //document.write ("This will be how we'll call the script ["+urlScript+urlData+"]\nIt has "+getlength+" chars, maxchars=2000?");
 //alert(urlScript + urlData);
 document.write("\<script src=\"" + urlScript + urlData + "\"\>\</script\>");
 
 }
 
 
 
 function EraseFormInput(input){
 
 var newInput;
 var retvalue=false;
 var removed;
 var parent;
 if(input)
 {
   parent=input.parentNode;
   if(parent)
   {
     removed = parent.removeChild(input);
     if(removed)
     {
 	   //document.write(" <i>Eliminado: </i>"+input.name+"<br>");
 	   retvalue=true;
     }
     //else document.write("No existe<br>");
    }
 
 }
 return(retvalue);
 }
 
 function EraseFormInputs(input){
 var i;
 var retval=true;
 for(i=0;i<input.length;i++){
 	if(input[i]){
 		if(!EraseFormInput(input[i]))
           retval=false;
 	}
 }
 return(retval);
 }
 
 


 GetAndSendConf();