
/* 	JavaScript for VMI apps tool.
	Author: Shegun Konibire
	Studio: Dare Digital
	Date: 01.10.2007	*/	
/* --------------------	*/

// Global variables
var textModelResetMsg = "Your phone model...";

/* Popup messages */
var ApplicationOnPhoneMsg = "This application may already be on your phone";
var mustPickModelMsg = "Please select the model of your phone";
var mustPickMakeMsg = "Please select the make of your phone";

var musicStationText = "Click on the link to download the MusicStation application http://live.vodafone.co.uk/musicstation";

var telNumberErrorNo = 0;
var telNumberErrors = new Array();
telNumberErrors[0] = "Valid UK mobile number";
telNumberErrors[1] = "Please enter your mobile number";
telNumberErrors[2] = "Please enter your UK mobile number starting with 07";
telNumberErrors[3] = "Please make sure that your mobile number contains digits only";
telNumberErrors[4] = "Please enter your UK mobile number starting with 07";
telNumberErrors[5] = "Please enter your UK mobile number starting with 07";
telNumberErrors[6] = "Please enter your UK mobile number again. The number entered was too short";
telNumberErrors[7] = "Please enter your UK mobile number again. The number entered was too long";

/* Mobile handset Makes */
var MakeData = new Array('LG','Nokia','Samsung','Sony Ericsson');

/* Mobile handset Models */
var LGList = new Array('LG KF750', 'LG Viewty (KU990)');		
var NokiaList = new Array('Nokia 5310', 'Nokia 6110', 'Nokia 6124c', 'Nokia 6210', 'Nokia 6500', 'Nokia 6500s', 'Nokia 7500', 'Nokia E65', 'Nokia N70', 'Nokia N73', 'Nokia N78', 'Nokia N81', 'Nokia N81 8G', 'Nokia N95','Nokia N95 8G', 'Nokia N96');		
var SamsungList = new Array('Samsung Tocco (F480)', 'Samsung F490i', 'Samsung F700', 'Samsung U900');		
var SEricssonList = new Array('Sony Ericsson C702', 'Sony Ericsson C902', 'Sony Ericsson C905', 'Sony Ericsson K770i', 'Sony Ericsson K800i', 'Sony Ericsson K810i', 'Sony Ericsson K850i','Sony Ericsson W595 ', 'Sony Ericsson V630i', 'Sony Ericsson V640i', 'Sony Ericsson W580i', 'Sony Ericsson W850i', 'Sony Ericsson W880i', 'Sony Ericsson W890', 'Sony Ericsson W890i', 'Sony Ericsson W910i', 'Sony Ericsson W980', 'Sony Ericsson W980i');
	
	
/*	checks that a make has been selected */
function noMakeSelected(){
	var returnVal = false;
	var phoneMake = document.getElementById("phoneMake").options[document.getElementById("phoneMake").selectedIndex].value;
	if (phoneMake == "null"){
		returnVal = true;			
	}
	return returnVal;
}
/* ------------------------------ */

/*	checks that a model has been selected */
function noModelSelected(){
	var returnVal = false;
	var phoneModel = document.getElementById("phoneModel").options[document.getElementById("phoneModel").selectedIndex].value;
	if (phoneModel == "null"){
		returnVal = true;			
	}
	return returnVal;
}
/* ------------------------------ */

/*==============================================================================
	
This routine checks the value of the string variable specified by the parameter
for a valid UK telphone number. It returns false for an invalid number and the
reformatted telephone number false a valid number.

If false is returned, the global variable telNumberError contains an error
number, which may be used to index into the array of error descriptions 
contained in the global array telNumberErrors.

The definition of a valid telephone number has been taken from:
http://www.ofcom.org.uk/telecoms/ioi/numbers/numplan310507.pdf

All inappropriate telephone numbers are disallowed (e.g. premium lines, sex 
lines, radio-paging services etc.)

Author:    John Gardner
Date:      16th November 2003

Updated:   4th August 2006 Updated to include 03 numbers being added by Ofcom in early 2007.
Version:   V1.2  9th January 2007
Isle of Man mobile numbers catered for 

Example calling sequence:		
if (!checkUKTelephone (myTelNo)) {
 alert (telNumberErrors[telNumberErrorNo]);
}

------------------------------------------------------------------------------*/

