/*
	#####################################################
	################ COPYRIGHT INFORMATION ##############
	#####################################################
	This file is developed and maintained for to be use
	in accordance by HarvestThe.Net. Any modification or
	reproduction, without the consent of HarvestThe.Net,
	will result breach of copyright and person or others
	involved or responsible for such act, may be brought 
	before court for breaching copyright law.
	##################################################### 
*/




function IsDecimal(sText)
{
	var regex = /^-?\d+(\.\d+)?$/
	return regex.test(sText);
}
function IsPositiveDecimal(sText)
{
	var regex = /^\d+(\.\d+)?$/
	return regex.test(sText);
}
function IsInteger(sText)
{
	var regex = /^-?\d+$/; // ^\d+$ any string containing one or more digits
	return regex.test(sText);
}
function IsPositiveInteger(sText)
{
	var regex = /^\d+$/; // ^\d+$ any string containing one or more digits
	return regex.test(sText);
}
function IsFloat(sText)
{
	return sText.length>0 && !(/[^0-9.]/).test(sText) && (/\.\d/).test(sText);
}
function IsPositiveFloat(sText)
{
	if (IsFloat(sText) || IsInteger(sText))
		if (Number(sText) >= 0)
			return true;
	return false;
}

function isURL(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("http://") == -1)
    return false;
  else if (argvalue == "http://")
    return false;
  else if (argvalue.indexOf("http://") > 0)
    return false;

  argvalue = argvalue.substring(7, argvalue.length);
  if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length - 1) == ".")
    return false;

  if (argvalue.indexOf("/") != -1) {
    argvalue = argvalue.substring(0, argvalue.indexOf("/"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  if (argvalue.indexOf(":") != -1) {
    if (argvalue.indexOf(":") == (argvalue.length - 1))
      return false;
    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
      return false;
    argvalue = argvalue.substring(0, argvalue.indexOf(":"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }
  return true;
}

function validateText(fieldId,message)
{
	// ensures that something is entered into a text field
	if (isWhitespace(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}

function validateMandatoryEmail(fieldId,message)
{
	// ensures that an email address is entered into a text field
	if (!isEmail(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateOptionalEmail(fieldId,message)
{
	// ensures that nothing or an email address is entered into a text field
	if (!isWhitespace(document.getElementById(fieldId).value))
	{
		return validateMandatoryEmail(fieldId,message)
	}
	return true;
}
function validateMandatoryInteger(fieldId,message)
{
	// ensures that an integer is entered into a text field
	if (document.getElementById(fieldId).value == "")
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	if (!IsInteger(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	// TO DO - finish this!!
	return true;
}
function validateOptionalInteger(fieldId,message)
{
	// ensures that nothing or an integer is entered into a text field
	if (document.getElementById(fieldId).value == "")
		return true
	if (!IsPositiveInteger(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateMandatoryPositiveInteger(fieldId,message)
{
	// ensures that an integer >=0 is entered into a text field
	if (document.getElementById(fieldId).value == "")
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	if (!IsPositiveInteger(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	// TO DO - finish this!!
	return true;
}
function validateMandatoryPositiveIntegerGreaterThanZero(fieldId,message)
{
	// ensures that an integer >0 is entered into a text field
	if (document.getElementById(fieldId).value == "")
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	if (!IsPositiveInteger(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	if (document.getElementById(fieldId).value == 0)
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateOptionalPositiveInteger(fieldId,message)
{
	// ensures that nothing or an integer >=0 is entered into a text field
	if (document.getElementById(fieldId).value == "")
		return true;
	
	if (!IsPositiveInteger(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateMandatoryPositiveDecimal(fieldId,message)
{
	// ensures that a decimal >=0 is entered into a text field
	if (document.getElementById(fieldId).value == "")
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	
	if (!IsPositiveDecimal(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateOptionalPositiveDecimal(fieldId,message)
{
	// ensures that nothing or a decimal >=0 is entered into a text field
	if (document.getElementById(fieldId).value == "")
		return true;
		
	if (!IsPositiveDecimal(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateMandatoryPositiveDecimalGreaterThanZero(fieldId,message)
{
	// ensures that a decimal >=0 is entered into a text field
	if (document.getElementById(fieldId).value == "")
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	
	if (!IsPositiveDecimal(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	
	if (document.getElementById(fieldId).value == 0)
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}

	return true;
}
function validateMandatoryDate(fieldId,message)
{
	// ensures that a date is entered into a text field
	if (!isDateF(document.getElementById(fieldId).value,'d/M/yyyy'))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateMandatoryDateGreaterThanToday(fieldId,message)
{
	// ensures that a date is entered into a text field
	if (!isDateF(document.getElementById(fieldId).value,'d/M/yyyy'))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	var today = new Date()
	if (compareDates(formatDate(today,'d/M/yyyy'),'d/M/yyyy',document.getElementById(fieldId).value,'d/M/yyyy')!=0)
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}

	return true;
}
function validateDateEarlierThan(fieldEarlyId,fieldLateId,message)
{
	// ensures that the date entered into text field fieldEarlyId is the same as or earlier than the one entered into fieldLateId
	if (compareDates(document.getElementById(fieldEarlyId).value,'d/M/yyyy',document.getElementById(fieldLateId).value,'d/M/yyyy')!=0)
	{
		alert(message);
		document.getElementById(fieldEarlyId).select();
		document.getElementById(fieldEarlyId).focus();
		return false;
	}
	return true;
}
function validateOptionalDate(fieldId,message)
{
	// ensures that a date is entered into a text field
	if (document.getElementById(fieldId).value == "")
		return true;
	if (!isDateF(document.getElementById(fieldId).value,'d/M/yyyy'))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateSelect(fieldId,message,defaultValue)
{
	// ensures that the default value in a select field isn't selected
	if (document.getElementById(fieldId).options[document.getElementById(fieldId).selectedIndex].value == defaultValue)
	{
		alert(message);
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateUrl(fieldId,message)
{
	// ensures that a URL is entered into a text field
	if (document.getElementById(fieldId).value == "" || !isURL(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateEditor(fieldId,message)
{
	// ensures that something is entered into an editor control
	if (document.getElementById(fieldId))
		if (document.getElementById(fieldId).value)
			if (document.getElementById(fieldId).value == "")
			{
				alert(message);
				return false;
			}
	if (document.getElementById(fieldId))
		if(document.getElementById(fieldId).Document)
			if(document.getElementById(fieldId).Document.body)
				if(document.getElementById(fieldId).Document.body.innerHTML)
					if (document.getElementById(fieldId).Document.body.innerHTML == "")
					{
						alert(message);
						return false;
					}
	return true;
}
function validateMandatoryFloat(fieldId,message)
{
	// ensures that a positive float is entered into a text field
	if (document.getElementById(fieldId).value == "")
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	if (!IsPositiveFloat(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function validateOptionalFloat(fieldId,message)
{
	// ensures that nothing or a positive float is entered into a text field
	if (document.getElementById(fieldId).value == "")
		return true;
	if (!IsPositiveFloat(document.getElementById(fieldId).value))
	{
		alert(message);
		document.getElementById(fieldId).select();
		document.getElementById(fieldId).focus();
		return false;
	}
	return true;
}
function testdigit(s){
	for(i=0;i<s.length;i++ ) {
		var c = s.charAt(i);
		if(!(isDigit(c))){
			return false;
		}
	} 
	return true;
}

function getQueryStringValue(queryStr, queryVar)
{
	var str;
	var arrQueryVars = new Array();
	var thisVariable;
	
	if (queryStr.indexOf("&") > 0)
	{
		str = queryStr.split("&");
		
		if(str.length > 0)
		{
			for (i=0;i<str.length; i++)
			{
				thisVariable = new Object();
				var partStr = str[i].split("=");
				thisVariable.name = partStr[0];
				thisVariable.value = partStr[1];
				arrQueryVars.push(thisVariable);
			}
		}
	}
	else
	{
		thisVariable = new Object();
		var partStr = queryStr.split("=");
		thisVariable.name = partStr[0];
		thisVariable.value = partStr[1];
		arrQueryVars.push(thisVariable);
	}
	
	if (arrQueryVars != null || arrQueryVars.length > 0)
	{
		for (i =0;i<arrQueryVars.length;i++)
		{	thisVariable = arrQueryVars[i];
			if ( thisVariable.name == queryVar )
				return thisVariable.value;
		}
	}
	
	return "";
}

function replaceQueryStringValue(queryStr, queryVar, queryValue)
{
	
	var retStr = queryStr;
	var str;
	var arrQueryVars = new Array();
	var thisVariable;
	
	if (queryStr.indexOf("&") > 0)
	{
		str = queryStr.split("&");
		
		if(str.length > 0)
		{
			for (i=0;i<str.length; i++)
			{
				thisVariable = new Object();
				var partStr = str[i].split("=");
				thisVariable.name = partStr[0];
				thisVariable.value = partStr[1];
				arrQueryVars.push(thisVariable);
			}
		}
	}
	else
	{
		thisVariable = new Object();
		var partStr = queryStr.split("=");
		thisVariable.name = partStr[0];
		thisVariable.value = partStr[1];
		arrQueryVars.push(thisVariable);
	}
	
	if (arrQueryVars != null || arrQueryVars.length > 0)
	{
		retStr = "";
		for (i =0;i<arrQueryVars.length;i++)
		{
			thisVariable = arrQueryVars[i];
			if ( thisVariable.name == queryVar )
				thisVariable.value = queryValue;
			if (i==0)
				retStr += thisVariable.name + '=' + thisVariable.value;
			else
				retStr += '&' + thisVariable.name + '=' + thisVariable.value;
		}
	}
	return retStr;
}

function removeQueryStringVar(queryStr, queryVar)
{
	var retStr = queryStr;
	var str;
	var arrQueryVars = new Array();
	var thisVariable;
	
	if (queryStr.indexOf("&") > 0)
	{
		str = queryStr.split("&");
		
		if(str.length > 0)
		{
			for (i=0;i<str.length; i++)
			{
				thisVariable = new Object();
				var partStr = str[i].split("=");
				thisVariable.name = partStr[0];
				thisVariable.value = partStr[1];
				arrQueryVars.push(thisVariable);
			}
		}
	}
	else
	{
		thisVariable = new Object();
		var partStr = queryStr.split("=");
		thisVariable.name = partStr[0];
		thisVariable.value = partStr[1];
		arrQueryVars.push(thisVariable);
	}
	
	if (arrQueryVars != null || arrQueryVars.length > 0)
	{
		retStr = "";
		for (i =0;i<arrQueryVars.length;i++)
		{
			thisVariable = arrQueryVars[i];
			if ( thisVariable.name != queryVar )
			{
				if (retStr == "") 
					retStr += thisVariable.name + '=' + thisVariable.value;
				else
					retStr += '&' + thisVariable.name + '=' + thisVariable.value;
			}
		}
	}
	return retStr;
}

function convertToMoney(calculatedvalue)
{
	var input = calculatedvalue.toString();
	//alert(input);
	var convertedMoneyValue;
	if (input.length > 0)
	{
		var returnvalue = input.split(".");
		//debug - alert
		//alert(returnvalue[0]);
		//alert(returnvalue[1].length);
		if (returnvalue.length == 2)
		{
			//3345.56777
			convertedMoneyValue = returnvalue[0];						//3345
			convertedMoneyValue += ".";									//3345.
			if (returnvalue[1].length == 1)
				convertedMoneyValue += returnvalue[1] + "0";			//3345.50
			else if (returnvalue[1].length >= 2)	
				convertedMoneyValue += returnvalue[1].substring(0,2);	//3345.56
				
		}
		else
		{
			//220
			convertedMoneyValue = input + ".00";
		}
	}
	//debug - alert
	//alert ("Before: " + convertedMoneyValue);
	convertedMoneyValue = parseFloat(convertedMoneyValue);
	//debug - alert
	//alert ("after: " + convertedMoneyValue);
	return convertedMoneyValue; 
}