
var i_step=0;
var step=new Array(4);
step[0]={name:'operatorbase',description:LBL_STEP0_DESCRIPTION};
step[1]={name:'operatorbasesettings',description:LBL_STEP1_DESCRIPTION};
step[2]={name:'superuser',description:LBL_STEP2_DESCRIPTION};
step[3]={name:'completed',description:LBL_STEP3_DESCRIPTION};


function submit(){
	var strData=getFormValues('frm_registration');
	var xmlDoc;
	xmlDoc=processRequestSync('operator_insup.php',strData,"POST");	
}


function refresh_step(){
	var prev,next,step_of;
	
	if(i_step>0){
		prev='<button id="prev_step" onclick="javascript:prev_step();"><< '+LBL_PREVIOUS_STEP+'</button>\n';
		if(i_step<2){ 
			next='<button id="next_step" onclick="javascript:next_step();">'+LBL_NEXT_STEP+' >></button>\n';
		}else{
			next='<button id="next_step" onclick="javascript:next_step();">'+LBL_STEP3_DESCRIPTION+'</button>\n';
		}
	}else{
		prev='<button disabled="true" id="prev_step"><< '+LBL_PREVIOUS_STEP+'</button>\n';
		next='<button id="next_step" onclick="javascript:next_step();">'+LBL_NEXT_STEP+' >></button>\n';
		
	}
	
	step_of=LBL_STEP_NO.replace(/^([^%]*)%s([^%]*)%s([^%]*)$/g,"$1"+(i_step+1)+"$2"+"3"+"$3");
	
	document.getElementById('steps').innerHTML=	prev+next+" <div id=\"step_description\"> "+step_of+" "+step[i_step].description+"</span>";
}

function next_step(){
	var i;
	//validate i_step
	//if validation ok then
	
	if(i_step==2) {
		registerIt();
		return 0;
	}
	
	if(validate(i_step)){
		//hide current step
		document.getElementById(step[i_step].name).style.display='none';
		document.getElementById(step[i_step].name).style.height='1px';
			
		//next_step
		i_step++;
		
		//show next
		document.getElementById(step[i_step].name).style.display='block';
		document.getElementById(step[i_step].name).style.height='auto';
	}
	//refresh info step
	refresh_step();
}

function prev_step(){
	var i;
	
	
	//hide current step
	document.getElementById(step[i_step].name).style.display='none';
	document.getElementById(step[i_step].name).style.height='1px';	
	//next_step
	i_step--;
	
	//show next
	document.getElementById(step[i_step].name).style.display='block';
	document.getElementById(step[i_step].name).style.height='auto';
	//refresh info step
	refresh_step();
	validation_reset();
}

function registerIt(){
	if(!check_rePwd()) {
		alert(MSG_ERROR_RETYPEPWD);
		return false;
	}
	if(!check_privacy()){
		alert(MSG_ERROR_PRIVACY);
		return false;
	}
	if(!check_conditions()){
		alert(MSG_ERROR_CONDITIONS);
		return false;
	}

	document.frm_registration.op.value='step_'+3;
	validation_reset();
	var strData=getFormValues('frm_registration');
	var xmlDoc,res;
	xmlDoc=processRequestSync('operator_insup.php',strData,"POST");
	res=xmlDoc.getElementsByTagName('response')[0].firstChild.nodeValue	
	if(res!="ok"){
		//if(res=="reg_error") alert("si è")
		//check_error(res,xmlDoc);
		alert("si è verificato un errore!");
		return false;
		}

	document.location.href='registration_completed.php';
	return true;
}

function validate(step){
	document.frm_registration.op.value='step_'+step;
	
	validation_reset();
	
	var strData=getFormValues('frm_registration');
	var xmlDoc,res;
	xmlDoc=processRequestSync('operator_insup.php',strData,"POST");
	
	res=xmlDoc.getElementsByTagName('response')[0].firstChild.nodeValue	
	if(res!="ok"){check_error(res,xmlDoc);return false;}
	return true;
}

