// FILE: 		scripts.js
// AUTHOR: 		Steve Ash, Jonathan Spilman
// DATE: 		7/25/2005
// TAG:			$Id: scripts.js,v 1.5 2008-05-10 05:46:03 steve Exp $
//
//		- contains various common js tasks
//

function showBigBooth() {
	var obj_wdw = window.open(
		'images/booth_layout.JPG', 'Booths', 
		'width=398,height=233,status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes'
	);
	obj_wdw.opener = window;
	obj_wdw.focus();
}

// formats a given text box entry (id) as HH:MM AM/PM
function formatTime(id) {
	var elem = document.getElementById(id);
	var str = elem.value;
	str = str.replace(/\s/g, '');
	str = str.toUpperCase();
	// format the hour
	if (str.length >= 5)	{
		var idx = str.indexOf(':');
		// format the hour
		if (idx == 1) {
			str = '0' + str;
		} 
		var idxAP = str.indexOf('A');
		if (idxAP < 0) {
			idxAP = str.indexOf('P');
		}
		// change a to am and p to pm
		if ((str.length - 1) == idxAP) {
			str = str + 'M';
		}
		idx = str.indexOf(':', 3);
		if (idx > 3) {
			// remove the seconds if they have a am/pm
			if (idxAP >= 0) {
				// remove the seconds in between the mins and the AMPM
				str = str.substring(0, idx) + str.substring(idxAP);
			} else {
				str = str.substring(0, idx);
			}
		}
		// readd the space before the A
		idxAP = str.indexOf('A');
		if (idxAP < 0) {
			idxAP = str.indexOf('P');
		}
		if (idxAP >= 0) {
			str = str.substring(0, idxAP) + ' ' + str.substr(idxAP, 2);
		}
		elem.value = str;
	}
}

/* given a text box this removes spaces, commas and dollar signs and formats for #0.00 */
function formatMoney(txtIn) {
	var tmp = txtIn.value.replace(/(\s|\$|,|-)/g, "");
	var idx = tmp.indexOf(".");
	var left = "0";
	var right = "00";
	if (idx >= 0) { // we have a decimal
		if (idx + 1 < tmp.length) { // we have at least one digit after decimal
			right = tmp.substr(idx + 1);
			// format right side for two digits
			if (right.length == 1) {
				right = right + "0";
			} else if (right.length > 2) {
				right = right.substr(0, 2);
			}
		}
		if (idx > 0) { // we have at least one digit to left
			left = tmp.substr(0, idx);
			// strip out leading zeros
			while ((left.length > 1) && (left.substr(0,1) == "0")) {
				left = left.substr(1);
			}
		}
	} else {
		left = tmp;
	}
	// construct new value
	txtIn.value = left + "." + right;
}

	// given two strings, date like 'YYYY-MM-DD' and time like 'HH:MM AMPM' creates a js 
	//	date obj
	function parseDate(inpDate, inpTime)	{
		var yr = inpDate.substr(0,4);
		var mo = inpDate.substr(5,2);
		var dy = inpDate.substr(8,2);
		var hr = inpTime.substr(0,2);
		var mn = inpTime.substr(3,2);
		var ampm = inpTime.substr(6,2);
		if (hr.substr(0,1) == '0') {
			hr = hr.substr(1,1);
		}
		if (hr == '12') {
			if (ampm == 'AM') {
				hr = 0;
			}
		} else if (ampm == 'PM') {
			hr = parseInt(hr) + 12;
		}
		mo = parseInt(mo) - 1;
		return new Date(yr, mo, dy, hr, mn, 0, 0);
	}

/**
*	Validates the given id_input (string id) as a date YYYY-MM-DD
*	the id_input_err is an element that will be populated with the error
*	message (if not passed in as null)
*/
function validateDateHelper(id_input, id_input_err) {
	var inDateElem = document.getElementById(id_input);
	var tmpErr = document.getElementById(id_input_err);
	if (!(checkDate(inDateElem))) {
		if (id_input_err !== null && tmpErr !== null) {
			tmpErr.innerHTML = "Date must be of the form YYYY-MM-DD";
		}
		inDateElem.select();
		return false;
	}
	// implicit else
	if (id_input_err !== null && tmpErr !== null) {
		tmpErr.innerHTML = '';
	}
	return true;
}


/*--------------------------
* Validate Date Field script- By JavaScriptKit.com
* For this script and 100s more, visit http://www.javascriptkit.com
* This notice must stay intact for usage
* Modified 2007-05-26 by Jonathan Spilman to handle dates in format YYYY-MM-DD
--------------------------- */

function checkDate(input){
	var validformat=/^\d{4}-\d{2}-\d{2}$/ //Basic check for format validity
	var returnval=false
	if (validformat.test(input.value)) {
		//Detailed check for valid date ranges
		var monthfield=input.value.split("-")[1]
		var dayfield=input.value.split("-")[2]
		var yearfield=input.value.split("-")[0]
		var dayobj = new Date(yearfield, monthfield-1, dayfield)
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
			returnval = false
		else
			returnval=true
	}
	// SA: don't always want to do this here (not this functions concern)
	//if (returnval==false) input.select()
	return returnval
}

/**
 * Validate that a given value is a number
 */
