/*******************
function jsUCase(s)
function jsLen(str)
function jsRight(str, n)
function jsMid(str, start, len)
function jsInStr(strSearch, charSearchFor)
function constrainToAlphaKeyPress(evt, obj)
function constrainToNumericKeyPress(evt, obj)
function constrainToNumberKeyPress(evt)
function convertKeyToUpperCase(evt)
function jsIsNumeric(sString)
function isMaxLength(obj)
function doPriceSync(gp, bShowWithGST, obj)
function window_openWH(url, w, h)
function validateEmail(obj, msg, bAllowBlank)
function nonWS(s)
function jsTrim(s)
function validateNoQuotes (obj, msg)
function validateNonZero(obj, msg)
function validatePresence(obj, msg)
function validatePresenceAlphaNumeric(obj, msg, msgAlpha)
function validatePresenceNonNegNumber(obj, msg)
function validatePresenceLimit(obj, sMsg, nLimit, sLimitMsg)
function validateCurrency(sName, sMessage)
function validatePresenceBound(obj, sMsg, nLow, nHigh, sBoundMsg)
function validatePresenceAtLeast(obj, sMsg, nLow)
function isStringAlphaNumeric(s)
function isCharAlphaNumeric(c)
function validateImageSelection(sSel, sImgBrowse, sMsg)
function trim(s)
function removeAllSpaces(s)
function validatePhoneNumber(obj)
function validatePostcode(obj)
function CheckIllegalChars(name, obj)
function validatePositiveCurrency(obj, sMsg)
function validatePrice(obj)
function remove(sValue, sSearch)

*******************/

//conver to upper case.
function jsUCase(s)
{
    return String(s).toUpperCase();
}

// IN: str - the string whose length we are interested in
// RETVAL: The number of characters in the string
function jsLen(str)
{
	return String(str).length;
}


