// ****************************************************************
// confirm_del_btn, Christian J. Clark 5/04
// pops up a confirmation box from a button
// Confirm delete on buttons
// ****************************************************************
function confirm_del_btn()
{
  var where_to = confirm("Are you sure you want to remove this record?")
  if (where_to == true)
  {
    return true;
  }
  else
  {
    return false;
  }
}

// ****************************************************************
// confirm_delete created by Dave Zyga, 2/04
// pops up confirmation box on links
// Confirm delete on links
// ****************************************************************
function confirm_delete(url)
{
	if (confirm("Are you sure you want to remove this record?"))
	{
		location.href = url;
	}
}

// ****************************************************************
// confirm_action
// pops up confirmation box on links
// ****************************************************************
function confirm_action(url, text)
{
	if (confirm(text))
	{
		location.href = url;
	}
}


// ****************************************************************
// Check to see if a value is numeric
// ****************************************************************
function IsNumeric(sText)
{
	var ValidChars = "0123456789.-";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

// ****************************************************************
// DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
// ****************************************************************
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) { this[i] = 30; }
		if (i==2) { this[i] = 29; }
   } 
   return this
}

function isDate(dtStr){
    if(dtStr == ""){ return true; }
    
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}
return true;
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus();
		return false;
	}
    return true;
}

//==============================================================================================
// Time Validation
//==============================================================================================
function IsValidTime(timeStr) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		alert("Time is not in a valid format.");
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23) {
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if (hour <= 12 && ampm == null) {
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
			alert("You must specify AM or PM.");
			return false;
   		}
	}
	if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	if (minute<0 || minute > 59) {
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		alert ("Second must be between 0 and 59.");
		return false;
	}
	return false;
}

//==============================================================================================
//==============================================================================================
// User Experience functions
//==============================================================================================
//==============================================================================================


// ****************************************************************
// Unhide Hidden RS List Rows
// ****************************************************************
function unhide(rs_id) {
	var tbl_element = document.getElementById(rs_id);
	var allNodes = tbl_element.getElementsByTagName('tr');
	var str = "";
	var reg = new RegExp("hidden");
	for (i = 0; i < allNodes.length; i++) {
		str = allNodes[i].getAttribute('class');
		if (reg.test(str)) {
			Element.show(allNodes[i]);
		}
		else if (allNodes[i].getAttribute('class') == "more_link") {
			var linkNodes = allNodes[i].getElementsByTagName('a');
			linkNodes[0].innerHTML = "( ...less )";
			linkNodes[0].href = "javascript:rehide('" + rs_id + "')";
		}
	}
}

// ****************************************************************
// Rehide Hidden RS List Rows
// ****************************************************************
function rehide(rs_id) {
	var tbl_element = document.getElementById(rs_id);
	var allNodes = tbl_element.getElementsByTagName('tr');
	var num_hidden = 0;
	var str = "";
	var reg = new RegExp("hidden");
	for (i = 0; i < allNodes.length; i++) {
		str = allNodes[i].getAttribute('class');
		if (reg.test(str)) {
			Element.hide(allNodes[i]);
			num_hidden++;
		}
		else if (allNodes[i].getAttribute('class') == "more_link") {
			var linkNodes = allNodes[i].getElementsByTagName('a');
			linkNodes[0].innerHTML = "( " + num_hidden + " more... )";
			linkNodes[0].href = "javascript:unhide('" + rs_id + "')";
		}
	}
}

// ****************************************************************
// onchange_goto, Jacob Romero 01/07
// modified from go() on http://www.quirksmode.org/js/select.html
// sets onchange action of passed name of select box
// to goto the value of the option.  
// Value must be a valid link to work.
// ****************************************************************
function onchange_goto(elName){
    var el = document.getElementsByName(elName);
    if(el[0]){
        el[0].onchange = function() {
           link = el[0].options[el[0].selectedIndex].value;
           if (link) {
               location.href = link;    
           }
        }
    }
}


// ****************************************************************
// new_window, Jacob Romero 01/07
// Open new window with href = link_path.
// ****************************************************************
function new_window(link_path)
{
    var wind = window.open(link_path, '');
}

// ****************************************************************
// new_window, Jacob Romero 01/07
// Returns true if users browser is safari
// ****************************************************************
function is_safari()
{
    version = window.navigator.userAgent;
    if (version.match("Safari")){ return true; }
    else { return false; }
}

// ****************************************************************
// new_window, Jacob Romero 01/07
// Returns true if users browser is Firefox
// ****************************************************************
function is_firefox()
{
    version = window.navigator.userAgent;
    if (version.match("Firefox")){ return true; }
    else { return false; }
}

//****************************************************************
// Trim Functions
//****************************************************************
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}



//************************************************************************
// Show and Hide the drop down menus
//************************************************************************
$( document ).ready ( function() {
    $("body").click(function () {
        $(".sub_nav").hide();
    });
    $("#nav_area ul li a.drop_menu").each(function () {
        $(this).click(function () {
            var a_name = $(this).attr("name");
            $(".sub_nav").each(function () {
                if($(this).siblings("a").attr("name") != a_name) {
                    $(this).hide();
                }
            });
            $(this).siblings("ul").toggle();
            var time_out;
            $(this).siblings("ul").mouseleave(function () {
                time_out = setTimeout(function () {
                    var display = $("a[name=" + a_name + "]").siblings("ul").css("display");
                    if(display == "block") {
                        $("a[name=" + a_name + "]").siblings("ul").hide();
                    }
                }, 900)
            });
            $(this).siblings("ul").mouseenter(function () {
                clearTimeout(time_out);
            });
            return false;
        });
    });
} );


// Open New Window
function open_win(url_add, width, height, menubar, status, location, toolbar, scrollbars) {
   window.open(url_add,'welcome','width=' + width + ',height=' + height +',menubar=' + menubar +',status=' + status +',location=' + location +',toolbar=' + toolbar +',scrollbars=' + scrollbars +'');
   return false;
}

//*****************************************************************
// Create an alert if member has IE 6 or IE 8
//*****************************************************************
//$( document ).ready ( function() {
//    if($.browser.msie && $.browser.version == "6.0"){
//    	var message = "";
//    	//*********************************************************
//    	// Message
//    	//*********************************************************
//    	message += "You are using an old version of Internet Explorer, ";
//    	message += "<a href=\"http://www.microsoft.com/windows/internet-explorer/default.aspx\" target=\"_blank\">Update Here!</a> ";
//    	message += "However, this site is best viewed when using <a href=\"http://www.mozilla.com/en-US/firefox/ie.html\" target=\"_blank\">Firefox</a> ";
//    	message += "or <a href=\"http://www.apple.com/safari/download\" target=\"_blank\">Safari</a>";
//    	
//        $("body").prepend("<div class=\"ie_alert\"><img src=\"/img/alert.jpg\" /><span> OOPS!</span> " + message);
//        
//        
//    	//*********************************************************
//    	// Shift the body bg image down
//    	//*********************************************************
//    	$("body").css("background-position", "50% 31px");
//    }
//});



