// JavaScript Document
//var homepath="http://192.168.1.51/Joomla1_5/";
var homepath="http://www.imusausa.com/";

function sleepTimeOutDisplay(divId){	
	$(divId).style.display	=	'none';
}
function numericValue(evt) {
	
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));	
	
    if (charCode > 31 && (charCode < 48 || charCode > 57) || charCode==46) {
        //alert("Enter only numeric values\nThat is between 0-9\n in this field.");
        return false;
    }
    return true;
}
function process_one(frm,mode,frm_tab1,step){

	frm.mode.value=mode;
	var count	=	0;
	var errorMsg = Array();
	var result='false';
	var type_of_product='false';
	var WasThisItem='false';
	var WasItem='false';
	var IsThisProduct='false';
	var factors='false';
	var f_one=0;
	var f_two=0;
	var f_three=0;
	var f_four=0;
	var f_five=0;
	var f_six=1;
	var elemAll	=	frm.elements.length;
	//if($('form-tab1').style.visibility=='visible'){
		if(eval(frm.Title)){
			for (k=0; k<elemAll; k++){
				var elem	=	eval(frm.elements[k]);
				if(eval("frm.elements[k].id.match(/title_/)")){
					//alert("in");
					if(elem.checked==true && f_one!=1 ){
						f_one=1
						result='true';
					}
				}
			}
		
			if(result=='false'){
				errorMsg[count]="Select Title";
				count++;
			}
		}
		if(eval(frm.FirtsName)){
			if(frm.FirtsName.value==''){
				errorMsg[count]="Plesae provide First Name";
				count++;
			}
		}
		if(eval(frm.LastName)){
			if(frm.LastName.value==''){
				errorMsg[count]="Plesae provide Last Name";
				count++;
			}
		}
		
		if(eval(frm.Stree)){
			if(frm.Stree.value==''){
				errorMsg[count]="Plesae provide Stree";
				count++;
			}
		}
		if(eval(frm.City)){
			if(frm.City.value==''){
				errorMsg[count]="Plesae provide City";
				count++;
			}
		}
		if(eval(frm.State)){
			var state=frm.State;
			//if(state.options[state.selectedIndex].value==''){
				//errorMsg[count]="Plesae provide State";
				//count++;
			//}
		}
		if(eval(frm.ZipCode)){
			if(frm.ZipCode.value==''){
				errorMsg[count]="Plesae provide ZipCode";
				count++;
			}
		}
		/*if(eval(frm.Email)){
			if(frm.Email.value!=''){
				if(!validateEmail(frm.Email.value,1,1)){
					errorMsg[count]="Plesae provide valide email address";
					count++;	
				}
			}
		}*/
	//}
	if($('form-tab2').style.display=='block'){
		if(eval(frm.Categories)){
			for (k=0; k<elemAll; k++){
				var elem	=	eval(frm.elements[k]);
				if(eval("frm.elements[k].id.match(/type_of_product_/)")){
					//alert("in");
					if(elem.checked==true && f_two!=1 ){
						f_two=1
						type_of_product='true';
					}
				}
			}
		
			if(type_of_product=='false'){
				errorMsg[count]="Please select What type of product did you purchase?";
				count++;
			}
		}
		if(eval(frm.WasThisItem)){
			for (k=0; k<elemAll; k++){
				var elem	=	eval(frm.elements[k]);
				if(eval("frm.elements[k].id.match(/WasThisItem_/)")){
					//alert("in");
					if(elem.checked==true && f_three!=1 ){
						f_two=1
						WasThisItem='true';
					}
				}
			}
		
			if(WasThisItem=='false'){
				errorMsg[count]="Please select Where did you purchase this product?";
				count++;
			}
		}
	}
	if($('form-tab3').style.display=='block'){
		if(eval(frm.WasItem)){
			for (k=0; k<elemAll; k++){
				var elem	=	eval(frm.elements[k]);
				if(eval("frm.elements[k].id.match(/WasItem_/)")){
					//alert("in");
					if(elem.checked==true && f_four!=1 ){
						f_four=1
						WasItem='true';
					}
				}
			}
		
			if(WasItem=='false'){
				errorMsg[count]="Please select Was this item";
				count++;
			}
		}
		if(eval(frm.IsThisProduct)){
			for (k=0; k<elemAll; k++){
				var elem	=	eval(frm.elements[k]);
				if(eval("frm.elements[k].id.match(/IsThisProduct_/)")){
					//alert("in");
					if(elem.checked==true && f_five!=1 ){
						f_five=1
						IsThisProduct='true';
					}
				}
			}
		
			if(IsThisProduct=='false'){
				errorMsg[count]="Please select Is This Product";
				count++;
			}
		}
		
		for (k=0; k<elemAll; k++){
			var elem	=	eval(frm.elements[k]);
			if(eval("frm.elements[k].id.match(/factors_/)")){
				if(elem.checked==true){
					//alert(f_six);
					if(f_six > 2){
						factors='true';
					}else{
						f_six=f_six+1
					}
					
				}
			}
		}
	
		if(factors=='false'){
			errorMsg[count]="Please select factors most influenced your decision to purchase at list three";
			count++;
		}
	
		if(!frm.infoMail.checked){
			errorMsg[count]="Please select please send me information, offers  and updates from IMUSA!";
			count++;
		}
		if(!frm.Policy.checked){
			errorMsg[count]="Please select Online Privacy Policy";
			count++;
		}
		if(!frm.aceptaterminos.checked){
			errorMsg[count]="Please select terms and conditions of use";
			count++;
		}
	}
	if(errorMsg.length > 0){
		document.getElementById("errorDiv").style.display='block';
		document.getElementById("errorDiv").style.visibility='visible';
		showErrorMsg = document.getElementById("errorInnerDiv");
		showErrorMsg.style.display='block';
		showErrorMsg.innerHTML = "";
		for(i=0;i<errorMsg.length;i++){
			showErrorMsg.innerHTML += '<div class="errorList">'+errorMsg[i]+'</div>';
		}
		setTimeout("sleepTimeOutDisplay('errorDiv');",10000);
		window.scroll(0,0);
	return false;
	}else{
		if(eval(frm.State).value){
			frm.state_st.value=frm.State.value;
		}
		if(eval(frm.dia)){
			frm.day.value=frm.dia.value;
			frm.month.value=frm.mes.value;
			frm.year.value=frm.ano.value;
			
		}
		if(eval(frm.birth)){
			frm.Age.value=frm.birth.value;
		}
		if(eval(frm.Email)){
			frm.mail.value=frm.Email.value;
		}
		
		var url = homepath+'registration_process.php';		
		var rand = Math.random(9999);		
		var myAjax = new Ajax.Request(url, {method: 'post', 
	                                    parameters: $(frm).serialize(),
										onSuccess: function(transport){
											
											//alert(transport.responseText);
											if(frm_tab1=='register'){										   		
												hideFormDivTwo();
											}else{
												hideFormDiv(frm_tab1, step);
											}
										}
										 }
	                              );
	}
}

