/*
	JAVASCRIPT FUNCTION INCLUDE
	EDC - Peter & Scottish
	04/01/2008
	River City Studio
*/

jQuery(document).ready(function() {
	initJQuery();
});

function initJQuery() {
	jQuery("a.iframe").live("click",function(e){
		e.preventDefault();
		jQuery(this).fancybox({
		'imageScale': true,
		'padding': 10,
		'zoomOpacity': true,
		'zoomSpeedIn': 1250,
		'zoomSpeedOut': 1250,
		'zoomSpeedChange': 1000,
		'overlayShow': true,
		'overlayColor': "#000",
		'overlayOpacity': 0.8,
		'enableEscapeButton': true,
		'showCloseButton': true,
		'hideOnOverlayClick': true,
		'hideOnContentClick': false,
		'width': '90%',
		'height': '90%',
		'type': 'iframe',
		'callbackOnStart': null,
		'callbackOnShow': null,
		'callbackOnClose': null,
		'centerOnScroll': true,
		'transitionIn': 'fade',
		'transitionOut': 'fade'
	}); return false;	});
	$('.datePicker').live('click', function() {
        $(this).datepicker({showOn:'focus',dateFormat: "yy-mm-dd",
		numberOfMonths: 1,
		duration: "",
		stepMonths: 1,
		showButtonPanel: true}).focus();
    });
/*	jQuery(".datePicker").datepicker({
		dateFormat: "yy-mm-dd",
		numberOfMonths: 1,
		duration: "",
		stepMonths: 1,
		showButtonPanel: true
});*/	}

var d = window.document;

//////////////////////////////////////
//  GENERIC RETURN ELEMENT FUNCTION //
//////////////////////////////////////

function getE( v ) {
  var d = window.document;
  e = false;
  if ( d.getElementById ) {
    e = d.getElementById( v );
  }
  else if ( d.all ) {
    e = d.all[ v ];
  }
  return e;
}


function removeElementById(remele) 
{
	remele = getE(remele);
	if (remele.parentNode && remele.parentNode.removeChild) 
	{
		remele.parentNode.removeChild(remele); 
	}
}



////////////////////////////////
//  SHOW / HIDE ELEMENT       //
////////////////////////////////

function toggleV(incoming_object_name,visibility)
{
	g = getE(incoming_object_name);
	g.style.visibility = visibility;

  /*string = "";
  for ( a in g ) {
    string += a + "\n";
  } 
  alert( string );
  */		
}


/////////////////////////////////////
//  DISABLE / ENABLE FORM ELEMENT  //
/////////////////////////////////////

function toggleD(incoming_object_name,enable_status) 
{
	e = getE(incoming_object_name);
	e.disabled=enable_status
	
	/*
	if (enable_status == false) // enable 
		e.disabled=false;
	if (enable_status == true ) //disable
		e.disabled=true;
	*/
}


///////////////////////////////
// RADIO CHECKED VALUES      //
///////////////////////////////

function setCheckedValue(radioObj, newValue) 
{
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) 
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++)
	{
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) 
		{
			radioObj[i].checked = true;
		}
	}
}
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
		{
			return radioObj[i].value;
		}
	}
	return "";
}

//////////////////////////////
// SELECT LIST VALUES       //
//////////////////////////////

function setSelectListVal(selectID,setVal) 
{
	sel = getE(selectID);
	for (i=0; i<sel.options.length; i++) 
		if (sel.options[i].text == setVal)
			sel.selectedIndex = i;
}

function setTextValue(selectID, setVal) {
	sel = getE(selectID);
	sel.value = setVal;
}

function getSelectListVal(selectID)
{
	sel = getE(selectID);
	index = sel.selectedIndex;
	return sel[index].value;
}

function getSelectListText(selectID)
{
	sel = getE(selectID);
	index = sel.selectedIndex;
	return sel[index].text;
}




