function toggleDiv(divID) {
    var div = document.getElementById(divID);

    if (div.style.visibility.toLowerCase() == "hidden")
        showDiv(divID);
    else
        hideDiv(divID);
}

function showDiv(divID) {
    var div = document.getElementById(divID);

    div.style.visibility = "visible";
    div.style.display = "inline";
}

function hideDiv(divID) {
    var div = document.getElementById(divID);

    div.style.visibility = "hidden";
    div.style.display = "none";
}

function expandInvoiceDetails(img, invoiceDivID)
{
    var imgExpand = "plus.gif";
    var imgCollapse = "minus.gif";
    var invoiceDiv = document.getElementById(invoiceDivID);    
    var spacerDiv = document.getElementById("_" + invoiceDivID);    
	
	spacerDiv.style.visibility = "visible";
	spacerDiv.style.display = "inline";
	invoiceDiv.style.visibility = "visible";
	invoiceDiv.style.display = "inline";
	img.src = replaceAll(img.src.toLowerCase(), imgExpand, imgCollapse);
	img.alt = "Collapse";
}
function collapseInvoiceDetails(img, invoiceDivID)
{
    var imgExpand = "plus.gif";
    var imgCollapse = "minus.gif";
    var invoiceDiv = document.getElementById(invoiceDivID);    
    var spacerDiv = document.getElementById("_" + invoiceDivID);    
	
	spacerDiv.style.visibility = "hidden";
	spacerDiv.style.display = "none";
	invoiceDiv.style.visibility = "hidden";
	invoiceDiv.style.display = "none";
	img.src = replaceAll(img.src.toLowerCase(), imgCollapse, imgExpand);
	img.alt = "Expand";

}
function toggleInvoiceDetails(img, invoiceDivID)
{
    var invoiceDiv = document.getElementById(invoiceDivID);    

	if (invoiceDiv.style.visibility.toLowerCase() == "hidden") 
	{
	    expandInvoiceDetails(img, invoiceDivID);
	} 
	else 
	{
		collapseInvoiceDetails(img, invoiceDivID);
	}
}

function resetForm()
{
    document.forms[0].reset();
}

function updateQuantity(fieldname, increase)
{
    qtyField = document.getElementById(fieldname);
      
    var qty = 0;
    
    if (isNaN(qtyField.value))
        qty = 1; 
    else       
        qty = eval(qtyField.value) + increase;
        
    var max = 10;
    var min = 1; 
        
    if (qty < min)
        qty = min;
    
    if (qty > max)
        qty = max;
        
    qtyField.value = qty;
}

function confirmDelete()
{
    if (confirm("Are you sure you want to remove this item?") == true)
        return true;
    else
        return false;
}

function determineColorComplement(hex)
{
    var hex = hex;
    var red = 0;
    var green = 0;
    var blue = 0;
    var lumination = 0;
    var returnColor = "";
    
    if (hex.indexOf("#") >= 0)
        hex = hex.substring(1);

    red = parseInt( hex.substring(0, 2), 16);
    green = parseInt( hex.substring(2, 4), 16);
    blue = parseInt( hex.substring(4, 6), 16);
    
    lumination = ( 0.299 * red + 0.587 * green + 0.114 * blue);
	
	if (lumination > 128)
		returnColor = "black";
	else
		returnColor = "white";
		
	return returnColor;
}

function showColorPicker(fieldname, bg)
{
    
    var divId = "frameColorPicker";
    
    var frameColorPicker = document.getElementById(divId);    
	buttonElement = document.getElementById(fieldname);

    try
    {
        frameColorPicker.src += "?DateTime=" + new Date().getMilliseconds() + "&FieldName=" + fieldname + "&BackGround=" + bg;
	    frameColorPicker.style.left = getOffsetLeft(buttonElement, 40) + "px";
        frameColorPicker.style.top = ( getOffsetTop(buttonElement, 0) + buttonElement.offsetHeight + 4 ) + "px";
    }
    catch (e)
    {

    }
    
	if (frameColorPicker.style.visibility.toLowerCase() == "hidden") 
	{
		frameColorPicker.style.visibility = "visible";
		frameColorPicker.style.display = "inline";
	} 
	else 
	{
		frameColorPicker.style.visibility = "hidden";
		frameColorPicker.style.display = "none";
	}
	
	
}

