Utility = function(){}
if (typeof(Aprimo) != "undefined")
	Aprimo.Namespace.Add("Aprimo.Util", "Utility");
	
Utility.ToInt = function(ValueToConvert)
{
	if (Utility.IsNull(ValueToConvert) || trim(Utility.ToString(ValueToConvert)).length == 0 || isNaN(ValueToConvert))
		return 0;
	else
		return parseInt(ValueToConvert);
}

Utility.ToString = function(ValueToConvert)
{
	if (Utility.IsNull(ValueToConvert))
		return "";
	else
		return ValueToConvert + "";
}

Utility.ToFloat = function(ValueToConvert)
{
	if (trim(ValueToConvert).length == 0 || isNaN(ValueToConvert))
		return parseFloat(0);
	else
		return parseFloat(ValueToConvert);
}

Utility.IsEmpty = function(ValueToCheck)
{
	if(typeof(ValueToCheck) == "string")
		if(String(ValueToCheck).length==0)return true;
	else
		return false
	
}

Utility.GetQueryStringValue = function(name)
{
  var loc = window.location.search.substring(1);
  var search = name + "=([^&^#]*)";
  var re = new RegExp(search);
  var matches = re.exec( loc );

  return (matches ? unescape(matches[1]) : "");
}

Utility.IsNull = function(ValueToCheck)
{
	if (ValueToCheck == null || typeof(ValueToCheck) == "undefined")
		return true;
	else
		return false;
}

Utility.GetEventObject = function(FunctionArguments)
{
	// If we have the event readily accessible from the window or the document (IE / Safari), use those.
	// Otherwise, we need to look through the function arguments collection 
	// and try to find the event object (Firefox).
	if (window.event)
		return window.event;
	else if (document.event)
		return document.event;
	else
	{
		for (var i = 0; i < FunctionArguments.length; i++)
		{
			var fArg = FunctionArguments[i];
			if (fArg)
			{
				if (!Utility.IsNull(fArg.eventType) || !Utility.IsNull(fArg.stopPropagation) || !Utility.IsNull(fArg.cancelBubble))
					return fArg;
			}
		}
	}
	
	return null;
}


Utility.Evaluate = function(evalfunction)
{
	try
	{	
		return eval(this.Unencode(evalfunction));
	}
	catch(ex)
	{
		return evalfunction;
	}
	
	return "";
}

