// JavaScript Document

//================================================
//|				Misc Stuff	
//================================================

// Input fill/clear
function checkClear(element,value){
	if(document.getElementById(element).value == value){
		document.getElementById(element).value = "";	
	}
}

function checkFill(element,value){
	if(document.getElementById(element).value == ""){
		document.getElementById(element).value = value;	
	}
}

// Hide/show element
function invis(element){
	document.getElementById(element).style.visibility = 'hidden';	
}

function vis(element){
	document.getElementById(element).style.visibility = 'visible';	
}

//================================================
//|				Regular Expressions
//================================================
function validEmail(email){
	var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	if (!emailRe.test(email)) {
		return (false);
	} else {
		return (true);
	}
}

function validPhone(phone){
	var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
	if (!phoneRe.test(phone)) {
		return (false);
	} else {
		return (true);
	}
}

function validZip(zip){
	var zipRe = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if (!zipRe.test(zip)) {
		return (false);
	} else {
		return (true);
	}
}

//================================================
//|				Get Element By Class
//================================================
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


//================================================
//|				  Cookie Control		
//================================================
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

//================================================
//|				String Manipulation
//================================================
// Converts first letter of a string to uppercase
function UCFirst(str){
   firstChar = str.substring(0,1);
   remainChar = str.substring(1);
   firstChar = firstChar.toUpperCase(); 
   remainChar = remainChar.toLowerCase();
   return firstChar + remainChar
}

// Remove HTML tags.
function removeHTML( strSrc ) {
	return server.HTMLEncode( strSrc.replace( /<[^<|>]+?>/gi,'' ) );
}

//================================================
//|				Change Input Type 
//================================================
// Converts first letter of a string to uppercase
// Example usage: changeInputType(document.getElementById('loginPass'),'text','Password',false,true);
// Example 2 (JS part 1)
function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var newElm = document.createElement('input');
  newElm.type = iType;
  if(oldElm.name) newElm.name = oldElm.name;
  if(oldElm.id) newElm.id = oldElm.id;
  if(oldElm.className) newElm.className = oldElm.className;
  if(oldElm.size) newElm.size = oldElm.size;
  if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
  if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
  newElm.onfocus = function(){return function(){
    if(this.hasFocus) return;
    var newElm = changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) newElm.hasFocus=true;
  }}();
  newElm.onblur = function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      changeInputType(this,'text',iValue,false,true);
    }
  }}();
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!blankValue) newElm.value = iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm = newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}