function jsRight(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else 
    {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

//IN: str - the string we are LEFTing
//    start - our string's starting position (0 based!!)
//    len - how many characters from start we want to get
//RETVAL: The substring from start to start+len
function jsMid(str, start, len)
{
	// Make sure start and len are within proper bounds
	if (start < 0 || len < 0) return "";

	var iEnd, iLen = String(str).length;
	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;

	return String(str).substring(start,iEnd);
}


// InStr function written by: Steve Bamelis - steve.bamelis@pandora.be
function jsInStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < jsLen(strSearch); i++)
	{
	    if (charSearchFor == jsMid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}


function constrainToAlphaKeyPress(evt, obj)
{
    if (!evt) evt = window.event    
    
	if (evt.shiftKey)
		return false;
	else 
	{
		var key = evt.keyCode;
		if (!key) key = evt.keycode;
		if (!key) key = evt.charCode;

		key = parseInt(key);

		if (((key >= 65) && (key <= 90)) || ((key >= 97) && (key <= 122)) ||(key == 127) || (key == 27) || (key == 9) || (key == 11) || (key == 8) || (key == 37) || (key == 39) || (key == 46))
			return true;
		else
			return false;
	}
}


function constrainToNumericKeyPress(evt, obj)
{
    if (!evt) evt = window.event    
    
	if (evt.shiftKey)
		return false;
	else 
	{
		var key = evt.keyCode;
		if (!key) key = evt.keycode;
		if (!key) key = evt.charCode;

		key = parseInt(key);
		
		if (((key >= 48) && (key <= 57)) || (key == 127) || (key == 27) || (key == 9) || (key == 11) || (key == 8) || (key == 37) || (key == 39) || (key == 46) || (key == 190))
			if (((key == 46) || (key == 190)) && (jsInStr(obj.value, ".") > 0))
				return false;
			else
				return true;
		else
			return false;
	}
}


function constrainToNumberKeyPress(evt)
{
    if (!evt) evt = window.event    
    
	if (evt.shiftKey)
		return false;
	else 
	{
		var key = evt.keyCode;
		if (!key) key = evt.keycode;
		if (!key) key = evt.charCode;

		key = parseInt(key);
		if (((key >= 48) && (key <= 57)) || (key == 127) || (key == 27) || (key == 9) || (key == 11) || (key == 8) || (key == 37) || (key == 39) || (key == 46))
			return true;
		else
			return false;
	}
}


function convertKeyToUpperCase(evt)
{
    if (!evt) evt = window.event;

    var key = evt.keyCode;
    if (!key)
    {
        key = evt.keycode;
    }
    if (!key)
    {
		key = evt.charCode;
    }

    key = parseInt(key);

    if ((key >= 97) && (key <= 122))
    {
        evt.charCode = key - 32;
    }
}


//  check for valid numeric strings	
function jsIsNumeric(sString)
{
	var sValidChars = "0123456789.";
	var sChar;
	var blnResult = true;

//   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
	for (i = 0; i < sString.length && blnResult == true; i++)
	{
		sChar = sString.charAt(i);
		if (sValidChars.indexOf(sChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}


function isMaxLength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}


function doPriceSync(gp, bShowWithGST, obj)
{
	// Note: Keep tenths of cents...
	var p1 = obj.value;

	if (bShowWithGST)
	{
		obj.value = Math.round(p1 * (1.0 + gp / 100.0) * 1000) / 1000.0;
	}
	else
	{
		obj.value = Math.round(p1 / (1.0 + gp / 100.0) * 1000) / 1000.0;
	}
}


function window_openWH(url, w, h)
{
	window.open(url, '_blank', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + w + ',height=' + h);
}


function validateEmail(obj, msg, bAllowBlank)
{
 var str = obj.value;
 if (jsTrim(str) == "")
 {
  if (bAllowBlank)
  {
   return true;
  }
  else
  {
   alert(msg);
   obj.focus();
   return false;
  }
 }

 var at = "@";
 var dot = ".";
 var lat = str.indexOf(at);
 var lstr = str.length;
 var ldot = str.indexOf(dot);

 if ( (str.indexOf(at) == -1)
   || (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr)
   || (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
   || (str.indexOf(at, (lat + 1)) != -1)
   || (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot)
   || (str.indexOf(dot, (lat + 2)) == -1)
   || (str.indexOf(" ") != -1) )
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}

 
function nonWS(s)
{
	if (s == " ") return false;
	if (s == "\t") return false;
	return true;
}


function jsTrim(s)
{
 var nLen = s.length;
 var nStart = 0;
 var nEnd = nLen - 1;
 var i;

 for (nStart = 0; nStart <= nEnd; nStart++)
  if (nonWS(s.charAt(nStart))) break;

 for (nEnd = nLen - 1; nEnd >= nStart; nEnd--)
  if (nonWS(s.charAt(nEnd))) break;

 return s.substr(nStart, nEnd - nStart + 1);
}


function validateNoQuotes (obj, msg)
{
 var s = new String(obj.value);
 var i;
 for (i = 0; i < s.length; i++)
 {
  switch (s.charAt(i))
  {
   case "'":
   case '"':
    alert(msg);
    obj.focus();
    return false;
  }
 }
 return true;
}


function validateNonZero(obj, msg)
{
 if (obj == null)
 {
  return true;
 }
 if (obj.value == '0')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresence(obj, msg)
{
 if (obj == null)
 {
  return true;
 }
 if (jsTrim(obj.value) == '')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceAlphaNumeric(obj, msg, msgAlpha)
{
 if (jsTrim(obj.value) == '')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 if (!isStringAlphaNumeric(obj.value))
 {
  alert(msgAlpha);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceNonNegNumber(obj, msg)
{
 if (jsTrim(obj.value) == '')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 if (!isFinite(obj.value) || obj.value < 0)
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}


function validateLimit(obj, nLimit, sLimitMsg)
{
 if (obj.value.length > nLimit)
 {
  alert(sLimitMsg);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceLimit(obj, sMsg, nLimit, sLimitMsg)
{
 if (jsTrim(obj.value) == "")
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (obj.value.length > nLimit)
 {
  alert(sLimitMsg);
  obj.focus();
  return false;
 }
 return true;
}


function validateCurrency(sName, sMessage)
{
 var ta;
 ta = document.getElementById(sName);
 if (ta)
 {
  if (!isFinite(ta.value))
  {
   alert(sMessage);
   ta.focus();
   return false;
  }
 }
 return true;
}


function validatePresenceBound(obj, sMsg, nLow, nHigh, sBoundMsg)
{
 if (!isFinite(obj.value))
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (obj.value < nLow || obj.value > nHigh)
 {
  alert(sBoundMsg);
  obj.focus();
  return false;
 }
 return true;
}


function validatePresenceAtLeast(obj, sMsg, nLow)
{
 if (jsTrim(obj.value) == "")
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (!isFinite(obj.value))
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 if (obj.value < nLow)
 {
  alert(sMsg);
  obj.focus();
  return false;
 }
 return true;
}

function isStringAlphaNumeric(s)
{
 var i;
 for (i = s.length - 1; i >= 0; i--)
  if (!isCharAlphaNumeric(s.charAt(i))) return false;

 return true;
}


function isCharAlphaNumeric(c)
{
 return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_'));
}


function isStringAlpha(s)
{
	var i;
	for (i = s.length - 1; i >= 0; i--)
	{
		if (!isCharAlpha(s.charAt(i)))
		{
			return false;
		}
	}
	return true;
}


function isCharAlpha(c)
{
	return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}


function validateImageSelection(sSel, sImgBrowse, sMsg)
{
 var imgSel = document.getElementById(sSel);
 if (imgSel.value == '/upload')
 {
  var imgUp = document.getElementById(sImgBrowse);
  if (jsTrim(imgUp.value) == '')
  {
   alert(sMsg);
   imgUp.focus();
   return false;
  }
 }
 return true;
}


function trim(s)
{	
	if((s==null)||(typeof(s)!='string')||!s.length)
	return'';
	return s.replace(/^\s+/,'').replace(/\s+$/,'')
} 


function removeAllSpaces(s)
{	
	if ((s == null) || (typeof(s) != 'string') || !s.length)
	{
		return '';
	}
	return s.replace(/\s+/g, '');
} 


function validatePhoneNumber(obj)
{
    var p = removeAllSpaces(obj.value);

    //if phone number is empty 
    if (p == '')
    {
		alert('Phone number cannot be empty');
		obj.focus();
		return false;
    }
	
	//check if the phone number is a number 
	if (isNaN(p) ||  p < 0 )
	 {
	  alert('Please enter a valid number. \ne.g 03 9999 9999 or 0499 999 999');
	  obj.focus();
	  return false;
	 }
	
	 	 
	//check if the phone number is 10 digits 
	if (( p.length !=10) && ( p != ''))
	{
	  alert('Please enter a valid number. \ne.g 03 9999 9999 or 0499 999 999');
	  obj.focus();
	  return false;
	}
	
	//Check if it is a integer
	if (Math.round(p) != p)
	{
	  alert('Please enter an integer. \ne.g 03 9999 9999 or 0499 999 999');
	  obj.focus();
	  return false;
	}
	
   	return true;
}


function validatePostcode(obj)
{
	var p = removeAllSpaces(obj.value)

	//if phone number is empty 
	if (p == '')
	 {
	  alert('Please enter your Postcode.');
	  obj.focus();
	  return false;
	 }
	 
	//check if the phone number is a number 
	if (isNaN(p) || p < 0 )
	 {
	  alert('Postcode number must be a number.');
	  obj.focus();
	  return false;
	 }
	
	
 
	//check if the phone number is 10 digits
 	if ((p.length !=4) && (p != ''))
	{
	  alert('Postcode must be 4 digits. Please enter a valid Postcode.');
	  obj.focus();
	  return false;
	} 	 
	
	//Check if it is a integer
	if (Math.round(p) != p)
	{
	  alert('Postcode must be a number . Please enter a valid Postcode.');
	  obj.focus();
	  return false;
	}

	return true;
}

//check if it is [a-zA-z0-9], including underscore
function CheckIllegalChars(name, obj)
{
//	var illegalChars = /\W/; // allow letters, numbers, and underscores
//	if (illegalChars.test(removeAllSpaces(obj.value))) 
//	{
  //      alert("The " + name + " contains illegal characters.");
	//	obj.focus();
      //  return false; 
    //}
   return true;   
}

function validatePositiveCurrency(obj, sMsg)
{
	var ta;
	ta = removeAllSpaces(obj.value);
	var num = new Number(ta);
	ta = num.toFixed(2);
	
	if (isNaN(ta) || ta < 0 )
	 {
		alert(sMsg);
		obj.focus();
		return false;
	 }
 
	 return true;
}

function validatePrice(obj)
{
	//var validChar= "0123456789.,";
	var priceValue = obj.value ;
	//var bValidPrice = false;
	
	//the function below use for a price with comma
	/*for (var i = 0; i < priceValue.length; i++)
	{
		bValidPrice = false;
		for (j = 0; j < validChar.length; j++)
		{
			if(priceValue.charAt(i) == validChar.charAt(j))
			{
				bValidPrice = true;
			}	
		}
		
		if (!bValidPrice)
		{
			alert ("Please enter the valid price.");
			obj.focus();
			return false;
		}
	}*/
	
	//check if the currency is out of range, Range for Currency in Access is 15 digits include 3 digit for decimals
	if (priceValue > 999999999999)
	{
		alert ("Price is out of range, please try again.");
		obj.focus();
		return false;
	}
	
	return true;
}


function remove(sValue, sSearch)
{
	var nNewVaue = sValue.replace(sSearch,'')
	return nNewVaue;
}

function disableAllLinks()
{
	var nLinkCounter;
	for (var nLinkCounter = 0; nLinkCounter < document.links.length; nLinkCounter++)
	{
		document.links[nLinkCounter].onclick = doDisableLinks();
	}
}

function doDisableLinks()
{
	return false;
}

function activeNav()
{
	var currentPageURLFullPath = window.location.href;

	var navContainer = document.getElementById("nav");
		
	if (document.all)
	{
		var navLinks = navContainer.all.tags('a');
	}
	else if (document.getElementsByTagName)
	{
		var navLinks = navContainer.getElementsByTagName('a');
	}

	for (var i = 0; i < navLinks.length; i++)
	{
		var navID = navLinks[i].id;		
		var navLink = navLinks[i];
		
		if(navLink == currentPageURLFullPath)
		{
			if(navID != "") document.getElementById(navID).style.color = "#F26724";
		}
	}
}

function validateContactForm()
{
	if (!validatePresence(document.getElementById("FromName"), "Please enter your name.")) return false;	
	if (!validatePresence(document.getElementById("FromEmail"), "Please enter your email adrress.")) return false;
	if (!validateEmail(document.getElementById("FromEmail"), "Please enter a valid email adrress.", false)) return false;
	return true;
}

function addItemToCartFromCategoryPage(itemID)
{
	if (validateItemQuantity(itemID))
	{
		document.getElementById("ItemID").value = itemID;
		document.frmItemPurchaseOnCategoryPage.action =  "cart.asp?ItemID=" + itemID;
		document.frmItemPurchaseOnCategoryPage.submit();
	}
}

//validate item quantity for category page
function validateItemQuantity(itemID)
{
	var oQty = document.getElementById('Quantity' + itemID);
	var oQtyLeft = document.getElementById('QuantityLeft' + itemID);
	var oQtyRight = document.getElementById('QuantityRight' + itemID);
	
	if (oQty)
	{
		if (!validatePresenceNonNegNumberAndBiggerThanZero(oQty, "Please enter a number greated than zero for the quantity."))	return false;
	}
	
	if (oQtyLeft)
	{
		if (!validatePresenceNonNegNumber(oQtyLeft, "Please enter a number for the quantity.")) return false;
	}
	
	if (oQtyRight)
	{
		if (!validatePresenceNonNegNumber(oQtyRight, "Please enter a number for the quantity.")) return false;
	}
	
	return true;
}

function validatePresenceNonNegNumberAndBiggerThanZero(obj, msg)
{
 if (jsTrim(obj.value) == '')
 {
  alert(msg);
  obj.focus();
  return false;
 }
 if (!isFinite(obj.value) || obj.value <= 0)
 {
  alert(msg);
  obj.focus();
  return false;
 }
 return true;
}