function setColorPicker(fieldname, color, bg)
{    
    var field = document.getElementById(fieldname);
    field.value = color;
    
    colorizeField(fieldname, bg);

    closeColorPicker("frameColorPicker");
}

function colorizeField(fieldname, bg)
{
    var fieldname = document.getElementById(fieldname);
    var color = fieldname.value;
        
    try
    {    
        if (bg.toLowerCase() == "true")
        {
            fieldname.style.color = determineColorComplement(color);
            fieldname.style.backgroundColor = color; 
            fieldname.style.fontWeight = "normal";  
        }
        else
        {
            fieldname.style.color = color;
            if (color.toUpperCase() == "#FFFFFF")
                fieldname.style.backgroundColor = "gray";
            else
                fieldname.style.backgroundColor = "white";
                        
            fieldname.style.fontWeight = "bold";
        }
    }
    catch (e) {}
}

function closeColorPicker(winName)
{
    windowName = document.getElementById(winName);
    
	windowName.style.visibility = "hidden";
	windowName.style.display = "none";
}

function menuHover(tableID, newCellStyle, newTextStyle)
{  
    var table = document.getElementById(tableID);
    var row1 = table.getElementsByTagName("tr").item(0);
    var row2 = table.getElementsByTagName("tr").item(1);
    var row3 = table.getElementsByTagName("tr").item(2);
    
    var cellLeft = row1.getElementsByTagName("td").item(0);
    var cellTop = row1.getElementsByTagName("td").item(1);
    var cellRight = row1.getElementsByTagName("td").item(2);
    var cellMiddle = row2.getElementsByTagName("td").item(0);
    var cellBottom = row3.getElementsByTagName("td").item(0);
    
    var caption = cellMiddle.getElementsByTagName("a").item(0);
    
    cellLeft.className = newCellStyle;
    cellTop.className = newCellStyle;
    cellRight.className = newCellStyle;
    cellMiddle.className = newCellStyle;
    cellBottom.className = newCellStyle;        
    caption.className = newTextStyle;
}

function updateTextField(fieldname, defaultValue, defaultAlign, focusedAlign)
{
    var field = document.getElementById(fieldname);

    if (field.value == defaultValue)
    {
        field.value = "";
        field.style.textAlign =  focusedAlign;
    }
    else if (field.value == "")
    {
        field.value = defaultValue;
        field.style.textAlign =  defaultAlign;
    }
}

/*
function popupWindow(url, width, height, scroll)
{
    var urlParams = "?Modal=true";
    
    if (url.indexOf("?") > 0)
        urlParams = "&Modal=true";    
    
    var top = (screen.height - height) / 2;
    var left = (screen.width - width) / 2;
    
    if (window.showModalDialog)
    {
        var childWindow = window.showModalDialog(url + urlParams, window, "dialogHeight:" + (height + 25) + "px;dialogWidth:" + (width + 25) + "px;edge:Raised;center:Yes;help:No;resizable:No;status:No;scroll:No");
    }
    else
    {
        var childWindow = window.open(url, "childWindow", "height=" + height + ",width=" + width + ",scrollbars=no,resizable=no,toolbars=no,status=no,menubar=no,location=no,left=" + left + ",top=" + top);
        childWindow.focus();
    }
}

function closeWindow()
{
    var parentWindow;
				
	if (parent.window.dialogArguments)
		parentWindow = parent.window.dialogArguments;
	else if (top.opener)
		parentWindow = top.opener;
		
	parentWindow.focus();
	self.close();
}
*/

