// JavaScript Document
	var telnr;
	
	/*
		Put the focus in the input text for the number phone
	*/
	function setFocus(){
		var userName = $('idUserName');
		userName.focus();
	}
	
	/*
		Check the telephone number and verify that the number haves the correct format
		The correct format of the number can be one of this:
			+27 XX XXX XXXX
			-27 XX XXX XXXX
 			 27 XX XXX XXXX
	*/
	function correctNumber(form){
		
		retval=true;
		cijfers=true;
		telnr = form.userName.value;
		
		/**
			Corrects numbers:
				start with +27 and 9 digits (length = 12) 		+27 -->27
				start with 27 and 9 digits (length = 11)		27  -->27
				start with 0 and 9 digits (length = 10)  		0   -->27
			All the formats will be changed to 27 XXX XXX XXX	
				
		*/
		
		if(telnr.length == 12){
			//first format +27 XXX XXX XXX
			if(telnr.slice(0,3) == '+27')
			{
				telnr = telnr.replace('+27','27');
				//new length 11
			}
			else
			{
				//the format isn't correct	
				alert("The phone number should start with '27', '+27' or '0' and 9 digits of the phone number.");
				return false;
			}
		}
		else if(telnr.length == 11){
			//second format of number phone
			if(telnr.slice(0,2) != '27'){
				//the format isn't correct	
				alert("The phone number should start with '27', '+27' or '0' and 9 digits of the phone number.");
				return false;	
			}
		}
		else if(telnr.length == 10){
			//third type of format, the number should start with 0
			if(telnr.slice(0,1) != '0'){
				//the format isn't correct	
				alert("The phone number should start with '27', '+27' or '0' and 9 digits of the phone number.");
				return false;	
			}
			else{
				//replace the 0 for a 27
				telnr = telnr.replace('0','27');
				//new length 11
			}
		}
		if(telnr.length < 11){
			alert('\nThe phone number is too short! It should be 11 characters');
			return false;
		}

		
		/*
			In this moment the telnr has the length of 11, and check others conditions of the number like
			characters
		*/

		for (i=1;i<telnr.length;i++)
		{
			if (isNaN(parseInt(telnr.charAt(i))))
			{
				cijfers=false;
			}
		}
	   
	   
		if (telnr.slice(0,2)!='27')
		{
			retval=false;
			melding="\nThe phone number should start with '27'.";
		}
	
		if (cijfers == false)
		{
			melding="\nThe phone number should only contain numbers.";
			retval=false;
		}
		else if ((telnr.length > 1 && telnr.length < 11))
		{
			retval=false;
			melding='\nThe phone number is too short! It should be 11 characters';
		}	
		else if (telnr.length == 0)
		{
			retval=false;
			melding='\nYou have not inserted any digit yet. Please try again!';
		}
			
		if (retval == false)
		{
			alert('Please check the format of the number.'+melding);
			return false;
		}
		else
		{
			return true;
		}	
	}
	
	
	/*
		select the operator of the select number
	*/
	function preselectOperator(){ 
		var optionOperator;
		var userName = telnr;
		
		//if the number have the prefix extension + or - We will delete this for the tests
		if(userName.slice(0,1) == '+' || userName.slice(0,1) == '-')
			userName = userName.slice(1,12);
				
		if(userName.length >= 4){
			//check with this prefix: 27791,2774,2784
			if(userName.slice(0,4) == '2774' || userName.slice(0,4) == '2784'){
				optionOperator = $('idCellC');
			}	
			else if(userName.slice(0,4) == '2783' || userName.slice(0,4) == '2773' || userName.slice(0,4) == '2778'){
				optionOperator = $('idMTN');
			}
			else if(userName.slice(0,4) == '2782' || userName.slice(0,4) == '2772' || userName.slice(0,4) == '2776' || userName.slice(0,4) == '2779' || userName.slice(0,4) == '2771'){
				optionOperator = $('idVodacom');
			}
			else{
				//in default we select vodaCom
				optionOperator = $('idVodacom');
			}
			//then check the option
			optionOperator.selected = true;		
		}
	}
	
	/*	
	 	Send the form's data to a module of process of information
	*/
	function sendForm(sid){
		/*
			variables filas
			   serviceID
			   alias
		*/
		var form = $('idSplashForm');
		if(correctNumber(form)){
			preselectOperator();
			
			var selectOperator = $('idSelectOperator');
			//if the prefix of the number is 082 We chage it for +27
			var operator = $('idOperator');
			operator.value = selectOperator.value;										//operator (vodacom, mtn, cellc)
			var date = new Date();
			var blinkoza_msisdn = $('idUserName');
                        var idBlinkoza_msisdn = $('idBlinkoza_msisdn');
                        idBlinkoza_msisdn.value = blinkoza_msisdn.value;
    

                        var orderId = blinkoza_msisdn.value + date.getTime();
			blinkoza_msisdn.value = '+' + telnr;																//userName 
			
                        form.action = "splashEnterPin46.html?"+sid+":"+"&orderId="+orderId;
						
			form.submit();
			return false;
		}
	}
	
			
	