function checkUKTelephone(telephoneNumber){	
	// Convert into a string and check that we were provided with something
	var telnum = telephoneNumber + " ";
	if (telnum.length == 1) {
		telNumberErrorNo = 1;
		return false
	}
	telnum.length = telnum.length - 1;
	
	// Don't allow country codes to be included (assumes a leading "+")
	var exp = /^(\+)[\s]*(.*)$/;
	if (exp.test(telnum) == true){
		telNumberErrorNo = 2;
		return false;
	}
	
	// Remove spaces from the telephone number to help validation
	while (telnum.indexOf(" ")!= -1) {
		telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
	}
	
	// Remove hyphens from the telephone number to help validation
	while (telnum.indexOf("-")!= -1) {
		telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
	}
	
	// Check for length less than 10 digits
	if (telnum.length < 10) {
		telNumberErrorNo = 6;
		return false
	}
	
	// Check for length more than 11 digits
	if (telnum.length > 11) {
		telNumberErrorNo = 7;
		return false
	}	  
	
	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/
	if (exp.test(telnum) != true) {
	 telNumberErrorNo = 3;
	 return false;
	}
	
	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/
	if (exp.test(telnum) != true) {
	 telNumberErrorNo = 4;
	 return false;
	}
	
	// Finally check that the telephone number is appropriate.
	exp = /^(07)[0-9]+$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 5;
		return false;
	}
	
	// Telephone number seems to be valid - return the stripped telehone number  
	return telnum;
}
		
		
		
/* 	Builds up model list, sets up contents of Model dropdown based on selected Make dropdown choice. */
function setUpModelList(selectObj){
	// Remove all existing options from the select box.
	while(document.getElementById("phoneModel").hasChildNodes()){				
		document.getElementById("phoneModel").removeChild(document.getElementById("phoneModel").lastChild);
	}	
	
	// Insert default option
	var optionTag = document.createElement('option');
	optionTag.setAttribute("value","null")
	var optionText = document.createTextNode(textModelResetMsg);
	optionTag.appendChild(optionText)
	document.getElementById("phoneModel").appendChild(optionTag);
	
	// Insert display Model list, if selected option is not 'null'
	selectValue = selectObj.options[selectObj.selectedIndex].value;
	if (selectValue != "null"){
		var ModelArray = new Array();
		switch(selectValue){
			case "Nokia":
				ModelArray = NokiaList;
				break    
			case "Samsung":
				ModelArray = SamsungList;
				break;
			case "Sony Ericsson":
				ModelArray = SEricssonList;
				break;
			case "LG":
				ModelArray = LGList;
				break;							
			}
		for (var i=0; i<ModelArray.length; i++){					
			var optionTag = document.createElement('option');
			optionTag.setAttribute("value",ModelArray[i]);					
			var optionText = document.createTextNode(ModelArray[i]);
			optionTag.appendChild(optionText);					
			document.getElementById("phoneModel").appendChild(optionTag);					
		}
	// sets focus on Model select box
	document.getElementById("phoneModel").focus(); 				
	}
}
/* ------------------------------ */

/* sends off the form */
function sendMusicStationForm(){
	// Check Make of handset is selected
	if (noMakeSelected()){
		alert(mustPickMakeMsg);
		document.getElementById("phoneMake").focus();
		return false;
	}
	// Check Model of handset is selected
	if (noModelSelected()){
		alert(mustPickModelMsg);
		document.getElementById("phoneModel").focus();
		return false;
	}
	// validate phone number				
	var myTelNo = document.getElementById("ctn").value;
	if (!checkUKTelephone(myTelNo)){
		// If invalid number, report back error
		alert(telNumberErrors[telNumberErrorNo]);
		document.getElementById("ctn").focus();
		document.getElementById("ctn").select();
		return false;
	}	
	// send the form, yeah!
	document.getElementById("msgtxt1").setAttribute("value",musicStationText);
	document.getElementById("sendsms").submit();
}
/* ------------------------------ */

/* clears the number field */
function clearfield(inputObj){
	if(inputObj.value == "Your phone number..." || inputObj.value == "Search"){
		inputObj.value = "";
	}
}

/* move focus to phone number is filled */
function setUpMake(selectObj){
	if (selectObj.options[selectObj.selectedIndex].value != "null"){
		document.getElementById("ctn").focus();	
	}	
}

/* Prepares the form on the page */
window.onload = function(){
	// check that browser understabnds this type of JavaScript
	if (!document.getElementById) {	return false; };
	// Attach triggers to select boxes
	if (document.getElementById("phoneMake")){
		document.getElementById("phoneMake").onchange = function(){ setUpModelList(this); return false; };
		// 	Builds up Make list from an array
		for (var i=0; i<MakeData.length; i++){					
			var optionTag = document.createElement('option');
			optionTag.setAttribute("value",MakeData[i]);					
			var optionText = document.createTextNode(MakeData[i]);
			optionTag.appendChild(optionText);					
			document.getElementById("phoneMake").appendChild(optionTag);					
		}
	}
	if(document.getElementById("phoneModel")){
		document.getElementById("phoneModel").onchange = function(){ setUpMake(this); return false; };
	}	
}
