// Start of Script (global.js)

function greaterthan(inputStr1, inputStr2)
{
	return (parseInt(inputStr1) > parseInt(inputStr2));
}

function smallerthan(inputStr1, inputStr2)
{
	return (parseInt(inputStr1) < parseInt(inputStr2));
}

function isempty(inputStr)
{
	return (String(inputStr).length == 0);
}

//	Check for Non-negative Integer Pattern
function checkint(s)
{
	return /^\d+$/.test(s);
}

//	Check for Non-empty Alphanumeric String (with optional non-leading/non-trailing underscore)
function checknameformat(s)
{
	return /^[a-z0-9](\w*[a-z0-9])?$/i.test(s);
}

//	Check for IPv4 Address "x.x.x.x" where 0 <= x <= 255
function checkipformat(s)
{
	var r = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(s);
	return (r && r[1] <= 255 && r[2] <= 255 && r[3] <= 255 && r[4] <= 255);
}


function validateIPv4(ip, prompt, obj)
{
	ip = String(ip);
	prompt = String(prompt);
	if (ip == "undefined" || prompt == "undefined" || String(obj) == "undefined")
	{
		alert("validateIPv4(): undefined value received, check script");
		return false;
	}
	
	if (isempty(ip))
	{
		alert("No " + prompt + "!");
		obj.focus();
		return false;
	}

	if (! checkipformat(ip))
	{
		alert("Invalid "+prompt+"!");
		obj.focus();
		return false;
	}

	return true;
}


// Simple Cookie Management
// ===== Start of Simple Cookie Management Functions =====

function setCookie(name, value, days)
{
	var expires = "";
	var path = "; path=/";
	if (days)
	{
		var date = new Date();
		if (days > 0)
		{
			date.setTime(date.getTime()+(days*24*60*60*1000));
		}
		expires = "; expires=" + date.toGMTString();
	}
	document.cookie = name + "=" + value + expires + path;
	return;
}
function getCookie(name)
{
	var prefix = name + "=";
	var arr = document.cookie.split("; ");
	for (var i = 0; i < arr.length; i++)
	{
		var str = arr[i];
		if (str.indexOf(prefix) == 0)
		{
			return str.substring(prefix.length, str.length);
		}
	}
	return null;
}
function clearCookie(name)
{
	return setCookie(name, "", -1);
}

// ===== End of Simple Cookie Management Functions =====


// Browser validation (show warning if the browser is not among compatible type!)
// Code based on PCMS ajax toolkit
// ===== Start of Browser Compatibility Verification Code =====
    
// Supported browser verification code
//	written by Kenny Kwok, 2007/02/15
//	(replace the obsolete function checkBrowser(),
//	which was fooled by opera browser, which may pretend as IE)
function verifyBrowserCompatibility() 
{
	// LOGIC ORDER IS IMPORTANT
	var bv = navigator.userAgent;
	if (/Opera/.test(bv))
	{	// Silly Opera~ Don't fool me as MSIE 6.0/Mozilla 5.0 anymore!
		return false;
	}

	var chrome_identifier = /Chrome/.exec(bv);
	if (chrome_identifier)
	{	// It's Chrome!, are you version 2.0 (or above)?
		var arr = /Chrome\/([0-9]*[.][0-9]*)([.][0-9]*)?/.exec(bv);
		var chrome_version = (arr && arr.length > 1? parseFloat(arr[1]): 0.0);
		var chrome_patch_level = (arr && arr.length > 2? parseFloat(arr[2]): 0.0);
		return ((chrome_version == 2.0 && chrome_patch_level >= 0) || (chrome_version > 2.0));
	}

	var safari_identifier = /Safari/.exec(bv);
	if (safari_identifier)
	{	// It's Safari!, are you version 3.1.1 (or above)?
		var arr = /Version\/([0-9]*[.][0-9]*)([.][0-9]*)?/.exec(bv);
		var safari_version = (arr && arr.length > 1? parseFloat(arr[1]): 0.0);
		var safari_patch_level = (arr && arr.length > 2? parseFloat(arr[2]): 0.0);
		return ((safari_version == 3.1 && safari_patch_level >= 0.1) || (safari_version > 3.1));
	}

	var firefox_identifier = /Firefox\/([0-9]*[.][0-9]*)/.exec(bv);
	if (firefox_identifier)
	{	// Lovely Firefox, are you version 1.5 (or above)?
		var firefox_version = parseFloat(firefox_identifier[1]);
		return (firefox_version >= 1.5);
	}

	var ie_identifier = /MSIE ([0-9]*[.][0-9]*)/.exec(bv);
	if (ie_identifier)
	{	// Nasty Internet Explorer, are you version 6.0 (or above)?
		var ie_version = parseFloat(ie_identifier[1]);
		return (ie_version >= 7.0);
	}
	return false;
}   
if (alertBrowserCompatibility_isDone == undefined)
{       
	var alertBrowserCompatibility_isDone = false;
}
function getmsgBrowserCompatibility()
{
	return "\
 - Firefox 2.0 or above<br>\
 - Safari 3.1.1 or above<br>\
 - Chrome 2.0 or above<br>\
 - Internet Explorer 7.0 or above<br>\
";
}
function alertBrowserCompatibility()
{
	if (alertBrowserCompatibility_isDone) return;
	if (!verifyBrowserCompatibility())
	{
		var warning_msg = "\
WARNING: Your browser is not compatible with\n\
Peplink Balance web administration interface\n\n\
Supported browsers are:\n\
Firefox 2.0 (or above)\n\
Safari 3.1.1 (or above)\n\
Internet Explorer 7.0 (or above)\n\n\
YOU MAY GET UNEXPECTED RESULT IF YOU PROCEED";
		alert(warning_msg);
	}
	alertBrowserCompatibility_isDone = true;
	return;
}

// This script automatically check for browser compatibility exactly once
//  If this behavior is not as expected, comment the line below
//alertBrowserCompatibility();

// ===== End of Browser Compatibility Verification Code =====

// End of Script (global.js)
