﻿// ********************************************************************************************************
function TabtoEditor(e) {
	var key = "";
	
  if (document.all) {
    key = e.keyCode;
    src = e.srcElement.tagName;
    shift = e.shiftKey;
    ctrl = e.ctrlKey;
  } else {
    key = e.keyCode;
    src = e.target.tagName;
    shift = e.modifiers & Event.SHIFT_MASK;
    ctrl = e.modifiers & Event.CONTROL_MASK;
  }

	if (key==9 && !shift) {
		if (getId(ControlID + "htmleditorsource")) {
			var oEditor = FCKeditorAPI.GetInstance(ControlID + "htmleditorsource");
			oEditor.Focus();
		}
		return false;
	}
	return true;
}

function FocusTextarea(input, width, height)
{
  input.style.width = width + "px";
  input.style.height = height + "px";
}

function BlurTextarea(input)
{
  input.style.width = "";
  input.style.height = "";
}

function SaveLists()
{
	coll = document.getElementsByTagName("SELECT");
	for (i = 0; i < coll.length; i++) {
	  coll[i].SV = coll[i].value;
	}
}

function RestoreLists()
{
	coll = document.getElementsByTagName("SELECT");
	for (i = 0; i < coll.length; i++) {
	  coll[i].value = coll[i].SV;
	}
}

// ********************************************************************************************************
// Input validation

function IsValidDate(datestring) {
	// Validate a date string
	var mm, dd, yyyy;	// for parsing out date pieces
	var maxdays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);	// max days in each month
	var dateparts = datestring.split("/");	// split the string into date parts
	if (dateparts.length!=3) return false;	// if date doesn't have 3 parts, return.
	// get each part from array
	mm = dateparts[0];
	dd = dateparts[1];
	yyyy = dateparts[2];
	// make sure each part is the right length
	if (mm.length<1 || mm.length>2) return false;
	if (dd.length<1 || dd.length>2) return false;
	if (yyyy.length!=4) return false;
	// make sure each part is a number
	if (isNaN(parseInt(mm))) return false;
	if (isNaN(parseInt(dd))) return false;
	if (isNaN(parseInt(yyyy))) return false;
	// convert each part to an integer
	mm = parseInt(mm);
	dd = parseInt(dd);
	yyyy = parseInt(yyyy);
	// make sure each part falls within a valid range
	if (mm<1 || mm>12) return false;  // 12 months in a year
	if (dd<1 || dd>((yyyy%4==0 && mm==2)?29:maxdays[mm-1])) return false;  // check days against maxdays array, force 29 for february+leapyear
	if (yyyy<1900 || yyyy>2100) return false;  // arbitrary year range, may need to be changed.
	return true;
}

function IsValidEmail(address) {
  return address.match(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/g);
}

function ValidPhone(phone) {
  return phone.match(/^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/g);
}

function ValidSSN(ssn) {
  return ssn.match(/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/g);
}

// ********************************************************************************************************
// Conversions

function parseCSSRGB(rgb) {
  if (rgb.substr(0,3)=="rgb") {
    rgb = rgb.replace(/[^0-9,]/g,'');
    var parse = rgb.split(",");
    R = parse[0];
    G = parse[1];
    B = parse[2];
		R = ((parseInt(R/16)).toString(16) + (R%16).toString(16)).toUpperCase();
		G = ((parseInt(G/16)).toString(16) + (G%16).toString(16)).toUpperCase();
		B = ((parseInt(B/16)).toString(16) + (B%16).toString(16)).toUpperCase();
    return R+G+B;
  }
  if (rgb.substr(0,1)=="#") {
    return rgb.substr(1,6).toUpperCase();
  }
  return "FFFFFF";
}

function HextoInt(ch) {
	// Convert hex characters to integer values
	switch (ch.toLowerCase()) {
		case "f" :
			return 15; break;
		case "e" :
			return 14; break;
		case "d" :
			return 13; break;
		case "c" :
			return 12; break;
		case "b" :
			return 11; break;
		case "a" :
			return 10; break;
		default:
			return parseInt(ch);
	}
}

function HextoBase100(hex) {
	// Convert a hex value to base 100
	num1 = HextoInt(hex.substr(0,1));
	num2 = HextoInt(hex.substr(1,1));
	return ((num1*16) + num2);
}

// ********************************************************************************************************
// Misc.

function CharCount(field, label) {
  getId(label).innerHTML = field.value.length;
}

function CleanWhitespace(node)
{
  var notWhitespace = /\S/;
  for (var x = 0; x < node.childNodes.length; x++)
  {
    var childNode = node.childNodes[x];
    if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue)))
    {
      node.removeChild(node.childNodes[x]);
      x--;
    }
    if (childNode.nodeType == 1) CleanWhitespace(childNode);
  }
}