<script language="javascript"> 
var submitted = false;
function check_input(field_name, pos,field_size) {
  var form=document.checkout_attendees;
  if (pos>=0){
	  var control=form.elements[field_name][pos];
  } else {
	  var control=form.elements[field_name];
  }
  var field_value = control.value;
  if (field_value == '' || field_value.length < field_size) {
    return true;
  }
} 
function check_select(field_name, pos,field_default) {
  var form=document.checkout_attendees;
  if (pos>=0){
	  var control=form.elements[field_name][pos];
  } else {
	  var control=form.elements[field_name];
  }
  var field_value = control.value;

   if (field_value == field_default) {
     return true;
   }
}
function dofill(){
	var frm=document.checkout_attendees;
	frm_customers=frm.elements["attendee[]"];
	for (icnt=1;icnt<frm.elements["city[]"].length;icnt++)
	{
	 if(frm_customers && frm_customers[icnt].checked) return;	
	 frm.elements["city[]"][icnt].value=frm.elements["city[]"][0].value;
	 frm.elements["state1[]"][icnt].style.display='';
	 frm.elements["state[]"][icnt].style.display="none";
	 if(frm.elements["state[]"][0].style.display!='none') 	
	 	state=frm.elements["state[]"][0].value;
	 else if(frm.elements["state1[]"][0].style.display!='none')
	 	state=frm.elements["state1[]"][0].value;
		 			 	
	 frm.elements["state1[]"][icnt].value=state;		
	 frm.elements["country[]"][icnt].value=frm.elements["country[]"][0].value;
	 frm.elements["postcode[]"][icnt].value=frm.elements["postcode[]"][0].value;
	 frm.elements["street_address[]"][icnt].value=frm.elements["street_address[]"][0].value;
	 frm.elements["email_address[]"][icnt].value=frm.elements["email_address[]"][0].value;
	}
}
function clear_error(pos){
	error_control=document.getElementById("error_box_"+pos);
	error_control.innerHTML="";
	error_control.style.display="none";
}
function show_errors(count){ 
	for (icnt=0;icnt<count;icnt++){
		error_control=document.getElementById("error_box_"+icnt);
		error_control.style.display="";
	}
}
function check_customer(pos){  
	var error_att="";
		if (check_input("first_name[]",pos,"<?php echo ENTRY_FIRST_NAME_MIN_LENGTH;?>")){
			error_att+="<?php echo FIRST_NAME_ERROR;?><br>";
		}
		if (check_input("last_name[]",pos,"<?php echo ENTRY_LAST_NAME_MIN_LENGTH;?>")){
			error_att+="<?php echo LAST_NAME_ERROR;?><br>";
		}
		if (check_input("email_address[]",pos,"<?php echo ENTRY_EMAIL_ADDRESS_MIN_LENGTH;?>")){
			error_att+="<?php echo EMAIL_ADDRESS_ERROR;?><br>";
		}
		if (check_input("street_address[]",pos,"<?php echo ENTRY_STREET_ADDRESS_ERROR;?>")){
			error_att+="<?php echo STREET_ADDRESS_ERROR;?><br>";
		}
		if (check_input("city[]",pos,"<?php echo ENTRY_CITY_MIN_LENGTH;?>")){
			error_att+="<?php echo CITY_ERROR;?><br>";
		}
		if (check_input("postcode[]",pos,"<?php echo ENTRY_POSTCODE_MIN_LENGTH;?>")){
			error_att+="<?php echo POSTCODE_ERROR;?><br>";
		}
		if(document.checkout_attendees.elements["state[]"].size!=0){
			if (document.checkout_attendees.elements["state[]"][pos].style.display!='none' && check_input("state[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
				error_att+="<?php echo STATE_ERROR;?><br>";
			}else if(document.checkout_attendees.elements["state1[]"][pos].style.display!='none' && check_input("state1[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
				error_att+="<?php echo STATE_ERROR;?><br>";
			}
		}else if(document.checkout_attendees.elements["state[]"].size==0) {
			if (document.checkout_attendees.elements["state[]"].style.display!='none' && check_input("state[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
				error_att+="<?php echo STATE_ERROR;?><br>";
			}else if(document.checkout_attendees.elements["state1[]"].style.display!='none' && check_input("state1[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
				error_att+="<?php echo STATE_ERROR;?><br>";
			}
		}	
		if (check_select("country[]",pos,"")){
			error_att+="<?php echo COUNTRY_ERROR;?><br>";
		}
		if (pos>0){
			var controls=document.checkout_attendees.elements["email_address[]"];
			var attendees=document.checkout_attendees.elements["attendee[]"];
			for (jcnt=pos-1;jcnt>0;jcnt--){ 
				if (controls[pos].value==controls[jcnt].value){
					if(attendees && attendees[pos].checked)
						error_att="<?php echo ERR_ATTENDEES_MUST_UNIQUE;?>";
					else if(attendees && !attendees[pos].checked)
						error_att="<?php echo EMAIL_UNIQUE_ERROR;?>";	
				}
				if(controls[jcnt].value.indexOf('@')<=0 || controls[jcnt].value.indexOf('.')<=0){
					error_att="<?php echo ADDRESS_CHECK_ERROR;?>";
				}
			}
		}
	return error_att;
}
function check_form(form_name) {
	var error=false;
	var frm=document.checkout_attendees;
	if(frm.elements["country[]"].size!=0){
		for(i=0;i<frm.elements["country[]"].length;i++){
			if(frm.elements["state[]"][i].style.display!='none')
				frm.elements["state[]"][i].disabled=false;
				frm.elements["country[]"][i].disabled=false; 
		}
	}else if(frm.elements["country[]"].size==0){
			if(frm.elements["state[]"].style.display!='none')
				frm.elements["state[]"].disabled=false;
			frm.elements["country[]"].disabled=false;  	
	}
	var controls=frm.elements["first_name[]"];
	set_state();
	if (controls.length){
		for (icnt=1;icnt<controls.length;icnt++){
			error_att=check_customer(icnt);
			if (error_att!="") { 
				error=true;
				error_control=document.getElementById("error_box_"+icnt);
				error_control.innerHTML=error_att;
				error_control.style.display="";
			}
		}
	} else {
		error_att=check_customer(-1);
		if (error_att!="") {
			error=true; 
			error_control=document.getElementById("error_box_0");
			error_control.innerHTML=error_att;
			error_control.style.display="";
		}
	}
	if (error == true) {
		return false;
	} else {
		submitted = true;
		return true;
	}
}

 function hide_customerlistbox(i){ 
	 var frm=document.checkout_attendees;
	 var val=new Array('','','','','','','','');
	 var frm_customer=frm.elements["attendee[]"];
	 var check=false;
	 	 if(frm_customer.length>0){
				if(frm_customer) check=frm_customer[i].checked;
				if(check){
					frm.customer_text[i].style.display="";
					fill_details(i);
				}else if(!check) { 
					frm.customer_text[i].style.display="none";
					fetch_details(val,i);
				}			
				frm.elements["first_name[]"][i].readOnly=frm.elements["last_name[]"][i].readOnly=frm.elements["email_address[]"][i].readOnly=frm.elements["street_address[]"][i].readOnly=frm.elements["city[]"][i].readOnly=frm.elements["postcode[]"][i].readOnly=check;
				frm.elements["country[]"][i].disabled=check;
				frm.elements["state[]"][i].style.display='none';
				frm.elements["state1[]"][i].style.display='';
				if(frm.elements["state1[]"][i])
					frm.elements["state1[]"][i].readOnly=check;
				 
		}else {
			if(frm.elements["attendee[]"]) check=frm.elements["attendee[]"].checked;	
			if(check){
				frm.customer_text.style.display="";
				fill_details(0);
			}else if(!check) {	
				frm.customer_text.style.display="none";
				fetch_details(val,0);
			}
			frm.elements["first_name[]"].readOnly=frm.elements["last_name[]"].readOnly=frm.elements["email_address[]"].readOnly=frm.elements["street_address[]"].readOnly=frm.elements["city[]"].readOnly=frm.elements["postcode[]"].readOnly=check;
			frm.elements["country[]"].disabled=check;
			if(frm.elements["state[]"].style.display!='none')
				frm.elements["state[]"].disabled=check;
			else if(frm.elements["state1[]"])
				frm.elements["state1[]"].readOnly=check;
		}
	}
  function fill_details(cnt){
  	id=0;
  	var frm=document.checkout_attendees.elements["customer_text"];
  	if(frm.size!=0)
  		id=frm[cnt].options[frm[cnt].selectedIndex].value; 
	else 
		id=frm.options[frm.selectedIndex].value;
		command="<?php echo tep_href_link(FILENAME_CHECKOUT_ATTENDEES,'command=fetch_customers_details','NONSSL',true,false);?>&cust_id="+id+"&cnt="+cnt; 
   	do_get_command(command);
  } 
   
  function do_result(result){ 
  	if(result.substr(0,5)=='Login'){
	 	do_expand(1);
		find_id=result.split('@');
		id=find_id[1];
		cnt=0;
		no_att=document.checkout_attendees.no_attendees.value;
		//location.href='<?php echo tep_href_link(FILENAME_CHECKOUT_ATTENDEES,tep_get_all_get_params().'cust_id='); ?>'+id+"&cnt="+cnt+"&no_att="+no_att;

		command="<?php echo tep_href_link(FILENAME_CHECKOUT_ATTENDEES,'command=fetch_customers_details&action=return','NONSSL',true,false);?>&cust_id="+id+"&cnt="+cnt+"&no_att="+no_att; 
		do_get_command(command);
	 }
	else if(result=='Failed'){
	 	document.getElementById('login_failed').style.display='';
	 }else{
  	 var textArr=new Array();
	 var valueArr=new Array();
	 cnt=0;
	 var frm=document.checkout_attendees;
	 var frm_attendees=frm.elements["attendee[]"];
	 if(result!='' && result.substr(result.indexOf('show_state'),10)=='show_state'){
	 	result=result.substr(result.indexOf('show_state')+10,result.length);
		splt=result.split("^");
		valueArr=splt[1].split(",");		
		textArr=splt[1].split(",");
		cnt=parseInt(splt[3]);
		if(frm_attendees){
			if(frm_attendees && frm_attendees.checked) return;
			else if(frm_attendees[cnt]  && frm_attendees[cnt].checked) return;
		} 
		//var div_state=false;
	//	div_state=document.getElementById("div_state");
		/*if(frm.elements["state[]"][cnt] && frm.elements["state[]"][cnt].type=='select-one') var state=frm.elements["state[]"][cnt];
		else if(frm[7].type=='select-one') var state=frm[7];
		if(frm.elements["state1[]"][cnt]) var state1=frm.elements["state1[]"][cnt];
		else if(frm.elements["state1[]"]) var state1=frm.elements["state1[]"];*/
		if(frm.elements["first_name[]"].length>1){
			state=frm.elements["state[]"][cnt];
			state1=frm.elements["state1[]"][cnt];
		} else{
			state=frm.elements["state[]"];
			state1=frm.elements["state1[]"];
		}	
		if(valueArr.length==1) {	
			state1.style.display="";
		//	if(div_state) div_state.style.display="none"; 
			 state.style.display="none";
		}else {
				state1.style.display="none";
			//	state1.value="";
			 state.style.display="";
			//if(div_state) div_state.style.display="";
			optionElement=state;
			// clear the previous details
			while(optionElement.options.length>0){
				optionElement.remove(optionElement.options.length - 1);
			}
			//create the new details
			for (icnt=0;icnt<textArr.length;icnt++){
			  var option = document.createElement('option');
			  option.text = textArr[icnt];
			  option.value = valueArr[icnt];
			  try {
				optionElement.add(option, null); // standards compliant; doesn't work in IE
			  }
			  catch(ex) {
				optionElement.add(option); // IE only
			  }
			}
		}
	}else if(result!='' && result.substr(result.indexOf('customer_list'),13)=='customer_list'){ 
	 result=result.substr(result.indexOf('customer_list')+13,result.length);
	 res=result.split('##');
	 if(document.getElementById('return_login')) document.getElementById('return_login').style.display='none';
	 var val=new Array('','','','','','','','');
	 if(res[0]!='') val=res[0].split("^");
	 if(res[1]!='') fetch_details(val,res[1]);
		 if(res[2]){ 
		 	checkbox_list = res[2].split('~');
			chk_len=checkbox_list.length-1;
			for(k=1;k<chk_len;k++){
				document.getElementById('select_cus['+ k +']').innerHTML=checkbox_list[k];
				document.getElementById('chk_ids['+ k +']').style.display='';
				}
	 	}	

	}
   }
  }  
	function set_state(){
		var frm=document.checkout_attendees;
			if(frm.elements["country[]"].size!=0){
				for(i=0;i<frm.elements["country[]"].length;i++){
					if(frm.elements["state[]"][i].style.display!='none'){
						frm.elements["customer_state[]"][i].value=frm.elements["state[]"][i].value;
					}
					else	
						frm.elements["customer_state[]"][i].value=frm.elements["state1[]"][i].value;
				}
			}else if(frm.elements["country[]"].size==0){
					if(frm.elements["state[]"].style.display!='none'){
						frm.elements["customer_state[]"].value=frm.elements["state[]"].value;
					}
					else 
						frm.elements["customer_state[]"].value=frm.elements["state1[]"].value;
			}
					  
	}
  
  function fetch_details(val,i){ 
  	var frm=document.checkout_attendees;
  	if(!frm.elements["first_name[]"].length){
  		frm.elements["first_name[]"].value=val[0];  
		frm.elements["last_name[]"].value=val[1];
		frm.elements["email_address[]"].value=val[2];
		frm.elements["street_address[]"].value=val[3];
		frm.elements["city[]"].value=val[4];  
		frm.elements["postcode[]"].value=val[5]; 
		if(frm.elements["state[]"].style.display!='none') 
			frm.elements["state[]"].value=val[6];
		else if(frm.elements["state1[]"])
			frm.elements["state1[]"].value=val[6];
		frm.elements["country[]"].value=val[7];
	}else if(frm.elements["first_name[]"][i]){
		frm.elements["first_name[]"][i].value=val[0];  
		frm.elements["last_name[]"][i].value=val[1];
		frm.elements["email_address[]"][i].value=val[2]; 
		frm.elements["street_address[]"][i].value=val[3];
		frm.elements["city[]"][i].value=val[4];  
		frm.elements["postcode[]"][i].value=val[5]; 
		if(frm.elements["state[]"][i].style.display!='none')
			frm.elements["state[]"][i].value=val[6];
		else if(frm.elements["state1[]"][i]){ 
			frm.elements["state1[]"][i].value=val[6];
		}
		frm.elements["country[]"][i].value=val[7];	
	}
  }
  
  function show_state(cnt){ 
  		var this_=document.checkout_attendees.elements["country[]"];
  		if(this_[cnt].size==0)
  			id=this_[cnt].options[this_[cnt].selectedIndex].value;
  		else
  			id=this_.options[this_.selectedIndex].value;
		if(id){
		  command="<?php echo tep_href_link(FILENAME_CHECKOUT_ATTENDEES,'command=show_state','NONSSL',true,false);?>&country_id="+id+"&cnt="+cnt;
		  do_get_command(command);	
		}
  }   
</script>