var iFrameObject; // dummy storage object for iFrame
function popupWindow(url, width, height, title, refresh)
{
	if (title == undefined)
		title = "";

	var frameName = "framePopupWindow_" + escape(url);
	
	if (refresh == undefined)
		refresh = 0;
	else
	{
		if (refresh)
			refresh = 1;
		else
			refresh = 0;
	}
	
	var redirectURL = "";
	redirectURL += "/Guardian/toolbox/popup/iframe_popup_window.aspx";
	redirectURL += "?URL=" + escape(url);
	redirectURL += "&WIDTH=" + escape(width);
	redirectURL += "&HEIGHT=" + escape(height);
	redirectURL += "&TITLE=" + escape(title);
	redirectURL += "&FRAME=" + escape(frameName);
	redirectURL += "&REFRESH=" + escape(refresh);

	var top = eval( document.body.scrollTop + height / 2);
	var left = eval( (document.body.clientWidth - width) / 2 );

	var tempIFrame = document.createElement("IFRAME");
	tempIFrame.setAttribute("id", frameName);
	tempIFrame.setAttribute("name", frameName);
	tempIFrame.style.width = width + "px";
	tempIFrame.style.height = height + "px";
	tempIFrame.style.position = "absolute";
	tempIFrame.style.top = top + "px";
	tempIFrame.style.left = left + "px";
	tempIFrame.src = redirectURL;
			
	iFrameObject = document.body.appendChild(tempIFrame);
}

function closeWindow()
{
    var parentWindow;
				
	if (parent.window.dialogArguments)
		parentWindow = parent.window.dialogArguments;
	else if (top.opener)
		parentWindow = top.opener;
		
	parentWindow.focus();
	self.close();
}
	
function redirectParent(url)
{
    var parentWindow;
				
	if (parent.window.dialogArguments)
		parentWindow = parent.window.dialogArguments;
	else if (top.opener)
		parentWindow = top.opener;
		
	parentWindow.location.href = url;
	parentWindow.focus();
	self.close();
}

function brokenItemImage(imageID)
{
	img = document.getElementById(imageID);
	img.src = "/images/no_photo.gif";
	
	var objImg = new Image();
	objImg.src = img.src;
	img.width = objImg.width;
	img.height = objImg.height;
}

function updatePreview(id, imgPath)
{				
	var fileName = extractFileName(imgPath);
	var fileExt = extractFileExt(imgPath);

	if (fileExt == "JPG" || fileExt == "JPEG")
	{
		img = document.getElementById(id);
		img.src = imgPath;

		var objImg = new Image();
		objImg.src = imgPath;
		var w = objImg.width;
		var h = objImg.height;

		if (w > h)
		{
			if (w > photoMaxWidth)
			{
				img.width = photoMaxWidth;
				img.height = (photoMaxWidth * h) / w;
			}
		}
		else
		{
			if (h > photoMaxHeight)
			{
				img.height = photoMaxHeight;
				img.width = (photoMaxHeight * w) / h;
			}			
		}
		
		return true;
	}
	else
	{
		alert("You may only upload JPEG images.");
		return false;		
	}
}
function updatePreview(imageID, golfCourseID)
{
    img = document.getElementById(imageID);
	var imageSource = img.src;
	var equals = imageSource.indexOf("=");
	var imageSource = imageSource.substring(0, equals + 1);
	img.src = imageSource + golfCourseID;
}


function extractFileName(filePath)
{
	filePath = replaceAll(filePath, "\\", "/");
	var fileName = "";
	var lastSlash = filePath.lastIndexOf("/");
	
	fileName = filePath.substring(lastSlash + 1);
	
	return fileName;
}

function extractFileExt(fileName)
{
	var fileExt = "";
	var lastPeriod = fileName.lastIndexOf(".");
	
	fileExt = fileName.substring(lastPeriod + 1).toUpperCase();
	
	return fileExt;
}

function replaceAll(text, oldChars, newChars)
{
	while ( text.indexOf(oldChars) >= 0 )
	{
		text = text.replace(oldChars, newChars);
	}
	
	return text;
}		

function getOffsetLeft(elm, parents_up) 
{
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	
	if(!parents_up) {
		parents_up = 10000; // arbitrary big number
	}
	while(parents_up > 0 && mOffsetParent) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}

	return mOffsetLeft;
}

function getOffsetTop(elm, parents_up) 
{
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	
	if(!parents_up) {
		parents_up = 10000; // arbitrary big number
	}
	while(parents_up > 0 && mOffsetParent) {
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}
	
	return mOffsetTop;
}