function confirmSubmit()
{
	var agree=confirm("Are you sure you wish to continue?");
	if (agree)
		return true ;
	else
		return false ;
}


////////////////////////////
//	FOR TIMECARD FORM		//
////////////////////////////

function totalRow(TimeID)
{
	var fromTimeVal = getSelectListVal('From'+TimeID+'Time');
	var fromDate = Date.parse(fromTimeVal);

	var toTimeVal = getSelectListVal('To'+TimeID+'Time');
	var toDate = Date.parse(toTimeVal);
	
	var span = (toDate.getTime() - fromDate.getTime()) / 1000; //SECONDS DIFFERENCE
	var spanHours = span / 60 / 60 ;
	
	getE('SubTotal'+TimeID).value = spanHours;
	//getE('SaveTimes'+TimeID).value = 1;
}
function advanceToTime(TimeID)
{
	var fromTimeVal = getSelectListVal('From'+TimeID+'Time');
	var fromDate = Date.parse(fromTimeVal);
	
	var toTimeVal = getSelectListVal('To'+TimeID+'Time');
	var toDate = Date.parse(toTimeVal);
	var span = (toDate.getTime() - fromDate.getTime()) / 1000; //SECONDS DIFFERENCE
	var spanHours = span / 60 / 60 ;
	
	if(spanHours < 0.25)
	{
		var newToDate = fromDate.add({ minutes: 15 });
		var newToTimeVal = newToDate.toString("h:mm tt");
		setSelectListVal('To'+TimeID+'Time',newToTimeVal);
	}
	totalRow(TimeID);
}
function backtrackToTime(TimeID)
{
	var toTimeVal = getSelectListVal('To'+TimeID+'Time');
	var toDate = Date.parse(toTimeVal);
	
	var fromTimeVal = getSelectListVal('From'+TimeID+'Time');
	var fromDate = Date.parse(fromTimeVal);
	var span = (toDate.getTime() - fromDate.getTime()) / 1000; //SECONDS DIFFERENCE
	var spanHours = span / 60 / 60 ;
	
	if(spanHours < 0.25)
	{
		var newFromDate = toDate.add({ minutes: -15 });
		var newFromTimeVal = newFromDate.toString("h:mm tt");
		setSelectListVal('From'+TimeID+'Time',newFromTimeVal);
	}
	totalRow(TimeID);
}





////////////////////////////
//	FOR NORMAL FORMS			//
////////////////////////////


function checkForm( action, url ) {
	var r = false;
	var s = "";
	
	for ( $i=0; $i<d.forms[0].length; $i++ ) {
		// alert ( d.forms[0][$i].name + " = " + d.forms[0][$i].value );
		if ( d.forms[0][$i].value.length > 0 ) {
			r = true;
		}
		else {
			r = false;
			alert( "All Fields Are Required" );
			return;
		}
	}
	
	if ( r ) {
		for ( $i=0; $i<d.forms[0].length; $i++ ) {
				s += "&" + d.forms[0][$i].name + "=" + d.forms[0][$i].value;
		}
		window.location.href = url + "?" + action + s;
	}
	
}

//////////////////////////
//	FOR AUTH FORMS			//
//////////////////////////


function checkPWord( action ) {
	if ( d.forms[0].p1 && d.forms[0].p2 ) {
		var p1 = d.forms[0].p1.value;
		var p2 = d.forms[0].p2.value;
	}
	
	var r = false;
	var s = "";
	
	for ( $i=0; $i<d.forms[0].length; $i++ ) {
		// alert ( d.forms[0][$i].name + " = " + d.forms[0][$i].value );
		if ( d.forms[0][$i].value.length > 0 ) {
			r = true;
		}
		else {
			r = false;
			alert( "All Fields Are Required" );
			return;
		}
	}
	
	if ( r && p1 && p2 ) {
		if ( p1 != p2 ) {
			alert( "Password Mismatch" );
			r = false;
		}
		else if ( p1.length < 6 ) {
			alert( "Password is too short!!" );
			r = false;
		}
	}
	
	if ( r ) {
		for ( $i=0; $i<d.forms[0].length; $i++ ) {
				s += "&" + d.forms[0][$i].name + "=" + d.forms[0][$i].value;
		}
		window.location.href = "/admin/auth/process.php?" + action + s;
	}
		
}


