var rowStyle;

function isMobileDevice(){
	function Client() {  
	}  
	  
	Client.prototype.mobileClients = [  
	    "midp",  
	    "240x320",  
	    "blackberry",  
	    "netfront",  
	    "nokia",  
	    "panasonic",  
	    "portalmmm",  
	    "sharp",  
	    "sie-",  
	    "sonyericsson",  
	    "symbian",  
	    "windows ce",  
	    "benq",  
	    "mda",  
	    "mot-",  
	    "opera mini",  
	    "philips",  
	    "pocket pc",  
	    "sagem",  
	    "samsung",  
	    "sda",  
	    "sgh-",  
	    "vodafone",  
	    "xda",  
	    "iphone",  
	    "android",
	    "ipad",
	    "ipod"
	];  
	  
	Client.prototype.isMobileClient = function(userAgent)  
	{  
	    userAgent=userAgent.toLowerCase();  
	    for (var i in this.mobileClients) {  
	        if (userAgent.indexOf(this.mobileClients[i]) != -1) {  
	            return true;  
	        }  
	    }  
	    return false;  
	}  
	  
	var client = new Client();  			
	return client.isMobileClient(navigator.userAgent);
}

function getStartPage(){
	var mobileSite = '../pages/mobile/mobileStartPage.jsf';
	var desktopSite = '../pages/test/test.jsf'; 
	
	if(isMobileDevice())
	{	
		return mobileSite;							
	}
	else
	{	
		return desktopSite;				
	}
}

function getLoginPage(){
	var mobileSite = 'mobileLogin.jsf';
	var desktopSite = 'standardLogin.jsf';
	if(isMobileDevice())
	{	
		return mobileSite;							
	}
	else
	{	
		return desktopSite;				
	}
}

// highlight for DataTable   
function hl(thisObj) 
{
	rowStyle = thisObj.className;
	thisObj.className = "rowHighlighted"; 

}
// removeHighlight for DataTable                
function rhl(thisObj)
{   	
	thisObj.className = rowStyle;
} 

function showLoadingProgress()
{
	document.getElementById('progressPanelContainer').style.display='';
}

function showLoadingProgressMsg(progressText)
{
	document.getElementById('progressPanelContainer').style.display='';
	document.getElementById('progressPanelText').innerHTML=progressText;
}

function closeLoadingProgress()
{
	document.getElementById('progressPanelContainer').style.display='none';
}

function toggleDisplay(id)
{
    var s = document.getElementById(id).style;
    if (s.display == 'none')
    {
        s.display = 'inline';
        var pxl = document.getElementById(id).offsetHeight;
        scrollDown(pxl);
    }
    else
    	s.display = 'none';    
}

// Some functions for cookie handling

// Set cookie value
//
// param1: name
// param2: value
// param3: expiration in days
// param4: path
// param5: domain
// param6: secure

function setCookie(name, value)
{
	// Prepare arguments
	var args = setCookie.arguments;
	var num_args = setCookie.arguments.length;
	var expires = (num_args > 2) ? args[2] : null;
	var path = (num_args > 3) ? args[3] : null;
	var domain = (num_args > 4) ? args[4] : null;
	var secure = (num_args > 5) ? args[5] : false;
	
	// Calculate expiration date
	var today = new Date();
	if (expires)
	  expires = expires * 1000 * 60 * 60 * 24;
	var expirationDate = new Date(today.getTime() + (expires));
	
	// Set cookie	
	document.cookie = name + "=" + encodeURIComponent(value) +
					  ((expires == null) ? "" : (";expires=" + expirationDate.toGMTString())) +
					  ((path == null) ? ";path=/" : (";path=" + path)) +
					  ((domain == null) ? "" : (";domain=" + domain)) +
					  ((secure == true) ? ";secure" : "");
}

// Get cookie value
//
// param1: name of cookie to retrieve
// returns: value of cookie, null on error
function getCookie(name)
{
	var test = document.cookie;
	var start = document.cookie.indexOf(name + "=");
	if ((!start) && (name != document.cookie.substring(0, name.length)))
		return null;
	if ( start == -1 )
		return null;
	var len = start + name.length + 1;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
}

// Delete cookie
//
// param1: cookie name
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
		document.cookie = name + "=" +
						  ((path) ? ";path=" + location.path : "") +
						  ((domain) ? ";domain=" + location.hostname : "" ) +
						  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// Check for browser cookie support
//
// returns: true, when successful
function checkCookieSupport()
{
	setCookie("test", "none", "", location.pathname + "/testpath", location.hostname);
	if (getCookie("test"))
		return true;
	else
		return false;
}

function setDisabled(isDisabled, disableId)
{
  if (disableId != null)
    document.getElementById(disableId).disabled = isDisabled;
}

function confirmWithLoading(confirmText)
{
	if(confirm(confirmText))
	{
		showLoadingProgress();
		return true;
	}
	return false;
}


function confirmWithLoadingProgressText(confirmText, progressText)
{
	if(confirm(confirmText))
	{
		showLoadingProgressMsg(progressText);
		return true;
	}
	return false;
}

function scrollToBottom()
{
	window.scrollBy(0,document.body.scrollHeight);
}

function scrollDown(pxl)
{
	window.scrollBy(0,pxl);
}
