/**
 * NSCash Content Management System
 *
 * LICENSE: This sofware and all its components are licensed exclusively to NewSensations Inc.
 *          Unauthorized use and/or reproduction is subject to full prosecution under the laws
 *          of the Digital Millenium Copyright Act and related federal statutes under the
 *          jurisdiction of the United States of America and the State of California.
 *
 * @package   NSCashCMS
 * @author    Jim Gillispie <jim@dragonscripts.com>
 * @copyright DragonScripts Inc.
 *
 */
// --- COMMON JAVASCRIPT FUNCTIONS --- //

var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion);
var isIE  = sUserAgent.indexOf("compatible") > -1
            && sUserAgent.indexOf("MSIE") > -1
            && !sUserAgent.indexOf("Opera") > -1;
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");


var EventUtil = new Object;

EventUtil.addEventHandler = function ( oTarget, sEventType, fnHandler) {
  if ( oTarget.addEventListener) { // DOM compliant browsers
    oTarget.addEventListener(sEventType, fnHandler, false);
  } else if ( oTarget.attachEvent ) { // IE
    oTarget.attaqchEvent("on" + sEventType, fnHandler);
  } else { // others
    oTarget["on" + sEventType] = fnHandler;
  }
}

EventUtil.removeEventHandler = function ( oTarget, sEventType, fnHandler ) {
  if ( oTarget.remvoeEventListener ) {
    oTarget.removeEventListner(sEventType, fnHandler, false);
  } else if ( oTarget.detachEvent ) {
    oTarget.detachEvent("on" + sEventType, fnHandler);
  } else {
    oTarget["on" + sEventType] = null;
  }
}

EventUtil.formatEvent = function ( oEvent ) {
  if ( isIE && isWin) {
    oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
    oEvent.eventPhase = 2;
    oEvent.isChar = (oEvent.charCode > 0);
    oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
    oEvent.pageY = oEvent.clientY + document.body.scrollTop;
    oEvent.preventDefault = function () {
      this.returnValue = false;
    }

    if ( oEvent.type == "mouseout" ) {
      oEvent.relatedTarget = oEvent.toElement;
    } else if ( oEvent.type == "mouseover" ) {
      oEvent.relatedTarget = oEvent.fromElement;
    }

    oEvent.stopPropagation = function () {
      this.cancelBubble = true;
    }

    oEvent.target = oEvent.srcElement;
    oEvent.time = (new Date).getTime();
  }
  return oEvent;
}

EventUtil.getEvent = function () {
  if ( window.event) {
    return this.formatEvent(window.event);
  } else {
    return EventUtil.getEvent.caller.arguments[0];
  }
}


var FormUtil = new Object;

FormUtil.Validate = function (oForm) {

}


var TextUtil = new Object;

TextUtil.blockChars = function (oTextbox, oEvent) {
  oEvent = EventUtil.formatEvent(oEvent);
  var sInvalidChars = oTextbox.getAttribute("invalidchars");
  var sChar = String.fromCharCode(oEvent.charCode);
  var bIsValid = sInvalidChars.indexOf(sChar) == -1;
  return bIsValid || oEvent.ctrlKey;;
}

TextUtil.allowChars = function (oTextbox, oEvent) {
  oEvent = EventUtil.formatEvent(oEvent);
  var sValidChars = oTextbox.getAttribute("validchars");
  var sChar = String.fromCharCode(oEvent.charCode);
  var bIsValid = sValidChars.indexOf(sChar) > -1;
  return bIsValid || oEvent.ctrlKey;;
}

TextUtil.blurBlock = function (oTextbox) {
  var sValidChars = oTextbox.getAttribute("validchars");
  var arrValidChars = sValidChars.split("");
  for (var i=0; i < arrValidChars.length; i++) {
    var currChar = oTextbox.value.indexOf(arrValidChars[i]);
    if (currChar == -1) {
      alert("ERROR: Only Characters. '" + sValidChars + "' allowed.");
      oTextbox.focus();
      oTextbox.select();
      return;
    }
  }
}


function copyText(theText) {
	if(!document.all) return;
	theText.select()
	theText.execCommand('copy');
}

function checkAll( form_name ) {
  var checkbox;
	var cells = document.getElementById(form_name).getElementsByTagName('td');
	for ( var c = 0; c < cells.length; c++ ) {
		checkbox = cells[c].getElementsByTagName( 'input' )[0];
		if ( checkbox && checkbox.type == 'checkbox' ) {
			unique_id = checkbox.name + checkbox.value;
			if ( checkbox.disabled == false ) {
				checkbox.checked = true;
				deselectProduct("addMemberForm", checkbox.value);
			}
		}
	}
}

function unCheckAll( form_name ) {
	var checkbox;
	var cells = document.getElementById(form_name).getElementsByTagName('td');
	for ( var c = 0; c < cells.length; c++ ) {
		checkbox = cells[c].getElementsByTagName( 'input' )[0];
		if ( checkbox && checkbox.type == 'checkbox' ) {
			checkbox.checked = false;
		}
	}
}

function confirm_delete(msg) {
	input_box=confirm("OK to " + msg + ". Cancel to Abort.");
	if (input_box==true) {
		return (true);
	} else {
		return (false);
	}
}

function showHide ( div ) {
  var myDiv = document.getElementById(div);
  if ( myDiv ) {
    if ( myDiv.style.display == 'block' ) {
      myDiv.style.display = 'none';
    } else {
      myDiv.style.display = 'block';
    }
  }
}

function exPop( sEx ) {
  if ( sEx ) {
    window.open(sEx, 'exWin', 'width=950,height=800,resize=yes,toolbars=yes,status=yes,scrollbars=yes');
  }
}
// EOF //