////////////////////////
//	FOR OPTIONAL			//
//	PASSWORD CHANGE		//
////////////////////////


function infoBox( v ) {
		e = d.getElementById( "infoBox" );
		if ( v == "1" ) {
			e.innerHTML = "<p>New Password: <input class='lineUp' type='password' name='p1' /></p><p>Verify New Password: <input class='lineUp' type='password' name='p2' /></p><p>&nbsp;</p>";
		}
		else if ( v == "0" ) {
			e.innerHTML = "";	
		}
}


//////////////////////////
//	AUTO FILL PHOTO			//
//	WHEN ADDIN A SHOW		//
//////////////////////////

function photo( p, f ) {
//   if ( d.getElementById( f + "Pop" ) ) {
//     document.forms[0][f].value = p;
//   	d.getElementById( f + "Pop" ).innerHTML = "<img src='/i/100/" + p + "' />";
//   }
//   else {
//     document.forms[0][f].text = p;
//     d[ f + "Pop" ].innerHTML = "<img src='/i/100/" + p + "' />";
//   }
  alert( "hi." );

}



////////////////////////////
//	ADD A CHARACTER TO		//
//	A FORM FIELD					//
////////////////////////////

function add( c, e ) {
	d.forms[0][e].value += c;
  count( e ); 
}


////////////////////////////
//  COUNT CHARACTERS      //
//  IN A FORM FIELD       //
//  UPDATE COUNT          //
//  ACCORDINGLY           //
////////////////////////////

function count( f ) {
//   alert( f );
  c = 8000 - d.forms[0][f].value.length;
  e = d.getElementById( f );
  e.innerHTML = "<p>Characters remaining: " + c + "</p>";
  if ( c < 0 ) {
    e.innerHTML = "<p class='red'>Out of Space!</p>";
  }
}





/////
//	FOR DETERMINING WHICH PAGE, IF ANY, HAS BEEN SELECTED

var selectedPage = false;

////////////////////////////////
//	REDRAW PAGE LIST FOR SUB	//
//	PAGE ORDERING							//
////////////////////////////////

function redrawPageList() {
	e = getE( "pageList" );
	e.innerHTML = "";
	for ( i=1, j=pages.length; i<j; i++ ) {
		var classStr = "";
		if ( selectedPage == i ) classStr = " class='selected'";
		s = "\n		<p><a href='#'" + classStr + " onclick='selectPage( " + i + " ); return false;'>" + pageTitles[pages[i]] + "</a></p>";
		e.innerHTML+= s;
	}
}

////////////////////////////////
//	SELECT A PAGE TO MOVE			//
////////////////////////////////

function selectPage( i ) {
	
	if ( !selectedPage || selectedPage != i ) {
		selectedPage = i;	
	}
	else {
		selectedPage = false;
	}
	redrawPageList();
	
}

////////////////////////////////
//	MOVE A PAGE UP						//
////////////////////////////////

function up() {
	i = selectedPage;
	
	if ( i && i != 1 ) {
		p1 = pages[i];
		p2 = pages[i-1];
		
		pages[i-1] = p1;
		pages[i] = p2;
		
		selectedPage = i-1;
		redrawPageList();
	}
}

////////////////////////////////
//	MOVE A PAGE DOWN					//
////////////////////////////////

function down() {
	i = selectedPage;
	
	if ( i  && i != ( pages.length-1 ) ) {
		p1 = pages[i];
		p2 = pages[i+1];
		
		pages[i] = p2;
		pages[i+1] = p1;
		
		selectedPage = i+1;
		redrawPageList();
	}
}