function hideFormDiv(elemId, nextId){
	var next_elem	=	'form-tab'+nextId;
	
	var elem	=	document.getElementById(elemId);
	elem.style.display	=	'none';
	
	if(nextId>0){
		var elem_next	=	document.getElementById(next_elem);	
		elem_next.style.display	=	'block';
	}
	
}
function hideFormDivTwo(){
	document.getElementById("divOne").style.display="none";	
	document.getElementById("divTwo").style.display="block";	
}
function validateEmail(addr,man,db){
		if (addr == '' && man) {
		   if (db) //alert('email address is mandatory');
		   return false;
		}
		var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
		for (i=0; i<invalidChars.length; i++) {
		   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
			  if (db) //alert('email address contains invalid characters');
			  return false;
		   }
		}
		for (i=0; i<addr.length; i++) {
		   if (addr.charCodeAt(i)>127) {
			  if (db) //alert("email address contains non ascii characters.");
			  return false;
		   }
		}

		var atPos = addr.indexOf('@',0);
		if (atPos == -1) {
		   if (db) //alert('email address must contain an @');
		   return false;
		}
		if (atPos == 0) {
		   if (db) //alert('email address must not start with @');
		   return false;
		}
		if (addr.indexOf('@', atPos + 1) > - 1) {
		   if (db) //alert('email address must contain only one @');
		   return false;
		}
		if (addr.indexOf('.', atPos) == -1) {
		   if (db) //alert('email address must contain a period in the domain name');
		   return false;
		}
		if (addr.indexOf('@.',0) != -1) {
		   if (db) //alert('period must not immediately follow @ in email address');
		   return false;
		}
		if (addr.indexOf('.@',0) != -1){
		   if (db)// alert('period must not immediately precede @ in email address');
		   return false;
		}
		if (addr.indexOf('..',0) != -1) {
		   if (db) //alert('two periods must not be adjacent in email address');
		   return false;
		}
		var suffi = addr.substring(addr.lastIndexOf('.')+1);
		var suffix = suffi.toLowerCase();
		if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
		   if (db) //alert('invalid primary domain in email address');
		   return false;
		}
		return true;
	}
	

function aceptar() {

	document.getElementById( 'crearEmpresa' ).disabled = !document.getElementById('aceptaterminos').checked; 

}