function isNumeric(x) {
	var RegExp = /^[-+]?\d*\.?\d*$/;
	var result = x.match(RegExp);
	if (result == null) result = false;
	return result;
}

/**
*	Validate that string is a time in the form HH:MM AM/PM
*/
function validateTimeHelper(id_src, id_src_err) {
	var inputElem = document.getElementById(id_src);
	var inputErr = document.getElementById(id_src_err);
	if (!(checkTime(inputElem)))	{
		// not a valid time
		if (id_src_err !== null && inputErr !== null) {
			inputErr.innerHTML = "Time must be of the form: HH:MM AM/PM";
		}
		inputElem.select();
		return false;
	}
	if (id_src_err !== null && inputErr !== null) {
		inputErr.innerHTML = '';
	}
	return true;
}

/**
*	Fucntions validates that a given input elements value is a 
*		time value like HH:MM AMPM
*/
function checkTime(input) {
	var validTime = /^(0\d|1[0-2]):[0-5][0-9]\s(AM|PM)$/;
	if (validTime.test(input.value))	{
		return true;
	}
	return false;
}




//Custom JavaScript Functions by Shawn Olson
//Copyright 2006
//http://www.shawnolson.net
//If you copy any functions from this page into your scripts, you must provide credit to Shawn Olson & http://www.shawnolson.net
//This file may not be used on adult sites
//or any site that incites hate
//or sites that are not child-friendly
//*******************************************
	function changecss(theClass,element,value) {
	//documentation for this script at http://www.shawnolson.net/a/503/
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	 for (var S = 0; S < document.styleSheets.length; S++){
	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    document.styleSheets[S][cssRules][R].style[element] = value;
	   }
	  }
	 }	
	}
// END script from http://www.shawnolson.net

	function isInteger (s)
    {
        var i;

        if (isEmpty(s))
             if (isInteger.arguments.length == 1) return 0;
             else return (isInteger.arguments[1] == true);

         for (i = 0; i < s.length; i++)
         {
              var c = s.charAt(i);

              if (!isDigit(c)) return false;
         }

         return true;
    }

    function isEmpty(s)
    {
        return ((s == null) || (s.length == 0))
    }

    function isDigit (c)
    {
        return ((c >= "0") && (c <= "9"))
    }

    function isSignedInteger (s)

    {   if (isEmpty(s))
             if (isSignedInteger.arguments.length == 1) return false;
             else return (isSignedInteger.arguments[1] == true);

         else {
              var startPos = 0;
              var secondArg = false;

              if (isSignedInteger.arguments.length > 1)
                    secondArg = isSignedInteger.arguments[1];

              // skip leading + or -
              if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
                  startPos = 1;
              return (isInteger(s.substring(startPos, s.length), secondArg))
         }
    }

    function isPositiveInteger (s)
    {   var secondArg = false;

         if (isPositiveInteger.arguments.length > 1)
              secondArg = isPositiveInteger.arguments[1];

         // The next line is a bit byzantine.  What it means is:
         // a) s must be a signed integer, AND
         // b) one of the following must be true:
         //    i)  s is empty and we are supposed to return true for
         //        empty strings
         //    ii) this is a positive, not negative, number

         return (isSignedInteger(s, secondArg)
                && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
    }
	
	function isNonnegativeInteger (s) {
		var secondArg = false;

         if (isNonnegativeInteger.arguments.length > 1)
              secondArg = isNonnegativeInteger.arguments[1];

         // The next line is a bit byzantine.  What it means is:
         // a) s must be a signed integer, AND
         // b) one of the following must be true:
         //    i)  s is empty and we are supposed to return true for
         //        empty strings
         //    ii) this is a number >= 0

         return (isSignedInteger(s, secondArg)
                && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
	}
	
	// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
   function isIntegerInRange (s, a, b)
   {   if (isEmpty(s))
         if (isIntegerInRange.arguments.length == 1) return false;
         else return (isIntegerInRange.arguments[1] == true);

      // Catch non-integer strings to avoid creating a NaN below,
      // which isn't available on JavaScript 1.0 for Windows.
      if (!isInteger(s, false)) return false;

      // Now, explicitly change the type to integer via parseInt
      // so that the comparison code below will work both on
      // JavaScript 1.2 (which typechecks in equality comparisons)
      // and JavaScript 1.1 and before (which doesn't).
      var num = parseInt (s);
      return ((num >= a) && (num <= b));
   }
   
   function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
function rteSafe($strText) {
    //returns safe code for preloading in the RTE
    $tmpString = $strText;
    
    //convert all types of single quotes
    $tmpString = str_replace(chr(145), chr(39), $tmpString);
    $tmpString = str_replace(chr(146), chr(39), $tmpString);
    $tmpString = str_replace("'", "&#39;", $tmpString);
    
    //convert all types of double quotes
    $tmpString = str_replace(chr(147), chr(34), $tmpString);
    $tmpString = str_replace(chr(148), chr(34), $tmpString);
//  $tmpString = str_replace("\"", "\"", $tmpString);
    
    //replace carriage returns & line feeds
    $tmpString = str_replace(chr(10), " ", $tmpString);
    $tmpString = str_replace(chr(13), " ", $tmpString);
    
    return $tmpString;
}
    