function get_settings(tz){
	var reqUrl='op=defaultsettings&country='+tz;
	var xmlDoc=processRequestAsync("/xml/utils.php",refresh_settings,"GET",true,reqUrl);
}


function refresh_settings(xmlDoc){
	res=xmlDoc.getElementsByTagName('response')[0].firstChild.nodeValue	
	if(res!="ok"){check_error(res,xmlDoc);return false;}
	var frm=document.frm_registration;	
	if(xmlDoc.getElementsByTagName("settings")[0]){
		frm.operatorbasesettingslanguagecode.value=xmlDoc.getElementsByTagName('languagecode')[0].firstChild.nodeValue;
		if(xmlDoc.getElementsByTagName('currency')[0].firstChild)
			frm.operatorbasesettingscurrency.value=xmlDoc.getElementsByTagName('currency')[0].firstChild.nodeValue;
		frm.operatorbasesettingsformatdate.value=xmlDoc.getElementsByTagName('formatdate')[0].firstChild.nodeValue;
		frm.operatorbasesettingsampm.value=xmlDoc.getElementsByTagName('ampm')[0].firstChild.nodeValue;
		if(xmlDoc.getElementsByTagName('decimalseparator')[0].firstChild)
			frm.operatorbasesettingsdecimalseparator.value=xmlDoc.getElementsByTagName('decimalseparator')[0].firstChild.nodeValue;
		if(xmlDoc.getElementsByTagName('thousandseparator')[0].firstChild)
			frm.operatorbasesettingsthousandseparator.value=xmlDoc.getElementsByTagName('thousandseparator')[0].firstChild.nodeValue;
		frm.operatorbasesettingsdecimaldigits.value=xmlDoc.getElementsByTagName('decimaldigits')[0].firstChild.nodeValue;
		frm.operatorbasesettingstimezone.value=xmlDoc.getElementsByTagName('timezone')[0].firstChild.nodeValue;
		if(xmlDoc.getElementsByTagName('distanceunitmeasurement')[0].firstChild)
			frm.operatorbasesettingsdistanceunitmeasurement.value=xmlDoc.getElementsByTagName('distanceunitmeasurement')[0].firstChild.nodeValue;
	}else{
		frm.operatorbasesettingslanguagecode.value="en";
		frm.operatorbasesettingscurrency.value=
		frm.operatorbasesettingsformatdate.value=frm.operatorbasesettingsampm.value=frm.operatorbasesettingsdecimalseparator.value=
		frm.operatorbasesettingsthousandseparator.value=frm.operatorbasesettingsdecimaldigits.value=
		frm.operatorbasesettingstimezone.value=frm.operatorbasesettingsdistanceunitmeasurement.value="";
	}

	show_time(frm.operatorbasesettingstimezone);
	return true;
}


function startup(){
	//checkFirefox();	
	var country=document.frm_registration.operatorbasecountry.value;
	var countystate=document.frm_registration.operatorbasecountystate.value;
	refresh_step();
	country_change(document.frm_registration.operatorbasecountry);
}

function country_change(select){
	get_settings(select.value);
}

function show_time_span(xmlDoc){
	var res=xmlDoc.getElementsByTagName("response")[0].firstChild.nodeValue;
	if(res!="ok"){check_error(res,xmlDoc);return false;}
	var localtime=xmlDoc.getElementsByTagName("localtime")[0].firstChild.nodeValue;
	if(localtime){
		var span=document.getElementById("localtimepreview");
		span.innerHTML=MSG_LOCALTIME_PREVIEW+localtime;
	}

}

function check_rePwd(){
	return (document.getElementsByName("checkpwd")[0].value==document.getElementsByName("superuserpassword")[0].value);
}

function check_privacy(){
	return document.getElementsByName("checkprivacy")[0].checked;
}

function check_conditions(){
	return document.getElementsByName("checkconditions")[0].checked;
}


setEvent(window,"load",startup);
