

//open image modal dialog
function openPhotoGalleryImage(docid, nodepos) {

    //set size
    $('modalWindow').style.width = 500 + "px";
    $('modalWindow').style.height = "auto";
    
    //open modal dialog
    openModalDialog();

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxlargeimage.aspx?docid=" + docid + "&nodepos=" + nodepos, "modalWindow", "");
}


//open image modal dialog addressingguidelines
function openPhotoAddressingGuidelinesImage(fileid, imgdocid) {
    //set size
    $('modalWindow').style.width = 632 + "px";
    $('modalWindow').style.height = "auto";

    //open modal dialog
    openModalDialog();

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxaddressingguidelines.aspx?fileid=" + fileid + "&imgdocid=" + imgdocid, "modalWindow", "");
}


//open modal dialog
function openModalDialog()
{
    //display div modal dialogs
	$('modalWindow').style.display = $('modalBackground').style.display = 'block';

	//special < IE7 -only processing for windowed elements, like select	
	if (window.XMLHttpRequest == null)
	{
	    if ($('hideType') != null) 
	    {
		    var type = $('hideType').value;

		    if (type == 'iframe')
			    $('modalIframe').style.display = 'block';
		    if (type == 'replace')
			    ReplaceSelectsWithSpans();
		}
	}

	//call once to center everything
	OnWindowResize();
	
	//hook events
	if (window.attachEvent)
		window.attachEvent('onresize', OnWindowResize);
	else if (window.addEventListener)
		window.addEventListener('resize', OnWindowResize, false);
	else
		window.onresize = OnWindowResize;
	
	// we won't bother with using javascript in CSS to take care keeping the window centered
	if (document.all)
		document.documentElement.onscroll = OnWindowResize;
}

//event: on window resize
function OnWindowResize()
{
	//we only need to move the dialog based on scroll position if
	//we're using a browser that doesn't support position: fixed, like < IE 7
	var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
	var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
	var div = $('modalWindow');

	//set current position
	div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
	div.style.top = Math.max((top + (GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
}

//event: on window close
function OnModalWindowClose()
{
	$('modalWindow').style.display = $('modalBackground').style.display = 'none';
	
	// special IE-only processing for windowed elements, like select	
	if (document.all)
	{
	    if ($('hideType') != null) 
	    {
		    var type = $('hideType').value;
		    alert("OK");
		    if (type == 'iframe')
			    $('modalIframe').style.display = 'none';
    		
		    if (type == 'replace')
			    RemoveSelectSpans();
		}
	}
	
	if (window.detachEvent)
		window.detachEvent('onresize', OnWindowResize);
	else if (window.removeEventListener)
		window.removeEventListener('resize', OnWindowResize, false);
	else
		window.onresize = null;
	
}

/* These functions deal with IE's retardedness in not allowing divs to 
 * cover select elements by replacing the select elements with spans */

//IE: remove select spans
function RemoveSelectSpans()
{
	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];
		
		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className != 'selectReplacement')
		{
			continue;
		}
			
		select.parentNode.removeChild(select.nextSibling);
		select.style.display = select.cachedDisplay;
	}
}

//IE: replace selects with spans
function ReplaceSelectsWithSpans()
{
	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];
		
		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className == 'selectReplacement')
		{
			continue;
		}
			
		var span = document.createElement('span');
		
		// this would be "- 3", but for that appears to shift the block that contains the span 
		//   one pixel down; instead we tolerate the span being 1px shorter than the select
		span.style.height = (select.clientHeight - 4) + 'px';
		span.style.width = (select.clientWidth - 6) + 'px';
		span.style.display = 'inline-block';
		span.style.border = '1px solid rgb(200, 210, 230)';
		span.style.padding = '1px 0 0 4px';
		span.style.fontFamily = 'Arial';
		span.style.fontSize = 'smaller';
		span.style.position = 'relative';
		span.style.top = '1px';
		span.className = 'selectReplacement';
		
		span.innerHTML = select.options[select.selectedIndex].innerHTML + 
			'<img src="custom_drop.gif" alt="drop down" style="position: absolute; right: 1px; top: 1px;" />';
		
		select.cachedDisplay = select.style.display;
		select.style.display = 'none';
		select.parentNode.insertBefore(span, select.nextSibling);
	}
}

/* utiltiy functions */

//tools: get window width
function GetWindowWidth()
{
    //calculate width
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;
	
	//return
	return width;
}

//tools: get window height
function GetWindowHeight()
{
    //calculate height
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;
  		
  	//return
  	return height;
}

//tools: get element by id
function $(id)
{
    //return
	return document.getElementById(id);
}


//open post sendtofriend modal dialog
function openSendToFriendModalDialog(url, senderemail, sendername, recipientemail, message, validate, title, recipienname) {
    //open modal dialog
    if (validate == 2) {
        advancedHeight = 460;
        advancedWidth = 611;
        openModalDialog();
        validate = 0;
    }

    //submit form
    if (validate == 1) {
        //set input values
        senderemail = document.getElementById("senderemail").value;
        sendername = escape(document.getElementById("sendername").value);
        recipientemail = document.getElementById("recipientemail").value;
        message = escape(document.getElementById("message").value.substring(0, 1100));
    }

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxsendtofriend.aspx?url=" + url + "&senderemail=" + senderemail + "&sendername=" + sendername + "&recipientemail=" + recipientemail + "&recipienname=" + recipienname + "&validate=" + validate + "&message=" + message + "&title=" + title, "modalWindow", "");
}