//General JavaScript functions
//Include this file in all pages that use JS.

/**
 * General cross-browser DOM object referencer.
 * @param idDiv string with the object id (defined in the id attribute of the object's tag).
 * @param refDoc DO NOT USE, recursive Netscape layer lookup.
 * @return the object's reference, false if not found.
 */
function getDivRef(idDiv, refDoc) {
	if (!refDoc)
		refDoc = document;
	
	if (document.layers) {
		if (refDoc.layers[divID])
			return refDoc.layers[divID];
		else {
			for (var x = 0, y; !y && x < refDoc.layers.length; x++)
				y = getDivRef(divID, refDoc.layers[x].document);
			
			return y;
		}
	} else if (document.getElementById)
		return document.getElementById(idDiv);
	else if (document.all)
		return document.all[idDiv];
	
	return false;
}

/**
 * Show a layer and hidde the another similar layers.
 * @param prefix string with the div's prefix.
 * @param index int with the number of layer.
 * @param totalLayers int with the total number of layers. 
 */
function showLayer(prefixDiv, index, totalLayers, resizedCell, prefixLink) {
	for (var i = 1; i<= totalLayers; i++)
	{
		if(i != index)
		{
			getDivRef(prefixDiv + i).style.visibility = "hidden";
			getDivRef(prefixLink + i).style.background="url('images/spacer.gif') no-repeat";
			getDivRef(prefixLink + i).style.paddingLeft="0px";			
			getDivRef(prefixLink + i).style.color="#0099cc";					
			getDivRef(prefixLink + i).style.textDecoration="underline";								
		}
		else
		{
			getDivRef(prefixLink + i).style.background="url('images/newsSVineta.gif') no-repeat";
			getDivRef(prefixLink + i).style.paddingLeft="15px";		
			getDivRef(prefixLink + i).style.color="#999933";					
			getDivRef(prefixLink + i).style.textDecoration="none";								
			getDivRef(prefixDiv + i).style.height = 'auto'; getDivRef(prefixDiv + i).style.visibility = "visible";
		}		
	}	
	//getDivRef("tab" + i).style.backgroundColor = "#e4e3e3";
	//getDivRef("tabLbl" + i).className = "menutab";
	newHeight = getDivRef(prefixDiv + index).offsetHeight + 25;
	resizeCell(resizedCell,newHeight);
}

/**
 * Resize a Cell based on the passed variable of height.
 * @param id string with the id of the cell that will be resized.
 * @param index int with the new height for the cell.
 */
function resizeCell(id,value) {
	var currentDiv=getDivRef(id); 
	currentDiv.style.height=value+'px';
}



function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}

/**
 * Refills a form if validation did not pass, and
 * colors incorrect/missing fields.
 * @param valArray Array containing fields and refills.
 */
function refillForm(valArray) {
	var isInvalid = "";
	
	for (var i = 0; i < valArray.length; i++) {
		var pair = valArray[i].split("|");
		var ctrlRef = getDivRef(pair[0]);
		var ctrlType = (ctrlRef ? (ctrlRef.type ? ctrlRef.type.toLowerCase() : "unknown") : "");
		
		if (ctrlType == "text" || ctrlType == "textarea" || ctrlType == "password") {
			if (ctrlType != "password")
				ctrlRef.value = pair[1];
			
			if (pair.length > 2) {
				var isReqOrVal = pair[2];
				
				if (isReqOrVal) {
					if (isReqOrVal == "1") {
						ctrlRef.style.color = "#FFFFFF";
						ctrlRef.style.backgroundColor = "#ff6666";
					}
					
					if (isReqOrVal == "2") {
						ctrlRef.style.backgroundColor = "#FFFF99";
						isInvalid += "I";
					}
					
					if (isReqOrVal == "3") {
						ctrlRef.style.backgroundColor = "#FFFF99";
						isInvalid += "E";
					}
					
					if (isReqOrVal == "4") {
						ctrlRef.style.color = "#FFFFFF";
						ctrlRef.style.backgroundColor = "#ff6666";
						isInvalid += "P";
					}
					
					if (isReqOrVal == "5") {
						ctrlRef.style.backgroundColor = "#FFFF99";
						isInvalid += "U";
					}
				}
				
			}
			
		} else if (ctrlType == "select-one") {
			for (var j = 0; j < ctrlRef.options.length; j++) {
				if (ctrlRef.options[j].value == pair[1])
					ctrlRef.selectedIndex = j;
			}
		}
	}
	return isInvalid;
}


function logoff()
{
	var logout = confirm("Are you sure you want to logoff? \n You will need to log in again to continue working");
	if (logout)
	{
		document.location="http://crag.columbusnova.com/logout.php";			
	}
}