////////////////////////////////
//	SAVE THE PAGE ORDER				//
////////////////////////////////

function savePages() {
	for ( i=1, j=pages.length; i<j; i++ ) {
		s+= pages[i] + "|" + i;
		if ( i < j-1 ) s+= "||";
	}
	d.forms[0].pages.value = s;
	d.forms[0].submit();
}





/*
	AJAX BASE FUNCTIONALITY
*/

var xmlhttp = null;
var displayDiv = "";
var nextFunc = "";

////////////////////////////////
//	BASIC AJAX LOADER					//
////////////////////////////////

function getPage( displayDiv, url, second ) {

	if ( second != false ) nextFunc = second;
	else nextFunc = "";
	
	xmlhttp=null;
	if (window.XMLHttpRequest) {// code for all new browsers
  	xmlhttp=new XMLHttpRequest();
  }
	else if (window.ActiveXObject) {// code for IE5 and IE6
  	xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
  }
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=function() {
			stateChange( displayDiv, nextFunc );
		}
		xmlhttp.open( "GET", url, true );
		xmlhttp.send( null );
  }
	else {
  	alert( "Your browser does not support XMLHTTP." );
  }
}

////////////////////////////////
//	EVENT HANDLER FOR HTTP		//
//	STATE CHANGE							//
////////////////////////////////

function stateChange( displayDiv, nextFunc ) {
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200) {// 200 = OK
			e = getE( displayDiv );
			if (displayDiv != false) {
				e.innerHTML = xmlhttp.responseText;
				initJQuery();
			}
			if ( nextFunc != "" ) eval( nextFunc );
		}
		else {
			alert("Problem retrieving XML data");
		}
	}
}


/* ---------------------------- Call Function  ---------------------------------- */

function popContent( displayDiv, url, second ) {
	e = getE( displayDiv );
	e.innerHTML = "<img id='ajaxPreloader' src='/_img/icons/animatedLoader.gif' alt='' />";
	getPage( displayDiv, url, second );
}



	/* ------------------- JQuery DatePicker Instantiation ---------------------- */

function makeDatePicker() {
	$(".datePicker").datepicker({
		dateFormat: "yy-m-d",
		numberOfMonths: 3,
		duration: "",
		stepMonths: 3,
		showButtonPanel: true
	});
}



	/* ----------------------- TinyMCE Call Script ------------------------------ */
	
function makeWYSIWYG(incomingWidth, incomingHeight) {

	if(!incomingWidth) {
		incomingWidth = 250;
	}
	
	if(!incomingHeight) {
		incomingHeight = 135;
	}
	
	tinyMCE.init({
		mode : "textareas",
		theme : "simple",
		width : incomingWidth,
		height: incomingHeight
	});
}


	/* ----------------------- Secondary function call for bid options ------------- */
	
