function $(objId){return document.getElementById(objId)}

window.addListener = function(eventName,listener){
	if(eventName.length > 2 && eventName.substring(0,2) == 'on') eventName = eventName.substring(2,eventName.length);
	if (window.addEventListener) window.addEventListener(eventName, listener, false);
	else if(window.attachEvent) window.attachEvent('on' + eventName, listener);
}
window.addOnLoadListener = function(listener){window.addListener('onload', listener)};

function getViewportScrollX(){
  var scrollX = 0;
  if( document.documentElement && document.documentElement.scrollLeft) scrollX = document.documentElement.scrollLeft;
  else if(document.body && document.body.scrollLeft) scrollX = document.body.scrollLeft;
  else if(window.pageXOffset) scrollX = window.pageXOffset;
  else if(window.scrollX) scrollX = window.scrollX;
  return scrollX;
}

function getViewportScrollY(){
  var scrollY = 0;
  if(document.documentElement && document.documentElement.scrollTop) scrollY = document.documentElement.scrollTop;
  else if(document.body && document.body.scrollTop) scrollY = document.body.scrollTop;
  else if(window.pageYOffset) scrollY = window.pageYOffset;
  else if(window.scrollY) scrollY = window.scrollY;
  return scrollY;
}

var positionedDivs = [];
function positionDiv(thisObj,position){
    rePositionDiv(thisObj,position);
    positionedDivs[positionedDivs.length] = {o:thisObj, p:position};
	if(positionedDivs.length == 1){
		window.addListener('onresize',function(){for(var x1=0;x1<positionedDivs.length;x1++)rePositionDiv(positionedDivs[x1].o, positionedDivs[x1].p)});
		window.addListener('onscroll',function(){for(var x2=0;x2<positionedDivs.length;x2++)if(positionedDivs[x2].p.scroll != null && positionedDivs[x2].p.scroll == 0) rePositionDiv(positionedDivs[x2].o, positionedDivs[x2].p)});
	}
}

function rePositionDiv(thisObj,position){
    var X, Y, av, ax, ay, scroll
    X=0; Y=0; ax='L'; ay='T'; scroll=1
	
    if(position.anchor != null){
	    av = position.anchor.toUpperCase();
	    ax = av.substr(0,1);
	    ay = av.substr(1,1);
    }
	
	if(position.scroll != null && position.scroll == 0)scroll=0
	
	if(ax == 'C')
		if(scroll) X = (document.body.clientWidth / 2) - (thisObj.offsetWidth / 2);
		else X = getViewportScrollX() + (document.body.clientWidth / 2) - (thisObj.offsetWidth / 2);
	else if(scroll) X = 0;
		else X = getViewportScrollX();

    if(ay == 'C')
		if(scroll) Y = (document.body.clientHeight/2) - (thisObj.offsetHeight/2);
		else Y = getViewportScrollY() + (document.body.clientHeight/2) - (thisObj.offsetHeight/2);
	else if(scroll) Y = 0; 
		else Y = getViewportScrollY();

    if(position.offset != null){
    	if(ax == 'R' && !scroll) X -= position.offset[1];
		else X += position.offset[0];
		if(ay == 'B' && !scroll) Y -= position.offset[1];
		else Y += position.offset[1];
    }
	
	if(ax == 'R') thisObj.style.right = (0-X) + 'px'
	else thisObj.style.left = X + 'px'
	if(ay == 'B') thisObj.style.bottom = (0-Y) + 'px';
	else thisObj.style.top = Y + 'px';
}

