/* 
  -  Common Javascrip routines for this site 
  -  Bill Leddy - Mar 7, 2009 
*/


// alias jquery so A4D won't try to interpret it as a local variable
var j$ = jQuery;
jQuery().noConflict;

function toggleInactive() {
	// generic way to hide and show
	j$('.OSHA-activeStaff, .OSHA-inactiveStaff').toggle();
	if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion.search(/MSIE 8+/) > 0)) {
		var theMessage = "Sorry, This Feature may not work with Internet Explorer 8.";
		theMessage += '\n\nSwitching to "Compatability View" from the Tools menu should help.';
		theMessage += "\n\nIf you still have trouble, try accessing our site using either the FireFox or Chrome web browsers.";
		alert(theMessage);
	}
}
/* Enforce the business rules for spore test paperless options*/
function enforceSporeTestOptions (theTarget) {
	if((j$('#customers__Fax_Test_Results').attr('checked') == true) && (j$('#customers__Print_Test_Results').attr('checked') == true)){
		j$('#'+theTarget.id).removeAttr('checked');
		alert("You can't select to receive the report by both Fax & Mail. ");
		return false;
	}
	else {
		return true;
	}
}

function busy(ObjectSelector,isBusy){
	// Ex: busy('#busyDiv',true)
	if(isBusy){
		j$(ObjectSelector).html('<img height="16" width="16" src="/images/busy.gif" alt="busy"/>');
	} else {
		j$(ObjectSelector).html('');
	}
}

// Pause script execution for a bit
function executeLater(scriptToRun,pauseTime) {
	// pass pauseTime as seconds
	if(!pauseTime) var pauseTime = 2;
	if(!scriptToRun) var scriptToRun = "";
	pauseTime = pauseTime * 1000; // convert to miliseconds
	var pauseTimer = setTimeout(scriptToRun,pauseTime);
}

function openFormDialog(theDialog,pageURL) {
	if(!theDialog) theDialog = '#dialog-content';
	if(pageURL) {
		// get the current dimensions of the document
		var docHeight = window.outerHeight;
		var docWidth = window.outerWidth;
		//j$(theDialog).Hide();
		
		j$(theDialog).load(pageURL,
			function(){
				// get the dimensions of content
				var theHeight = j$(theDialog).outerHeight();
				var theWidth = j$(theDialog).outerWidth();
				//alert('doc = '+docHeight+' dlog = '+theHeight)
				var theTop = (docHeight / 3) - (theHeight / 2) + window.pageYOffset;
				var theLeft = (docWidth / 2) - (theWidth / 2) + window.pageXOffset;
				// position into view
				j$(theDialog).css('position','absolute').css('top',theTop.toString()+'px').css('left',theLeft.toString()+'px').css('z-index','1000').show();
		});
	}
}


// Paint the screen with a translucent div to simulate a modal dialog
function setModal(objectID,modalState) {
	var objectID = "#"+objectID;
	var docHeight = j$(document).height()+"px";
	var docWidth = j$(document).width()+"px";
	j$(objectID).css("height",docHeight).css("width",docWidth);
	if(modalState) {
		// display the div
		j$(objectID).show();
	}
	else {
		//hide the div
		j$(objectID).hide();
	}
}

/*
	For forms that are displayed along with the left side navigation menu.
	Alert user that the form has been modified before allowing them to navigate away from the form
*/
var formModified = "";
function testFormIsModified(whichFormSelector) {
	// whichFormSelector is a jQuery compatible selector name 
	j$('.side-menu a').click(function(){
		if(formModified != "") {
			//alert("display dlog");
			var theTarget = j$(this).attr('href'); // need to pass this to the called dialog method...
			j$(this).removeAttr('href'); // so the link is not followed
			setModal("modal-background",true);
			var pageURL = "/index.a4d?action=home.saveFormDlg";
			if(theTarget != undefined) pageURL += ';returnTo='+theTarget;
			openFormDialog("#dialog-content",pageURL+';formID='+formModified);
		}
	});
	j$(whichFormSelector+' input:text, '+whichFormSelector+' textarea').change(function(){if(formModified ==''){formModified = whichFormSelector;}});
	j$(whichFormSelector+' input:checkbox, '+whichFormSelector+' input:radio').click(function(){if(formModified ==''){formModified = whichFormSelector;}});
}

function setPaperlessActions(theURL) {
	j$('.paperlessOption').click(function() {
	//alert(this.name+"="+this.checked);
		if(enforceSporeTestOptions(this)) {
			//alert('saving option ');
			j$.get(theURL+this.name+"="+this.checked);
		}
		else {//alert('Not Save.');
		}
	});

}

function formatPhone(phone) {
	// format the phone number to standard and return
	var Phone_t=getNumbersOnly (phone);
	var Length_i=Phone_t.length;
	
	if(Length_i==7) {
	  //Number without area code
		Phone_t=Phone_t.substr(0,3)+"-"+Phone_t.substr(3);
	}
	else if (Length_i==10) {
	  //Area code and Number
		Phone_t="("+Phone_t.substr(0,3)+") "+Phone_t.substr(3,3)+"-"+Phone_t.substr(6,4);
	}
	else {
	  // just return the number as submitted
		Phone_t=phone;
	}
	return Phone_t;
} //formatPhone

function getNumbersOnly(numberString) {
	// Return only the numerals from the string passed in 
	var out = '';
	var theNums = '0123456789';
	for(var i = numberString.length - 1; i>=0 ; i--) {
		if(theNums.indexOf(numberString.substr(i,1)) != -1 ) {
			out = numberString.substr(i,1) + out;
		}
	}
	return out;
}