function popBidOptions(bidID) {
	popContent("bidOptionDump", "/admin/clients/projects/bids/bidOptions/getBidOptions.php?bidID=" + bidID);
}

	
	// Open the new Window and move it so we can see the options
	
	function popBidWindow(bidID) {
		windowOpened = window.open("/admin/clients/projects/bids/manageBids.php?bidID=" + bidID, "bidSpec", "width=775, height=800, scrollbars=1")
		windowOpened.moveTo(window.windowOpened.opener.innerWidth, 0);
	}
	
	
	// Reload the Child Window ... aka submit the form
	
	function reloadFormWindow( bidOptionID ) {
		try {
			document.forms["bidForm"].bidOptionToAdd.value = bidOptionID;
// 			document.forms["bidForm"].submit();
			submitBidForm();
		}
		catch(e) {
			alert("You don't have a bid open for editing.\n\nPlease select from the list at the top.");
		}
	}


	// Submit Ajax Bid Form 
	
	function submitBidForm() {
		var getBidForm = document.forms["bidForm"];
		for(i=0; i<getBidForm.length; i++) {
			if( getBidForm[i].type == "checkbox" ) {
				if ( getBidForm[i].checked == false ) {
					getBidForm[i].value = "";
				}
			}
		}
		tinyMCE.triggerSave();
		
// 		alert(getBidForm.description.value);
		
		popForm( "right", "manageBids.php", getBidForm, "makeWYSIWYG('425', '105')");
	}
	
	
	function popOptionWindow(targetURL) {
		optionWindow = window.open(targetURL, "bidOption", "width=385, height=400, scrollbars=1");
		optionWindow.moveTo(window.optionWindow.opener.innerWidth, 0);
	}
	
	function closeOptionWindow() {
		window.opener.location.reload();
		window.close();
	}



	/* ------------------ Additional Bid Functions once you get there -------------------------------- */
	
	function moveOrderDown(i) {
		var thisElement = document.bidForm["order[" + i + "]"];
		thisElement.value = parseFloat(thisElement.value) + 1;
		
		var nextElement = document.bidForm["order[" + (parseFloat(i) + 1) + "]"];
		nextElement.value = parseFloat(nextElement.value) - 1;
		
// 		document.bidForm.submit();
		submitBidForm();
	}
	
	function moveOrderUp(i) {
		var thisElement = document.bidForm["order[" + i + "]"];
		thisElement.value = parseFloat(thisElement.value) - 1;
		
		var nextElement = document.bidForm["order[" + (parseFloat(i) - 1) + "]"];
		nextElement.value = parseFloat(nextElement.value) + 1;
		
// 		document.bidForm.submit();
		submitBidForm();
	}
	
	
	function swapOrder(orderInput) {
		// Get the total number of Order Inputs
	
		var totalDivs = document.getElementsByTagName("div");
		var numberOfFormPieces = 0;
		
		for(i=0; i<totalDivs.length; i++) {
			if(totalDivs[i].className == "bidPiece") {
				numberOfFormPieces++;
			}
		}
		
		// Get the original value and the new value for comparison
		
		var currentValue = orderInput.attributes[2].nodeValue;
		var newValue = orderInput.value;
		
		// Check for change
		
		if(newValue > currentValue) {
			for(j=0; j<numberOfFormPieces; j++) {
				var orderID = document.bidForm["order[" + j + "]"];
				if(orderID.name != orderInput.name) {
					if(orderID.value <= newValue && orderID.value >= currentValue) {
						orderID.value = parseFloat(orderID.value) - 1;
					}
				}
				
				// Reset the original value for another change
				orderID.attributes[2].nodeValue = orderID.value;
			}
		} else if(newValue < currentValue) {
			for(j=0; j<numberOfFormPieces; j++) {
				var orderID = document.bidForm["order[" + j + "]"];
				if(orderID.name != orderInput.name) {
					if(orderID.value >= newValue && orderID.value <= currentValue) {
						orderID.value = parseFloat(orderID.value) + 1;
					}
				}
				
				// Reset the original value for another change
				orderID.attributes[2].nodeValue = orderID.value;
			}
		}
	}


/*
	FTP USERS FUNCTIONALITY
*/

function getDir( dir ) {
	displayDiv = "getDirs";
	getPage( displayDiv, "getDirectories.php?dir="+dir, false );
	
	f = d.forms[0];
	f.currentDir.value = dir;
}



	/* ------------------ Function to Show/Hide Hosting in Bible ------------------ */

function showHosting(e, incomingName, incomingAddress) {
	var hostName = document.getElementById("hostName");
	var hostAddress = document.getElementById("hostAddress");
	var container = document.getElementById("outsideHost");

	if (e.checked == true) {
		container.style.display = "none";
		hostName.value = "River City";
		hostAddress.value = "ftp3.rivercitynet.com";
	} else {
		container.style.display = "block";
		hostName.value = incomingName;
		hostAddress.value = incomingAddress;
	}
}



	/* --------------------- Alias Switch for the Email Section of the Bible -------------------- */
	
