// JavaScript Document

// ================= Function rollOver ======================================
// a function to control mouse-over images for links/buttons
//
// PARAMETERS
// img_name - name of the image holder, as defined in the HTML
// img_src - source of the image to be replacing the existing one
// ===========================================================================

function rollOver ( img_name , img_src ) {

	document[img_name].src = img_src ;

}

// ================= End Function rollOver ===================================


// ================= Function popUpWindow ====================================
// a function that displays pop up windows
//
// PARAMETERS
// img_name - name of the image holder, as defined in the HTML
// img_src - source of the image to be replacing the existing one
// ===========================================================================
var pop_up_win = 0 ;

function popUpWindow ( url_str , win_width , win_height , from_top , from_left ) {
	
	if ( pop_up_win ) {
		if ( !pop_up_win.closed ) {
			pop_up_win.close() ;
		}
	}

	pop_up_win = open ( url_str , 'pop_up_win' , 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=' + win_width + ',height=' + win_height + ',left=' + from_left + ',top=' + from_top + ',screenX=' + from_left + ',screenY=' + from_top ) ;

}

// ================= End Function popUpWindow ================================


// ================= Function verifyDelete ===================================
// Pops up a warning box asking if the user is sure they want to delete the
// item. On approval, it redirects to a page that does the deleting.  On
// denial, it cancels the action
//
// PARAMETERS
// itemID - index number of the item to be deleted
// type - what type of item is going to be deleted
// ===========================================================================

function verifyDelete ( itemID , type ) {
	
	// define the confirmation box
	var where_to = confirm ( "Are you SURE you want to delete this " + type + "?" ) ;
	
	// check to see what the response was
	if ( where_to == true ) {
		
		// if they said yes, go to the delete page
		window.location = "delitem.php?id=" + itemID + "&type=" + type ;
	} else {
		
  		// otherwise, do nothing here
		// we may want to throw something here eventually, but I don't think so
	} // end response checking if statement
}

// ================= End Function verifyDelete ===============================


// ------------- FUNCTION check_all -------------------------------
// a function to check/uncheck all text boxes by clicking one check box

function check_all () {
	// check to see if checked is true or false
	if ( checked == false ) {
		// if false, make it true
		checked = true ;
	} else {
		// and vice versa
		checked = false ;
	}
	// now, change the rest of the checkboxes
	for ( i = 0 ; document.getElementById('frmSongSelect').elements.length ; i ++ ) {
		document.getElementById('frmSongSelect').elements[i].checked = checked ;
	}
}

// end function check_all

// ------------- FUNCTION toggleLayer -----------------------------
// a function to display or hide lavers, similar to tabbed listings

function toggleLayer ( whichLayer ) {
	
	var layersArray = new Array();
	layersArray[0] = 'phone' ;
	layersArray[1] = 'address' ;
	layersArray[2] = 'hours' ;
	layersArray[3] = 'directions' ;
	layersArray[4] = 'message' ;

	// set some variables
	var elem, vis, layer, useme;
	
	// get the element in whichever way necessary and hide all the layers
	if ( document.getElementById ) 	{
		// this is the way the standards work
		elem = document.getElementById( whichLayer ) ;
		
		// switch all layers to display:none
		layer1 = document.getElementById( 'phone' ) ;
		layer1.style.display = "none" ;
		layer2 = document.getElementById( 'address' ) ;
		layer2.style.display = "none" ;
		layer3 = document.getElementById( 'hours' ) ;
		layer3.style.display = "none" ;
		layer4 = document.getElementById( 'directions' ) ;
		layer4.style.display = "none" ;
		layer5 = document.getElementById( 'message' ) ;
		layer5.style.display = "none" ;
	} else if ( document.all ) {
		// this is the way old msie versions work
		elem = document.all[whichLayer] ;
		
		// switch all layers to display:none
		layer = document.all["phone"] ;
		layer.style.display = "none" ;		
		layer = document.all["address"] ;
		layer.style.display = "none" ;		
		layer = document.all["hours"] ;
		layer.style.display = "none" ;		
		layer = document.all["directions"] ;
		layer.style.display = "none" ;		
		layer = document.all["message"] ;
		layer.style.display = "none" ;
	} else if ( document.layers ) { 
		// this is the way nn4 works
		elem = document.layers[whichLayer] ;
		
		// switch all layers to display:none
		layer = document.layers["phone"] ;
		layer.style.display = "none" ;
		layer = document.layers["address"] ;
		layer.style.display = "none" ;
		layer = document.layers["hours"] ;
		layer.style.display = "none" ;
		layer = document.layers["directions"] ;
		layer.style.display = "none" ;
		layer = document.layers["message"] ;
		layer.style.display = "none" ;
	}
	
	// make the selected item visible
	elem.style.display = "block" ;
}

// end function toggleLayer

// ------------- FUNCTION getObject -------------------------------
// a background function to determine the browser and method required to
// work with objects on the page

function getObject ( object ) {
	var theObject ;
	if ( document.all ) {
    	if ( typeof object == "string" ) {
			return document.all ( object ) ;
    	} else {
      		return object.style;
    	}
  	}
  	if ( document.getElementById ) {
    	if ( typeof object == "string" ) {
      		return document.getElementById ( object ) ;
    	} else {
      		return object.style ;
    	}
  	}
  	return null;
}

// end function getObject

// ------------- FUNCTION countCharacters -------------------------
// a function to count the characters in a text field and display how many
// characters are remaining

function countCharacters ( inputBox , messageBox , messageText , characters ) {
	// select the text box object
	var inputObject = getObject ( inputBox ) ;
	
	// select the output object
	var messageObject = getObject ( messageBox ) ;
	
	// calculate the remaining characters
	var remaining = characters - inputObject.value.length;
	
	// check to see if there are 0 characters left
	if ( remaining <= 0 ) {
    	remaining = 0 ;
    	messageText = '<span class="disable"> ' + messageText + ' </span>';
    	inputObject.value = inputObject.value.substr ( 0 , characters ) ;
	}
	
	// set the message
	messageObject.innerHTML = messageText.replace ( "{CHAR}" , remaining ) ;
}

// end function countCharacters

// ================= Function setFocus =======================================
// Sets the focus to a particular field on the page
//
// PARAMETERS
// form - name of the form the field is contained in
// field - name of the field to set the focus on
// ===========================================================================

function setFocus ( field ) {
	// get the object to focus
	var focusObject = getObject ( field ) ;
	// set the focus
	focusObject.focus () ;
}

// ================= End Function setFocus ===================================


// ================= Function selectDate =====================================
// Automagically opens a pop-up window with a calendar in order to select a
// date
//
// PARAMETERS
// field - name of the field which will be propagated by the date picker
// ===========================================================================

function selectDate ( field ) {
	// get the field
	var dateField = getObject ( field ) ;
	
	// get any existing value in the field
    var D = dateField.value ;
	
	// define the url we're popping up
	var url = "../codepages/date-picker.php?field=" + field ;
	
	// check to see if the data exists or is a non-date
    if ( D && D != "00/00/00" ) {
		// if it exists, split it up into its components and add the month and year tags to the url		
        D = D.split ( '/' ) ;
		url = url + "&mo=" + D[0] + "&yr=" + D[2] ;
    }
	
	// open the window
    win = window.open ( url , "win" , "status=no,scrollbars=no,toolbar=no,menubar=no,height=165,width=165" ) ;
	
	// alternate method for other browsers
    if ( parseInt ( navigator.appVersion ) == 2 && navigator.appName == "Netscape" )
        win = window.open ( url , "win" , "status=yes,height=325,width=250" ) ;
        //win'MakeDate',D[2], D[1],D[0], 'SetDate');
        //win.MakeDate ( D[2] , D[1] , D[0] ) ;
}

// ================= End Function selectDate =================================


// ================= Function fillDate =======================================
// Fills in a textbox with a date from another page
//
// PARAMETERS
// Day
// Month
// Year
// field - name of the field which will be propagated by the date picker
// ===========================================================================

function fillDate ( Day , Month , Year , field ) {
	// get the field
	var dateField = getObject ( field ) ;
	
	// fill the data
    dateField.value = Month + '/' + Day + '/' + Year;
}

// ================= End Function fillDate =====================================


// ================= Function returnDate =======================================
// Returns selected date from calling page and closes date pick
//
// PARAMETERS
// Day
// Month
// Year
// field - name of the field which will be propagated by the date picker
// ===========================================================================

function returnDate ( Day , Month , Year , field ) {
	
	// call the data filling function on the calling page
    opener.fillDate ( Day , Month , Year , field ) ;
	
	// close the window
    window.close ( ) ;
}

// ================= End Function returnDate ===================================


// ================= Function checkPassword =======================================
// Checks a confirm password field against a password field to ensure proper entry
// of a password for a user.  Also discards checking if check field is disabled
//
// PARAMETERS
// first - first password field
// second - second password field
// ===========================================================================

function checkPassword ( first , second ) {
	
	// create a variable to hold the message to be displayed
	var msg = '' ;
	
	// check to see if the second field is disabled
	if ( second.disabled == false ) {
			
			// if it's not disabled, then we need to check the passwords
			// check to see if the two fields match
			if ( second.value != first.value ) {
		
				// if not, create a message to display
				msg = 'Your passwords do not match!  Please re-type your passwords.';
			}
	}
	
	// check to see if there is a message
	if ( msg ) {
		
		// if so, display it in a message box
		alert ( msg ) ;
		
		// return to the page
		return false ;
	}
	
	// continue with the form submission
	return true ;
	
}

// ================= End Function returnDate ===================================