﻿
function ValidatorReset(valId) {
	var val = document.getElementById(valId);	
	val.isvalid = true;	
	ValidatorUpdateDisplay(val);
}



// Enhanced number formatting

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
  if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}


// Menu helper function

function menuClick(header, groupID) {
  var group = document.getElementById(groupID);  
  var show = (group.className == 'hide');
  if(header != null) {
    header.innerHTML = header.innerHTML.replace(/[\+\-]/, (show?'-':'+'));
  }
  group.className = show ? 'show' : 'hide';
  return false;
}



// Helpers for RadSpell spellchecking multiple fields

function MultipleTextSource(sources) {
	this.sources = sources;         
  this.getText = function()
  {
		var texts = [];
    for (var i = 0; i < this.sources.length; i++) {
			texts[texts.length] = this.sources[i].getText();
		}
    return texts.join("<controlSeparator><hr/></controlSeparator>");
	}         
	this.setText = function(text)
	{
		var texts = text.split("<controlSeparator><hr/></controlSeparator>");
		for (var i = 0; i < this.sources.length; i++) {
			this.sources[i].setText(texts[i]);
		}
	}
}

function RadEditorSource(editor) {
	this.editor = editor;	    
	this.getText = function()
	{
		return this.editor.GetHtml();
	}	       
	this.setText = function(text)
	{
		this.editor.SetHtml(text);
	}
}  

function TextAreaSource(textArea) {
	this.textArea = textArea;
	this.getText = function() 
	{
		return this.textArea.value;
	}         
	this.setText = function(text)
	{
		this.textArea.value = text;
	}
}

function RadEditorExtendContentArea(editor, by)
{
    if (editor.InitialHeight == -1) {
        editor.InitialHeight = editor.Document.body.clientHeight;
    }
    var currentHeight = editor.Document.body.scrollHeight;
    var newHeight = parseInt(currentHeight) + parseInt(by);
    if(newHeight < editor.InitialHeight) {
      newHeight = editor.InitialHeight;
    }
    var theIFrame = document.getElementById("RadEContentIframe" + editor.Id);
    theIFrame.style.height = newHeight.toString() + "px";
}

function RadEditorResizeContentArea(editor)
{
    if (editor.InitialHeight == -1)
    {
        editor.InitialHeight = editor.Document.body.clientHeight;
    }
    var targetHeight = editor.Document.body.scrollHeight;
    if (targetHeight > editor.InitialHeight)
    {
        var theIFrame = document.getElementById("RadEContentIframe" + editor.Id);
        theIFrame.style.height = parseInt(targetHeight) + "px";
    }
}

function RadEditorAttachResizeEvents(editor)
{
    editor.InitialHeight = -1;
    editor.GetContentArea().style.overflow = "hidden";
    editor.Document.body.scroll = "no";
    var resizeFnRef = function anon(){RadEditorResizeContentArea(editor)};
    editor.AttachEventHandler("RADEVENT_SEL_CHANGED", resizeFnRef)
    editor.AttachEventHandler("keydown", resizeFnRef)
}








// counting up/down the max length of a textarea

function TextCounter(maxLen, textId, labelId, down) 
{
	this.MaxLen = maxLen;
	this.TextBox = document.getElementById(textId);
	if(labelId)
		this.Label = document.getElementById(labelId);
	else
		this.Label = null;
	
	this.TextBox.Counter = this;
	this.TextBox.onkeyup = TextCounter_KeyUp;

	this.Update = function()
	{
		var val = this.TextBox.value;
		var m = val.match(/^\s*(\S+(\s+\S+)*)\s*$/);
		val = (m == null) ? "" : m[1];
		if(down != false) {
			var len = this.MaxLen - val.length;
			if(len < 0) {
				len = 0;
				this.TextBox.value = val.substring(0, this.MaxLen);
			}
			if(this.Label)
				this.Label.innerHTML = len.toString();
		} else {
			if(this.MaxLen != 0) {
				var len = this.MaxLen - val.length;
				if(len < 0) {
					val = val.substring(0, this.MaxLen);
					this.TextBox.value = val;
				}
			}
			if(this.Label) 
				this.Label.innerHTML = val.length.toString();
		}		
	}
			
	this.Update();
}

function TextCounter_KeyUp()
{
	this.Counter.Update();
}





// Cookie management of selected items

function GetSelectedArray(cookieName)
{
  var current_array = new Array();
  var current = getCookie(cookieName);
  if((current)&&(current.length > 0)) {
    current_array = current.split(",");
  }
  return current_array;
}

function CookieSelect(cookieName, selected, id) 
{
  var current_array = GetSelectedArray(cookieName);
  var new_array = new Array();
  for(var i = 0; i < current_array.length; i++) {
    if((current_array[i].toString() != id) && (current_array[i].toString().length > 0)) {
      new_array.push(current_array[i]);
    }
  }
  if(selected) {
    new_array.push(id);
  }
	setCookie(cookieName, new_array.toString(), undefined, "/");
}

function CountCookieSelect(cookieName)
{
	var current_array = GetSelectedArray(cookieName);
	return current_array.length;
}

function CheckCookieSelect(cookieName, entity)
{
	var currentCount = CountCookieSelect(cookieName);
	if(currentCount == 0)
		window.alert("You have not selected any " + entity);
	return (currentCount != 0);
}




/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie (defaults to end of current session)
   [path] - path for which the cookie is valid (defaults to path of calling document)
   [domain] - domain for which the cookie is valid (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to create cookie)
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}





// Help Manager - TODO: lose this!

function HelpManager()
{
  this.pageHelp = "";
  this.fieldHelpList = new Object();
  this.fieldHelpList.Length = 0;

  this.RegisterPage = function(text)
  {
    this.pageHelp = text;
  }

  this.RegisterField = function(field, text)
  {
    var fieldHelp = new Object();
    fieldHelp.Field = field;
    fieldHelp.Text = text;
    this.fieldHelpList[this.fieldHelpList.Length++] = fieldHelp;  
  }

	this.Show = function(field) 
	{
	  var help = document.getElementById('help');
	  if(help) {
	    if((typeof(field) == 'undefined')||(field == null)) {
  	    help.innerHTML = this.pageHelp;
  	  } else {
  	    for(var i = 0; i < this.fieldHelpList.Length; i++) {
  	      if(this.fieldHelpList[i].Field == field) {
  	        help.innerHTML = this.fieldHelpList[i].Text;
  	        break;
  	      }  	  
  	    }  	
  	  }  	
  	}
	}
}

var helpManager = new HelpManager();