function checkPageHeight(){	
	if (document.body && document.body.parentNode) {
		if (document.body.clientHeight && document.body.scrollHeight && document.body.scrollHeight > document.body.clientHeight) {
			if ($('scrollAlert')) {
				if (document.body.scrollHeight > (getViewportScrollY() + document.body.clientHeight)) {
					$('scrollAlert').style.visibility = 'visible';
				}
				else {
					$('scrollAlert').style.visibility = 'hidden';
				}
			}
			else {
				var statusDiv = document.createElement('div');
				var statusTextDiv = document.createElement('div');
				statusTextDiv.innerHTML = 'Scroll Down to See More';
				statusDiv.appendChild(statusTextDiv);
				statusDiv.id = 'scrollAlert'
				document.body.appendChild(statusDiv);
				positionDiv(statusDiv, {
					anchor: 'RB',
					scroll: 0
				});
				window.addListener('onscroll', checkPageHeight);
			}
		}
	}
}

function getPosition(element) {
    var r = {x: element.offsetLeft, y: element.offsetTop};
    if (element.offsetParent) {
        var tmp = getPosition(element.offsetParent);
        r.x += tmp.x;
        r.y += tmp.y;
    }
    return r;
};

function hideShowObj(thisObj,hideShow){
	var displayVal, hideShow, thisObj
	displayVal = 'visible';
	if(hideShow == '0') displayVal = 'hidden';
	else if(hideShow == 'hide') displayVal = 'hidden';
	else if(!hideShow) displayVal = (thisObj.style.visibility == 'visible')?'hidden':'visible';
	thisObj.style.visibility = displayVal;
}


function hiLite(){
	for (var a=arguments.length-1; a > -1; a--) {
		arguments[a].oldBG = arguments[a].style.backgroundColor;
		arguments[a].HLsiblings = arguments;
		arguments[a].onmouseout = function(){
			for (var a=this.HLsiblings.length-1; a > -1; a--) {
				this.HLsiblings[a].style.backgroundColor = this.HLsiblings[a].oldBG;
			}
		}
		arguments[a].onmouseover = function(){
			for (var a=this.HLsiblings.length-1; a > -1; a--) {
				this.HLsiblings[a].style.backgroundColor = "#FFA1A1";
			}
		}
		arguments[a].style.backgroundColor = "#FFA1A1";
	}
}

function mod10(cardNumber){ // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;
	for( i = 0; i < cardNumber.length; ++i ){
		ar[i] = parseInt(cardNumber.charAt(i));
	}
	for( i = ar.length -2; i >= 0; i-=2 ){ // you have to start from the right, and work back.
		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
	}										 // if the double digit is > 9, ADD those individual digits together 
	for( i = 0; i < ar.length; ++i ){
		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
	}
	return (((sum%10)==0)?true:false);	 	
}


function expired( month, year ) {
  	var now = new Date();							// this function is designed to be Y2K compliant.
    var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
    expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second of expired month
    if( now.getTime() < expiresIn.getTime() ) return false;
    return true;									// then we get the miliseconds, and do a long integer comparison
}


