function enterKeyTrap(e, onEnterEvalMethod) {
    var keycode;

    if (window.event){
    	keycode = window.event.keyCode;
    }else if (e){
    	keycode = e.which;
    }

    if (keycode == 13){
        eval(onEnterEvalMethod);
        return false;
    }

    return true;
}

function fitStringToWidthOneLine(str,width,className){
	// str    A string where html-entities are allowed but no tags.
	  // width  The maximum allowed width in pixels
	  // className  A CSS class name with the desired font-name and font-size. (optional)

	  //Create a span element that will be used to get the width
	  var span2 = document.createElement("span");
	  //Allow a classname to be set to get the right font-size.
	  if (className) span2.className=className;
	  span2.style.display='inline';
	  span2.style.visibility = 'hidden';
	  span2.style.padding = '0px';
	  document.body.appendChild(span2);
	  var result = str;
	  span2.innerHTML = result;

	  var lengthResult = result.length;
	  if(span2.offsetWidth > width){
		  while (span2.offsetWidth > width){
		  lengthResult--;
		  result = result.truncate(lengthResult, '');
		  span2.innerHTML = result;
		  }
		  result = result.truncate((lengthResult - 3), '');
		  result = result + '...';
	  }
	  document.body.removeChild(span2);
	  return result;
}

function calculateOffsetWidth(str, className){
	 var span = document.createElement("span");
	  //Allow a classname to be set to get the right font-size.
	  if (className) span.className=className;
	  span.style.display='inline';
	  span.style.visibility = 'hidden';
	  span.style.padding = '0px';
	  document.body.appendChild(span);
	  var result = str;
	  span.innerHTML = result;
	  var offWidth = span.offsetWidth;
	  document.body.removeChild(span);
	  return offWidth;
}

function sortStringArray(strArray) {
	var i;
	var j;
	var temp;
	for (i=0; i<strArray.length-1; i++){
		for (j=i; j<strArray.length; j++){
			if (strArray[j] < strArray[i]){
				temp = strArray[j];
				strArray[j] = strArray[i];
				strArray[i] = temp;
			}
		}
	}
	return strArray;
}