function showAlias(e) {
	var accountFields = document.getElementById("accountInputs").style;
	var aliasFields = document.getElementById("aliasInputs").style;
	
	if (e.checked == true) {
		accountFields.display = "none";
		aliasFields.display = "block";
	}
	else {
		accountFields.display = "block";
		aliasFields.display = "none";
	}
}



	/* -------------------------- Analytics Swap in the Bible ------------------------- */
	
function showAnalytics(e, incomingHost, incomingAddress, incomingUsername, incomingPassword) {
	aHost = document.getElementById("analyticsHost");
	aAddress = document.getElementById("address");
	aUser = document.getElementById("username");
	aPass = document.getElementById("password");

	if (e.checked == true) {
		aHost.value = "Google Analytics";
		aAddress.value = "http://www.google.com/analytics/";
		aUser.value = "River City Username";
		aPass.value = "River City Password";
	} else {
		aHost.value = incomingHost;
		aAddress.value = incomingAddress;
		aUser.value = incomingUsername;
		aPass.value = incomingPassword;
	}
}


//for preview section of the bible
function showPreviewType()
{
	var type = getSelectListVal('contentType');
	if(type == 'URL')
	{
		getE('addPreview').style.display="inline";
		getE('fileUpload').style.display="none";
		getE('urlLink').style.display="block";
	}
	else if(type == 'IMG' || type == 'DOC')
	{
		getE('addPreview').style.display="inline";
		getE('fileUpload').style.display="block";
		getE('urlLink').style.display="none";
	}
	else
	{
		getE('addPreview').style.display="none";
		getE('fileUpload').style.display="none";
		getE('urlLink').style.display="none";
	}
}



	/* ------------- Make Active Function For The Tabbed Feature Navigation ----------- */
	
function makeActive(e) {
	children = document.getElementById("featureNav").childNodes;
	
	for (i=0; i<children.length; i++) {
		if (children[i].nodeType == 1) {
			children[i].setAttribute("class", "");
		}
	}
	
	e.setAttribute("class", "active");
}




	/* --------------------- Inner HTML Function for adding a job with Design as Type ----------------- */
	
function populateDesignDescription(currentElement, targetID) {
	if (currentElement == "DESIGN") {
		document.getElementById(targetID).innerHTML = "Size:\n\nColor: (Color or Black &amp; White)\n\nCopy:\n\nLogo Provided? Yes/No (vector file preferred)\n\nReference Previous Job:";
	} else {
		document.getElementById(targetID).innerHTML = "";
	}
}




	/* ------------------- Newer Form Handlers for the Time Keeping System -------------------------- */
	
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}


function getSelectedEmployee() {
	var selectedEmployee;
	if(document.getElementById("selectedEmployeeID") != null) {
		selectedEmployee = document.getElementById("selectedEmployeeID").value;
	} else {
		selectedEmployee = getCookie("currentEmployeeID");
	}
	
	return selectedEmployee;
}



	// ------------------ Convenience Funtions --------------------- //

	
	// Change Selected Date
	
	function changeTimeCardDate(newDate) {
		document.getElementById("selectedTimeCardDate").value = newDate;
		submitAjaxForm();
	}
	
	// Change Selected Employee
	
	function changeEmployeeTimeCard(newID) {
		document.getElementById("selectedEmployeeID").value = newID;
		submitAjaxForm();
	}


	// Add A Time Cell
	
	function addTimeCell(jobNumber) {
		var hiddenInput = document.getElementById("addJobToToday");
		
		if(hiddenInput != null) {
			hiddenInput.value = jobNumber;
			submitAjaxForm();
		} else {
			alert("Your Time Card for Today is closed");
		}
	}
	
	// Delete a Time Cell
	
	function deleteTimeCell(timeRowID) {
		document.getElementById("deleteTimeRowID").value = timeRowID;
		submitAjaxForm();
	}
	
	
	// Submit Form
	
	function submitAjaxForm() {
		popForm( "cellDump", "getRecords.php", document.forms["timeCardForm"], "giveLastElementFocus()" );
		window.scrollTo(0,0);
	}
	
	
	// Finalize the Card
	
	function finalizeCard() {
		document.getElementById("finalize").value = 1;
		document.getElementById("editCard").value = "";
		submitAjaxForm();
	}
	
	
	
	// Edit a finalized Card
	
	function editFinalizedCard() {
		document.getElementById("editCard").value = 1;
		submitAjaxForm();
	}
	
	
	// Get the focus of the last timecode
	
	function giveLastElementFocus() {
		var codesArray = new Array();
		var jobs = document.getElementsByTagName("select");
		
		for(i=0; i<jobs.length; i++) {
			if ( jobs[i].className == "jobCode" ) {
				codesArray.push( jobs[i] );
			}
		}
		
		document.getElementById(codesArray[0].id).focus();
	}

	
	
	
	/* ------------------ Oh My Goodness This is so exciting -------------------------- */

		/* ------------------------------- POST Subission Ajax Functionality ----------------------------- */