function validateCard(cardNumber,cardType,cardMonth,cardYear) {
	if( cardNumber.length == 0 ) {						//most of these checks are self explanitory
		alert("Please enter a card or check number.");
		return false;				
	}
	for( var i = 0; i < cardNumber.length; ++i ) {		// make sure the number is all digits.. (by design)
		var c = cardNumber.charAt(i);
    	if( c < '0' || c > '9' ) {
    		alert("Please enter a valid card number. Use only digits. do not use spaces or hyphens.");
    		return false;
    	}
    }
    var length = cardNumber.length;			//perform card specific length and prefix tests
    switch( cardType ) {
		case 'c':
			return true;
        case 'a':
        	if( length != 15 ) {
        		alert("Please enter a valid American Express Card number.");
        		return false;
        	}
        	var prefix = parseInt( cardNumber.substring(0,2));
   			if( prefix != 34 && prefix != 37 ) {
   				alert("Please enter a valid American Express Card number.");
   				return false;
   			}
   			break;
   		case 'd':
  			if( length != 16 ) {
   				alert("Please enter a valid Discover Card number.");
   				return false;
   			}
   			var prefix = parseInt( cardNumber.substring(0,4));
   			if( prefix != 6011 ) {
   				alert("Please enter a valid Discover Card number.");
   				return false;
   			}
   			break;
   		case 'm':
   			if( length != 16 ) {
   				alert("Please enter a valid MasterCard number.");
   				return false;
   			}
   			var prefix = parseInt( cardNumber.substring(0,2));
			if( prefix < 51 || prefix > 55) {
				alert("Please enter a valid MasterCard Card number.");
				return false;
			}
			break;
		case 'v':
   			if( length != 16 && length != 13 ) {
   				alert("Please enter a valid Visa Card number.");
   				return false;
   			}
   			var prefix = parseInt( cardNumber.substring(0,1));
   			if( prefix != 4 ) {
   				alert("Please enter a valid Visa Card number.");
   				return false;
   			}
   			break;
   	}
   	if( !mod10( cardNumber ) ) { 		// run the check digit algorithm
   		alert("Sorry! this is not a valid credit card number.");
   		return false;
   	}
   	if( expired( cardMonth, cardYear ) ) {							// check if entered date is already expired.
   		alert("Sorry! The expiration date you have entered would make this card invalid.");
   		return false;
   	}
   	document.form.Submitform.value = 'Please wait...';
	document.form.Submitform.disabled = true;
	document.form.Submitform.style.cursor = 'normal';
   	return true; // at this point card has not been proven to be invalid
}

function disablebutton(){
	document.form.Submitform.value = 'Please wait...';
	document.form.Submitform.disabled = true;
	document.form.Submitform.style.cursor = 'normal';
	return true;
}

function openwin(path,details,h,w){
	if(h==''||h==undefined) h = '650';
	if(w==''||w==undefined) w = '700';
	if(window.CommentsWin) window.CommentsWin.close()
	if(details==''||details==undefined) details = 'resizable=yes status=no, toolbar=no, directories=no, location=no, menubar=no, scrollbars=yes, width='+w+', height='+h
	CommentsWin = window.open(path,'',details);
}


if (window.XMLHttpRequest){
    // If IE7, Mozilla, Safari, etc: Use native object
    var objXml = new XMLHttpRequest()
}else if (window.ActiveXObject){  
    // ...otherwise, use the ActiveX control for IE5.x and IE6
    var objXml = new ActiveXObject("Microsoft.XMLHTTP");
}


function preview(objID,objecttype){
	elm=$("previewdiv")
	elm.style.visibility ='visible';
	elm.style.border="medium solid";
	elm.innerHTML = 'Loading... Please wait...';
	objXml.open("GET","/templates/object.cfm?format=html&objecttype="+objecttype+"&action=display&objID="+objID, true);
	objXml.onreadystatechange=function() {
	   if (objXml.readyState==4) {
		   	elm.innerHTML=objXml.responseText;
	  }
	 }
	objXml.send(null);
}

function closediv(){
	elm=$("previewdiv")
	elm.style.visibility ='hidden';
	elm.style.border="0px";
	elm.innerHTML='';
}

function tabOver(thisObj){
    if(thisObj.className == 'tab')
      thisObj.className = 'tabOver';
    else
      thisObj.className = 'tab';
      
    if(!thisObj.onmouseout)
      thisObj.onmouseout = function(){tabOver(this)};    
}

/*
function showhideselect(xhow) {
	var theElems = document.getElementsByTagName("select");
	for (i=0;i<theElems.length;i++) {
		theElems[i].style.visibility = xhow;
	}
}
*/

String.prototype.isNumeric = function() {
    var RegExp = /^[-+]?\d*\.?\d+(?:[-+]?\d+)?$/;
    var result = this.match(RegExp);
    return (result !== null);
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function saveClick(cName,cVal){
	objXml.open("GET","/templates/saveclick.cfm?g=" + cName + "&v=" + cVal + "&c=1&o=0", true);
	objXml.send(null);
	return true;
}


window.addListener('onload',checkPageHeight);