Utility.Encode = function (val) {
	val = val.replace(/\'/g, "&#39;");
	val = val.replace(/\"/g, "&#34;");
	val = val.replace(/\</g, "&#60;");
	val = val.replace(/\>/g, "&#62;");
	val = val.replace(/\[/g, "&#91;");
	val = val.replace(/\]/g, "&#93;");
	return val;
}
Utility.Unencode = function(encodedstring)
{
	encodedstring = encodedstring.replace(/&#39;/g, "'");
	encodedstring = encodedstring.replace(/&#34;/g, '"');
	encodedstring = encodedstring.replace(/&#60;/g, "<");
	encodedstring = encodedstring.replace(/&#62;/g, ">");
	encodedstring = encodedstring.replace(/&#93;/g, "]");
	encodedstring = encodedstring.replace(/&#91;/g, "[");
	return encodedstring;
}

Utility.FormatDate = function(val)
{
	var d = new Date();
	
	var formattedDate = "";
	
	if (!isNaN(Date.parse(val)))
	{
		d.setTime(Date.parse(val));	
		var formattedDate = userDateFormat;
		var dd = d.getDate();if (dd < 10) dd = "0" + dd;
		var mm = d.getMonth() + 1;if (mm < 10) mm = "0" + mm;
		var yy = d.getFullYear();
		
		formattedDate = formattedDate.replace("DD", dd);
		formattedDate = formattedDate.replace("MM", mm);
		formattedDate = formattedDate.replace("YYYY", yy);
	}	
	
	return formattedDate
}

Utility.FormatTime = function(val)
{
	var d = new Date();
	
	var formattedTime = "";
	if (!isNaN(Date.parse(val)))
	{
		d.setTime(Date.parse(val));	
		formattedTime = userTimeFormat;
		var ap = "AM";
		var HH = d.getHours();
		var hh;
		
		if (HH > 11)
			ap = "PM";
		
		if (HH > 12)
			hh = HH - 12;
		else if (HH == 0)
			hh = 12;
		else
			hh = HH;
			
		if (hh < 10) hh = "0" + hh;
		if (HH < 10) HH = "0" + HH;
		var mm = d.getMinutes();if (mm < 10) mm = "0" + mm;
		var ss = d.getSeconds();if (ss < 10) ss = "0" + ss;
		
		if (formattedTime.indexOf('TT') > 0)
			formattedTime = formattedTime.replace("HH", hh);
		else
			formattedTime = formattedTime.replace("HH", HH);
		
		formattedTime = formattedTime.replace("MM", mm);
		formattedTime = formattedTime.replace("SS", ss);
		formattedTime = formattedTime.replace("TT", ap);
	}	
	
	return formattedTime;
}

Utility.FormatDateTime = function(val)
{
	var d = new Date();
	
	var formattedDate = "";
	var formattedTime = "";
	if (!isNaN(Date.parse(val)))
	{
		d.setTime(Date.parse(val));	
		formattedDate = userDateFormat;
		var dd = d.getDate();if (dd < 10) dd = "0" + dd;
		var mm = d.getMonth() + 1;if (mm < 10) mm = "0" + mm;
		var yy = d.getFullYear();
		
		formattedDate = formattedDate.replace("DD", dd);
		formattedDate = formattedDate.replace("MM", mm);
		formattedDate = formattedDate.replace("YYYY", yy);
		
		formattedTime = userTimeFormat;
		var ap = "AM";
		var HH = d.getHours();
		var hh;
		
		if (HH > 11)
			ap = "PM";
		
		if (HH > 12)
			hh = HH - 12;
		else if (HH == 0)
			hh = 12;
		else
			hh = HH;
			
		if (hh < 10) hh = "0" + hh;
		if (HH < 10) HH = "0" + HH;
		var mm = d.getMinutes();if (mm < 10) mm = "0" + mm;
		var ss = d.getSeconds();if (ss < 10) ss = "0" + ss;
		
		if (formattedTime.indexOf('TT') > 0)
			formattedTime = formattedTime.replace("HH", hh);
		else
			formattedTime = formattedTime.replace("HH", HH);
		
		formattedTime = formattedTime.replace("MM", mm);
		formattedTime = formattedTime.replace("SS", ss);
		formattedTime = formattedTime.replace("TT", ap);
		
	}
	
	return formattedDate + " " + formattedTime;
}

Utility.Clone = function(objToClone, jsType)
{	
	var clonedObj = null;
	if (Utility.IsNull(objToClone.clone))
	{
		// If we're dealing with an object, look for some ways to deep copy the object.
		// If we're dealing with a simple data type, just return the original value.
		if (typeof(objToClone) == "object")
		{
			if (objToClone.createClonedInstance)
				clonedObj = objToClone.createClonedInstance();
			else if (jsType)
				clonedObj = eval("new " + jsType + "();");

			Utility.ObjectCopy(objToClone, clonedObj);
		}
		else
			return objToClone;
	}
	else
		clonedObj = objToClone.clone(null, null);
	
	return clonedObj;
}

Utility.ObjectCopy = function(fromObject, toObject)
{
	for (var i in fromObject)
	{
		if (i != "owner" && !Utility.IsNull(fromObject[i]))
		{
			if (Utility.IsNull(fromObject[i].clone))
			{
				if (typeof(fromObject[i]) == "object" && toObject[i] != null)
					toObject[i] = Utility.ObjectCopy(fromObject[i], toObject[i].createClonedInstance());					
				else
					toObject[i] = fromObject[i];
			}
			else
				toObject[i] = fromObject[i].clone(null, toObject);
		}
	}
}

Utility.IsValidHexValue = function(input)
{
	var strPattern = /^#([0-9a-f]{1,2}){3}$/i;
	if(strPattern.test(input.value) == false || input.value.length<7)
		return false;
	else
		return true;
}