var currentModal;
var interruptedModal;


	function showModalPanel(modalName){
		if (currentModal==null || currentModal == undefined){
			Richfaces.showModalPanel(modalName);
			currentModal=modalName
		}
	}

	function hideModalPanel(modalName){
		if (modalName == currentModal){
			currentModal=null;
			Richfaces.hideModalPanel(modalName);

			if (interruptedModal != undefined && interruptedModal != null){
				showModalPanel(interruptedModal);
				interruptedModal=null;
			}
		} else if (modalName == interruptedModal){
			interruptedModal=null;
		}
	}

	function interruptModal(modalName){
		if (modalName == currentModal){
			return;
		}
		if (currentModal != undefined){
			Richfaces.hideModalPanel(currentModal);
		}
		interruptedModal=currentModal;
		currentModal=undefined;
		showModalPanel(modalName);
	}

	function disableButton(buttonId){
	    var buttonSpan = document.getElementById(buttonId + 'buttonMain');
	    if(buttonSpan != null){
	    	buttonSpan.style.display = "none";
	    }

	    var buttonSpanDisabled = document.getElementById(buttonId + 'buttonMainDisabled');
	    if(buttonSpanDisabled != null){
	    	buttonSpanDisabled.style.display = "inline-block";
	    }
	}

	function enableButton(buttonId){
	    var buttonSpan = document.getElementById(buttonId + 'buttonMain');
	    if(buttonSpan != null){
	   		buttonSpan.style.display = "inline-block";
	    }

	    var buttonSpanDisabled = document.getElementById(buttonId + 'buttonMainDisabled');
	    if(buttonSpanDisabled != null){
	    	buttonSpanDisabled.style.display = "none";
	    }
	}

	function renderButton(buttonId, render){
		var buttonSpan = document.getElementById(buttonId + 'buttonMain');
		var buttonSpanDisabled = document.getElementById(buttonId + 'buttonMainDisabled');
		if(render){
		    buttonSpan.style.display = "inline-block";
		    buttonSpanDisabled.style.display = "none";
		} else {
		    buttonSpan.style.display = "none";
		    buttonSpanDisabled.style.display = "none";
		}
	}

	function populateDeviceSwitch(context, pinLabel, imgNALabel){
		if(getValue('platforms', 'deviceCookie') > 1){
			$('devicesItem').style.display = 'inline';
		}
		var radioContent = '<table class="awDeviceSwitchRadio" >';
		var labelContent = '<table class="awDeviceSwitchRadioLabels">';
		var imgContent = ' ';
		var platformCount = parseInt(getValue('platforms', 'deviceCookie')) + 1;
		radioContent+="<tr>";
		for(i = 1; i < platformCount; i++) {

			radioContent+="<td>";
			radioContent+='<input type="radio" name="deviceSwitchRadio" onclick="changeDevice(); return false;" value="" id="deviceSwitchRadio:' + i + '" />';
			radioContent+="</td>";

			if(i == 2){
				imgContent+='<div id="' + i + 'ImageWrapper" class="awFloatLeft awDeviceWrapper" onmouseover="this.className=\'awDeviceWrapperOver2\'" onmouseout="this.className=\'awDeviceWrapper\'" onclick="selectDeviceRadioOption(\'deviceSwitchRadio:' + i + '\');">';
				imgContent+='<img id="' + i + 'deviceImg" height="90px" width="140px" class="awMarginBottom-Five" src=""/>';
			} else {
				imgContent+='<div id="' + i + 'ImageWrapper" class="awFloatLeft awDeviceWrapper" onmouseover="this.className=\'awDeviceWrapperOver\'" onmouseout="this.className=\'awDeviceWrapper\'" onclick="selectDeviceRadioOption(\'deviceSwitchRadio:' + i + '\');">';
				imgContent+='<img id="' + i + 'deviceImg" height="90px" width="80px" class="awMarginBottom-Five" src=""/>';
			}
			imgContent+='<br />';
			imgContent+= "BlackBerry ";
			imgContent+='<span id="' + i + 'deviceFamily" class="awDeviceFamily"></span>';
			imgContent+='<span id="' + i + 'deviceModel" class="awDeviceModel" ></span>';
			imgContent+='<br />';
			imgContent+='<span id="' + i + 'devicePin" style="color:#939393; font-weight:bold"></span>';
			imgContent+="</div>";
		}
		radioContent+="</tr>";
		radioContent+="</table>";
		$('headerForm:deviceSwitchRadioWrapper').innerHTML = radioContent;
		labelContent+="</tr>";
		labelContent+="</table>";
		$('headerForm:deviceSwitchRadioLabelsWrapper').innerHTML = labelContent;
		$('headerForm:deviceSwitchImageWrapper').innerHTML = imgContent;

		var activePlatform =  getValue('activePlatform', 'deviceCookie');
		var activeRadio = "deviceSwitchRadio:";
		activeRadio+= activePlatform;
		selectDeviceRadioOption(activeRadio);

		var activeImage = activePlatform;
		activeImage+="ImageWrapper";

		if(activePlatform == '2'){
			$('2ImageWrapper').setStyle({
				backgroundImage: "url(" + context + "/img/devices/glow2.png)",
				backgroundPosition: '70% 0%',
				backgroundRepeat: 'no-repeat'});
		} else {
			$(activePlatform + 'ImageWrapper').setStyle({
				backgroundImage: "url(" + context + "/img/devices/glow.png)",
				backgroundPosition: '50% 20%',
				backgroundRepeat: 'no-repeat'});
		}

		for(i = 1; i < platformCount; i++){
			var activePlatformDevice = 'activeDevice' + i;
			var deviceData = getValue(activePlatformDevice, 'deviceCookie');
			var deviceDataArray = deviceData.split(":");
			var family = deviceDataArray[0];
			var model = deviceDataArray[1];
			var pin = deviceDataArray[2];
			var image = deviceDataArray[3];

			if(family != null && family != " "){
				$(i + 'deviceFamily').innerHTML = "&nbsp;" +  family + "&nbsp;";
			}
			$(i + 'deviceModel').innerHTML = model;
			$(i + 'devicePin').innerHTML = pinLabel + " " + pin;
			if(image == "0"){
				$(i + 'ImageWrapper').innerHTML = imgNALabel;
			} else {
				$(i + 'deviceImg').src = context + '/servedimages/' + image + '.png/?t=15';
			}
			$('deviceSwitchRadio:' + i).value = pin;
		}
	}
	
	var $j = jQuery.noConflict();
	var gen_page=new Array();
	var slideInProgress = false;
	function generatePages(slideId, size, totalNumber){
		$j('#' + slideId + 'Paginator1').text((gen_page[slideId]*size) - (size - 1));
		if((gen_page[slideId]*size) > totalNumber){
			$j('#' + slideId + 'Paginator2').text(totalNumber);
		}else{	
			$j('#' + slideId + 'Paginator2').text(gen_page[slideId]*size);
		}
	}	

	function goRight(slideId, slideWidth, size, totalNumber){
		if(!slideInProgress){
			slideInProgress = true;
			checkInitialized(slideId);
			var maxPages = Math.ceil(totalNumber/size);
			if (gen_page[slideId] == 1 && (size >= totalNumber)){
				$j('#' + slideId + 'RightArrow').css({"display": "none"});
				$j('#' + slideId + 'RightArrowDisabled').css({"display": "block"});
				slideInProgress = false;
			} else {
				gen_page[slideId]++;
				generatePages(slideId, size, totalNumber);
				//check if on last page and disable/enable the right and left arrow accordingly.
				if ((gen_page[slideId]*size) >= totalNumber){
					$j('#' + slideId + 'RightArrow').css({"display": "none"});
					$j('#' + slideId + 'RightArrowDisabled').css({"display": "block"});
				} 
		
				$j('#' + slideId + 'LeftArrow').css({"display": "block"});
				$j('#' + slideId + 'LeftArrowDisabled').css({"display": "none"});
				
				var $newLeft = ((size*slideWidth) * (0 - gen_page[slideId])) + (size*slideWidth);
				
				$j('#' + slideId + 'AppSlider').animate({'left':$newLeft+'px'}, size*200, function(){
					slideInProgress = false;
				});
			}
		}
	}

	function goLeft(slideId, slideWidth, size, totalNumber){
		if(!slideInProgress){
			slideInProgress = true;
			checkInitialized(slideId);
			gen_page[slideId]--;
			generatePages(slideId, size, totalNumber);
			if (gen_page[slideId] <= 1){
				$j('#' + slideId + 'LeftArrow').css({"display": "none"});
				$j('#' + slideId + 'LeftArrowDisabled').css({"display": "block"});
			} 
			
			$j('#' + slideId + 'RightArrow').css({"display": "block"});
			$j('#' + slideId + 'RightArrowDisabled').css({"display": "none"});
			
			var	$newLeft = (size*slideWidth) - ((size*slideWidth) * (gen_page[slideId]));
			
			$j('#' + slideId + 'AppSlider').animate({'left':$newLeft+'px'}, size*200, function(){
				slideInProgress = false;
			});
		}
	}
	function checkInitialized(slideId){
		if (typeof(gen_page[slideId])== "undefined"){
			gen_page[slideId]=1;
		}
	}
	
	function sizeTT(id){
		var dynamicHeight = $j('#' + id + 'awTTMiddle').height();
		$j('#' + id + 'awTTLeft').height(dynamicHeight - 122);
		$j('#' + id + 'awTTRight').height(dynamicHeight - 122);
	}
	
	function hideTT(id){
		 document.getElementById(id).style.display = "none";
	}		
 function sizeFakeTT(id){
	 var middleTT = $j('#' + id + 'awTTFakeMiddle');
	 var middleHeight = middleTT.height();
	 $j('#' + id + 'awTTFakeLeft').height(middleHeight - 122);
	 $j('#' + id + 'awTTFakeRight').height(middleHeight - 122);
 }