/* ------------------------ BASIC AJAX LOADER (Differs From Regular) ---------------------------- */



function getForm( displayDiv, url, params, second ) {

	if ( second != false ) nextFunc = second;
	else nextFunc = "";
	
	xmlhttp=null;
	if (window.XMLHttpRequest) {// code for all new browsers
  	xmlhttp=new XMLHttpRequest();
  }
	else if (window.ActiveXObject) {// code for IE5 and IE6
  	xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
  }
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=function() {
			formStateChange( displayDiv, nextFunc );
		};
		xmlhttp.open( "POST", url, true );
		
		// New Headers to Manually Declare Form Submission
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		
		xmlhttp.send( params );
  }
	else {
  	alert( "Your browser does not support XMLHTTP." );
  }
}


/* ------------------- 	EVENT HANDLER FOR HTTP STATE CHANGE (Same As Regular) --------------------------- */

function formStateChange( displayDiv, nextFunc ) {
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200) {// 200 = OK
			e = getE( displayDiv );
			e.innerHTML = xmlhttp.responseText;
			if ( nextFunc != "" ) eval( nextFunc );
		}
		else {
			alert("Problem retrieving XML data");
		}
	}
}


/* ---------------------------- Call Function (Additional Variable)  ---------------------------------- */


function popForm( displayDiv, url, formToGrab, nextFunc ) {
	var params = getFormData( formToGrab );
	e = getE( displayDiv );
	e.innerHTML = "<p>loading...</p>";
	getForm( displayDiv, url, params, nextFunc );
}



/* ------- Take the Chosen Form and Make a String Uut of the Variables ------- */

function getFormData( formToGrab ) {
	var paramString = "";
	for(i=0; i<formToGrab.length; i++) {
		if(formToGrab[i].name != "" && formToGrab[i].value != "") {
			paramString = paramString + formToGrab[i].name + "=" +  formToGrab[i].value.replace(/&/g, "%26").replace(/'/g, '%27').replace(/\+/g, '%2B');
			if(i < formToGrab.length - 1)	{
				paramString = paramString + "&";
	}	}	}
	return paramString;
}

	/* -------------- Job Search Functions for the Time Card Page ----------------- */
	
function searchJobs() {
	var searchField = document.getElementById("jobSearchInput").value;
	
	if(searchField != "") {
		popContent("listCurrentJobs", "getJobs.php?search=" + searchField);
	} else {
		clearJobs();
	}
}


	
function clearJobs() {
	var searchField = document.getElementById("selectedEmployeeDropDown").value;

	popContent("listCurrentJobs", "getJobs.php?employeeID=" + searchField); document.getElementById("jobSearchInput").value = ""; 
	
}

function checkSelectValue(selectID) {
	selectValue = getSelectListVal(selectID);
	if(selectValue == '') {
		alert("Please select a value for "+selectID);
		return false;
	}
	return true;
}

