//TODO: we should set the class rather than set each property:
var NON_SELECTED_DEVICE = "solid white 1px";
var SELECTED_DEVICE = "solid blue 1px";
var HOVER_DEVICE = "dashed blue 1px";
var selectedDeviceId=null;
var hoverDevice=null;

//Selects a device during registration process
function selectDevice(deviceElement) {
	//show the selected device as selected
	hideBorders(true);
	if (deviceElement == null) {
		var selectedModel = document.getElementById("selectedDeviceModel").value;
		if (selectedModel == "") //no device has been selected yet
			return;
		selectedDeviceId = selectedModel;
		document.getElementById(selectedDeviceId).style.border = SELECTED_DEVICE;
	} else {
		//set the device's id on the FORM so it can submit the value
		document.getElementById("selectedDeviceModel").value = deviceElement.id;
		selectedDeviceId = deviceElement.id;
		deviceElement.style.border = SELECTED_DEVICE;
	}
}

//Hides borders around a device - including selected device if param is true
function hideBorders(incSelect) {
	//Clear off border around previously selected devices if any and if requested thru' param
	if (incSelect==true && selectedDeviceId != null) {
		document.getElementById(selectedDeviceId).style.border = NON_SELECTED_DEVICE;
		selectedDeviceId = null;
	}
	//Clear off border around previously hovered devices if any
	if (hoverDevice != null) {
		hoverDevice.style.border = NON_SELECTED_DEVICE;
		hoverDevice = null;
	}
}

//Shows a border around the device on phone selection page
function showBorder(deviceElement) {
	hideBorders(false);
	if (selectedDeviceId == deviceElement.id) {
		return;
	}
	deviceElement.style.border = HOVER_DEVICE;
	hoverDevice = deviceElement;
}


//This should probably set a cookie or something that we'll
//use to detect/set the language.
function setLocale(lang) {
	
	if (lang != "#") {
		var cc;
		switch (lang) {
			case "en": cc = "UK"; break;
			case "ge": cc = "GE"; break;
		}
	  	//var url = "switchLocale.action?country="+cc+"&language="+lang+"&page=/home.action";
	  	var url = "home.action?change_locale=" + lang;
		document.location = url;
	}
}

//This should probably set a cookie or something that we'll
//use to detect/set the language.
function updateUserLanguage(lang) {
	
	if (lang != "#") {
		var cc;
		switch (lang) {
			case "en": cc = "UK"; break;
			case "ge": cc = "GE"; break;
		}
	  	//var url = "switchLocale.action?country="+cc+"&language="+lang+"&page=/home.action";
	  	var url = "updateUserLanguage.action?change_locale=" + lang;
		document.location = url;
	}
}

function setProgressIndicator() {
	var el = document.getElementById("signupProgressIndex");
	if(!el) {
		return;
	} 
	var index = el.value;
	if (index == 1) {
		document.getElementById("row2").className = "step1";
	} else if (index == 2) {
		document.getElementById("row2").className = "step2";
	} else {
		document.getElementById("row2").className = "step3";
	}
}

function popHelpWindow(){
	window.open('http://ssbeta.yav4.com/betalabs/nokiaemail','pageHelp','width=850,height=850,resizable=yes,scrollbars=yes')
}

function newCaptcha(){
	document.getElementById("securityMeasure").value = "";
	return "kaptcha?" + Math.floor(Math.random()*1000000);
}

function showMsg(msg){
	// Scroll to the top of the page so the error message is always going to be visible to the user
	scroll(0,0);
	
	var cStrutsError = document.getElementById("strutsErrors");
	
	if (cStrutsError != null) {
		// we have already displayed an error message on the screen
		// we just need to change the text
		cStrutsError.innerHTML = msg;
		return;
	}
	
	var cErrors = document.getElementById("strutsMessages");
 	if (! cErrors) {
 		cErrors = document.createElement("div");
 		cErrors.id = "strutsMessages";
 		cErrors.className = "strutsErrors";
 		document.getElementById("clientMessages").appendChild(cErrors);
 	}
 	cErrors.innerHTML = msg;
}

function autofocus() {
  var bFound = false;
  // for each form
  for (f=0; f < document.forms.length; f++) {
    // for each element in each form
    for(i=0; i < document.forms[f].length; i++) {
	  var anElem = document.forms[f][i];
	  // if it's not a hidden element
      if (anElem.type != "hidden") {
        // and it's not disabled
        if (anElem.disabled != true) {
            // set the focus to it, unless it's a field that has pre-populated text that gets erased on focus
			if (anElem.id != "countryCode" && anElem.id != "mobileNumber" && anElem.id != "msisdn") {
            	anElem.focus();
            	var bFound = true;
            }
        }
      }
      // if found in this element, stop looking
      if (bFound == true)
        break;
    }
    // if found in this form, stop looking
    if (bFound == true)
      break;
  }
}

function newCaptchaText() {
	document.getElementById('kaptcha').src = newCaptcha(); 
	document.getElementById('securityMeasure').focus(); 
	return false;
}

function clearIfNew(id, defaultValue) {
	var fld = document.getElementById(id);
	if (fld.value == defaultValue) {
		fld.value = "";
		fld.style.color = "#444433";
		fld.style.fontStyle = "normal";
	}
}

function setDefaultMobileNumberValues(ccDefaultValue, mnDefaultValue) {
	var defColor = "#999999";
	var defStyle = "italic";
	
  	var cc = document.getElementById("countryCode");  
	if (cc && cc.value == "" ) {
		cc.value = ccDefaultValue;
		cc.style.color = defColor;
		cc.style.fontStyle = defStyle;
	}

	var mn = document.getElementById("mobileNumber");
	if (!mn) {
		mn = document.getElementById("msisdn");
	}
	if (mn && mn.value == "" ) {
		mn.value = mnDefaultValue;
		mn.style.color = defColor;
		mn.style.fontStyle = defStyle;
	}
}


function writeFloodlight(tag){
	foo = '<iframe src="' + tag + ";ord=" + getRandom() + '?" width="1" height="1" frameborder="0"></iframe>';
	document.write(foo);
}
function tagEvent(eventId){
	document.cookie = "nm-" + eventId + "=;path=/";
}
function unTagEvent(eventId){
	var cookie = getCookiedTagEvent(eventId);
	if (cookie != null && cookie != "") {
		document.cookie = "nm-" + eventId + "=" + escape(eventId) + ";path=/;expires=01/01/1999 00:00:00";
	}
}
function getCookiedTagEvent(eventId){
	if (document.cookie.length > 0){
		start = document.cookie.indexOf("nm-"+eventId + "=");
		//alert(eventId + ";" + start);
		if (start > -1){
			return true;
		}
	}
	return false;
}

function getRandom(){
	var axel = Math.random()+"";
	var a = axel * 10000000000000;
	return a;
}
