////////////////////////////////////////////////////////////////////////////
// NYCRx Javascript Calls
////////////////////////////////////////////////////////////////////////////

// include other JS...
document.write('<script src="/assets/javascript/md5.js"><\/script>');

// include other JS...
document.write('<script src="/assets/javascript/prototype.js"><\/script>');
document.write('<script src="/assets/javascript/scriptaculous.js"><\/script>');

// call a global variable  (rather than sending it out and parsing it back in)
var theID = '';

// this function toggles between two visible divs
function toggleLayer(whichLayerShow, whichLayerHide)
{
	document.getElementById(whichLayerHide).style.display = "none";
	new Effect.Appear(document.getElementById(whichLayerShow));
}

// this function allows the visible toggle for just one div
function showHideLayer(whichLayer)
{
	theLayer = document.getElementById(whichLayer);
	theLayer.style.display = (theLayer.style.display != "none") ? "none" : "";
}

// this function hides one div
function hideLayer(whichLayer)
{
	//document.getElementById(whichLayer).style.display = "none";
	new Effect.Fade( document.getElementById(whichLayer));
}

// this function hides many div
function hideLayers(whichLayers)
{
	var layersArray = whichLayers.split("::");
	for (var i = 0; i < layersArray.length; i++) {
		document.getElementById(layersArray[i]).style.display = "none";		
	}
}

// this function shows a div
function showLayer(whichLayer)
{
	//document.getElementById(whichLayer).style.display = "block";
  new Effect.Appear( document.getElementById(whichLayer));
}

// encrupt a field
function encryptField(id,text) {
		text = calcMD5(text);
		document.getElementById(id).value = text;
}

function gallerySecure(permissions,id) {
	if (permissions == "Secure") {
		showLayer('gallerySecure' + id);
	}
	else {
		hideLayer('gallerySecure' + id);
	}
	if (permissions != "Public") {
		showLayer('galleryPrivate' + id);
	}
	else {
		hideLayer('galleryPrivate' + id);
	}
}

function changeFontSize(change) {
	if (!document.body.style.fontSize) {
		document.body.style.fontSize = '0.95em';
	}
	if (change == 'up') {
		document.body.style.fontSize = (document.body.style.fontSize.replace(/em/,'')*1 + .2) + 'em';
	}
	if (change == 'down') {
		document.body.style.fontSize = (document.body.style.fontSize.replace(/em/,'')*1 - .2) + 'em';
	}
	ajaxer('f=fontSizeUpdate&d[size]=' + document.body.style.fontSize,'ajax','');
}

// these are generic AJAX functions
var req;

function ajaxer(data,file,div) 
{
	// hope to deprecate this soon... here, we assign a universal
	// variable  for the div id
	theID = div;
	if (div !='') {
		document.getElementById(theID).innerHTML = '<br /><br /><img src="/images/spinner.gif" alt="loading..." /><br /><br />loading';
	}
	// branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.open("POST", "/assets/ajax/" + file + ".php", true);
				req.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
        req.send(data);
        req.onreadystatechange = processReqChange;
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("POST", "/assets/ajax/" + file + ".php", true);
						req.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
            req.send(data);
        }
    }
}
function processReqChange() 
{
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200 && theID !='') {
			// set the id's HTML to the result
			document.getElementById(theID).innerHTML = req.responseText;
			new Effect.Appear( document.getElementById(theID));
		} 
		if (req.status != 200) {
			// explain the problem (we need error checking here)
			alert("Ajax Javascript Request Error\n" + req.statusText);
		}
	}
}