var cell_number;
var pin;

// verify if user in DB for operator
function step1(){
	
	cell_number = document.getElementById('cell_1').value + document.getElementById('cell_2').value + document.getElementById('cell_3').value;
	if (cell_number.length != 10){
		alert('Your cell phone number must be 10 digits!!');
		document.getElementById('cell_1').focus();
	}
	else{		
		checkUser();
	}
}

function displayStep1AndAHalf(){

	document.getElementById('div_step_1').style.display = 'none';
	document.getElementById('div_step_1_2').style.display = 'block';
}

function step2(){
	
	sendPinToMobile();
}

function displayStep2(user_type){
	
	document.getElementById('div_step_1').style.display = 'none';
	document.getElementById('div_step_1_2').style.display = 'none';
	document.getElementById('div_step_2').style.display = 'block';
	document.getElementById('div_step_1_on').style.display = 'none';
	document.getElementById('div_step_1_off').style.display = 'block';
	document.getElementById('div_step_2_on').style.display = 'block';
	document.getElementById('div_step_2_off').style.display = 'none';
	if (user_type == '1'){
		document.getElementById('div_pin_1').style.display = 'block';
	}
	else{
		document.getElementById('div_pin_2').style.display = 'block';
		document.getElementById('btn_continue').disabled = false;
	}
}

// validate pin and send download
function step3(){
		
	if (document.getElementById('div_pin_1').style.display == 'block'){ 
		if (document.getElementById('chk_conditions').checked){
			//document.getElementById('btn_continue').disabled = true;
			pin = document.getElementById('pin').value;
			validateMobilePin();
		}
		else{		
			alert('You must accept the terms and conditions to continue!');
			document.getElementById('chk_conditions').focus();
		}
	}
	else{
		pin = document.getElementById('pin').value;
		validateMobilePin();
	}
}

function displayStep3(){
	
	document.getElementById('div_step_2').style.display = 'none';
	document.getElementById('div_step_3').style.display = 'block';
	document.getElementById('div_step_2_on').style.display = 'none';
	document.getElementById('div_step_2_off').style.display = 'block';
	document.getElementById('div_step_3_on').style.display = 'block';
	document.getElementById('div_step_3_off').style.display = 'none';
}

function sendPinToMobile(){

	var xmlhttpreq;
	if(window.XMLHttpRequest) {
		xmlhttpreq = new XMLHttpRequest();
	} else {
		try {
			xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	
	xmlhttpreq.onreadystatechange = function() {
		if(xmlhttpreq.readyState == 4 && xmlhttpreq.status == 200) {
			displayStep2(xmlhttpreq.responseText);  
		}
	}
	xmlhttpreq.open("GET", web_root + "/ajax/step_1.php?cell_number=" + cell_number, true);
	xmlhttpreq.send(null);		
}

function validateMobilePin(){

	var xmlhttpreq;
	if(window.XMLHttpRequest) {
		xmlhttpreq = new XMLHttpRequest();
	} else {
		try {
			xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	
	xmlhttpreq.onreadystatechange = function() {
		if(xmlhttpreq.readyState == 4 && xmlhttpreq.status == 200) {
			var ret = xmlhttpreq.responseText;
			if (ret == '1'){
				displayStep3();
			}
			else{				
				alert('You have entered an incorrect PIN, please try again!');	
				document.getElementById('btn_continue').disabled = false;
			}
		}
	}
	xmlhttpreq.open("GET", web_root + "/ajax/step_2.php?cell_number=" + cell_number + "&pin=" + pin + "&prod=" + product_id, true);
	xmlhttpreq.send(null);		
}

function checkUser(){

	var xmlhttpreq;
	if(window.XMLHttpRequest) {
		xmlhttpreq = new XMLHttpRequest();
	} else {
		try {
			xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	
	xmlhttpreq.onreadystatechange = function() {
		if(xmlhttpreq.readyState == 4 && xmlhttpreq.status == 200) {
			var ret = xmlhttpreq.responseText;
			if (ret == '1'){
				sendPinToMobile();
			}
			else{
				displayStep1AndAHalf();
			}
		}
	}
	xmlhttpreq.open("GET", web_root + "/ajax/check_user.php?cell_number=" + cell_number, true);
	xmlhttpreq.send(null);	
}

function setMobileOperator(){

	var mobile_operator = document.getElementById('mobile_operator').value;
		
	var xmlhttpreq;
	if(window.XMLHttpRequest) {
		xmlhttpreq = new XMLHttpRequest();
	} else {
		try {
			xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	
	xmlhttpreq.onreadystatechange = function() {
		if(xmlhttpreq.readyState == 4 && xmlhttpreq.status == 200) {
			if (xmlhttpreq.responseText == '1'){
				sendPinToMobile();
			}
			else{
				alert('We are experiencing technical problems. Please try again later.!');
			}
		}
	}
	xmlhttpreq.open("GET", web_root + "/ajax/set_mobile_operator.php?cell_number=" + cell_number + "&operator_id=" + mobile_operator, true);
	xmlhttpreq.send(null);		
}

function resetPin(){

	var xmlhttpreq;
	if(window.XMLHttpRequest) {
		xmlhttpreq = new XMLHttpRequest();
	} else {
		try {
			xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	
	xmlhttpreq.onreadystatechange = function() {
		if(xmlhttpreq.readyState == 4 && xmlhttpreq.status == 200) {
			if (xmlhttpreq.responseText == '1'){
				alert('A new PIN has been sent to your cell phone by text message!');
			}
			else{
				alert('We are experiencing technical problems. Please try again later.!');
			}
		}
	}
	xmlhttpreq.open("GET", web_root + "/ajax/nip_oublie.php?cell_number=" + cell_number, true);
	xmlhttpreq.send(null);		
}

function checkConditions(){
					
	if (document.getElementById('btn_continue').disabled){
		document.getElementById('btn_continue').disabled = false;	
	}
	else{
		document.getElementById('btn_continue').disabled = true;
	}
}