/**
 * @author Kelly Venechanos
 */

function Popup(Caption,Text,LinkText,Link,IsModal,ajaxLink,poststring)
{
	var LinkEventText;
	if (Link==null || Link=="undefined")
	{
		LinkEventText="";
		
	}
	else
	{
		LinkEventText="onclick=\"OpenNewWindow('"+Link+"');\"";
		Caption+=" - <small><small>Click on Link Text for more.</small></small>";
	}
	var temp="<span "+LinkEventText+" style=\"border-bottom: 1px solid black;text-decoration:underline;cursor:pointer;\" onmouseover=\"javascript:popups_Popup(this,'"+Caption+"','"+Text+"',"+IsModal+","+ajaxLink+","+poststring+");\">"+LinkText+"</span>";
	//alert(temp);
	document.write(temp);
}

var PopupCaptionBackgroundColor="#357";
var PopupCaptionTextColor="#fff";
var PopupMessageBackgroundColor="palegoldenrod"; //#eec
var PopupMessageTextColor="black";
var ShadowColor="darkgray";
var ShadowXOffset=9;
var ShadowYOffset=7;


function SetupPopupAbilityOnPage()
{
	var thebody=document.body;
	var span=document.createElement("span");
	span.id="forpopups";
	thebody.appendChild(span);
	span.style.overflow="visible";
	span.style.position="absolute";
	span.style.top="0px";
	span.style.left="0px";
	span.style.height="0px";
	span.style.width="0px";
}

function popups_findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function popups_GetAbsoluteLeft(Element)
{
	var Left=0;
	return popups_findPos(Element)[0];
}

function popups_GetAbsoluteTop(Element)
{
	var Top=0;
	return popups_findPos(Element)[1];
}

function popups_SelfDestruct(Element)
{
	if (Element.parentNode)
	{
		Element.parentNode.removeChild(Element);
	}
}

function popups_SelfDestruct(Element)
{
	if (Element.parentNode)
	{
		Element.parentNode.removeChild(Element);
	}
}

function popups_setOpacity(Element,value)
{
    Element.style.opacity = value/10;
    Element.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function popups_myInfoType()
{
    var Form=null;
    var BackgroundNode=null;
	var ShadowNode=null;
    var HasIFrame=false;
	var theCaller=null;
	var IsModal=null;
}

function popups_IsIE6orOlder()
{
	if (window.XMLHttpRequest) 
	{
		// IE 7, mozilla, safari, opera 9
		return false;
	} else 
	{
		// IE6, older browsers
		return true;
	}
}

function popups_AppendRoundedTop(doc,Element,BackgroundColor)
{
  var Top=doc.createElement("b");
    Top.style.backgroundColor="transparent";
    Top.style.display="block";
	Top.style.position="relative";
 
    var Top1=doc.createElement("b");
    Top1.style.backgroundColor=BackgroundColor;
    Top1.style.display="block";
     Top1.style.height="1px";
    Top1.style.overflow="hidden";
   Top1.style.margin="0 5px";
    Top.appendChild(Top1);
	Top1.style.position="relative";
	//popups_setOpacity(Top1,8);


   var Top2=doc.createElement("b");
    Top2.style.backgroundColor=BackgroundColor;
    Top2.style.display="block";
     Top2.style.height="1px";
    Top2.style.overflow="hidden";
   Top2.style.margin="0 3px";
    Top.appendChild(Top2);
	Top2.style.position="relative";
	//popups_setOpacity(Top2,8);

   var Top3=doc.createElement("b");
    Top3.style.backgroundColor=BackgroundColor;
    Top3.style.display="block";
     Top3.style.height="1px";
    Top3.style.overflow="hidden";
   Top3.style.margin="0 2px";
    Top.appendChild(Top3);
	Top3.style.position="relative";
	//popups_setOpacity(Top3,9);

   var Top4=doc.createElement("b");
    Top4.style.backgroundColor=BackgroundColor;
    Top4.style.display="block";
     Top4.style.height="2px";
    Top4.style.overflow="hidden";
   Top4.style.margin="0 1px";
    Top.appendChild(Top4);
	Top4.style.position="relative";
    //ModalNode.appendChild(Top);
	//popups_setOpacity(Top4,9);

	Element.appendChild(Top);
}

function popups_AppendRoundedBottom(doc,Element,BackgroundColor)
{
  var Bottom=doc.createElement("b");
    Bottom.style.backgroundColor="transparent";
    Bottom.style.display="block";
 
    var Bottom1=doc.createElement("b");
    Bottom1.style.backgroundColor=BackgroundColor;
    Bottom1.style.display="block";
     Bottom1.style.height="2px";
    Bottom1.style.overflow="hidden";
   Bottom1.style.margin="0 1px";
    Bottom.appendChild(Bottom1);

   var Bottom2=doc.createElement("b");
    Bottom2.style.backgroundColor=BackgroundColor;
    Bottom2.style.display="block";
     Bottom2.style.height="1px";
    Bottom2.style.overflow="hidden";
   Bottom2.style.margin="0 2px";
    Bottom.appendChild(Bottom2);

   var Bottom3=doc.createElement("b");
    Bottom3.style.backgroundColor=BackgroundColor;
    Bottom3.style.display="block";
     Bottom3.style.height="1px";
    Bottom3.style.overflow="hidden";
   Bottom3.style.margin="0 3px";
    Bottom.appendChild(Bottom3);

   var Bottom4=doc.createElement("b");
    Bottom4.style.backgroundColor=BackgroundColor;
    Bottom4.style.display="block";
     Bottom4.style.height="1px";
    Bottom4.style.overflow="hidden";
   Bottom4.style.margin="0 5px";
    Bottom.appendChild(Bottom4);

    //ModalNode.appendChild(Bottom);
    Element.appendChild(Bottom);

}

function popups_CreatePopUp(x,y,s_caption,s_message,thecaller,IsModal,IsAjax)
{
		var doc=document;
    var HeaderBackgroundColor=PopupCaptionBackgroundColor;
	var HeaderTextColor=PopupCaptionTextColor;
    var OtherBackgroundcolor=PopupMessageBackgroundColor;//"#eec";
	var OtherTextcolor=PopupMessageTextColor;
    //var Form=doc.forms[0];
    var Form=document.getElementById("forpopups");//thecaller.parentNode;//doc.body;
    var ModalNode = doc.createElement("span");
    var BackgroundNode = doc.createElement("span");
	var ShadowNode= doc.createElement("span");
   
	if (IsModal==true)
		Form.appendChild(BackgroundNode);
	Form.appendChild(ShadowNode);
    Form.appendChild(ModalNode);
	popups_setOpacity(ShadowNode,3);

	if (IsModal==true)
	{
		BackgroundNode.style.zIndex=170;
		BackgroundNode.style.position="absolute";
		BackgroundNode.style.left="0px";
		BackgroundNode.style.top="0px";
		BackgroundNode.style.height="100%";
		BackgroundNode.style.width="100%";
		BackgroundNode.style.backgroundColor="gray";
		BackgroundNode.style.display="inline";
		BackgroundNode.innerHTML="<br>";
		popups_setOpacity(BackgroundNode,6);
    }
    ShadowNode.style.zIndex=170;
    ShadowNode.style.display="block";
    ShadowNode.style.position="absolute";
    ShadowNode.style.overflow="visible";
    ShadowNode.style.left=(x+ShadowXOffset)+"px"
    ShadowNode.style.top=(y+ShadowYOffset)+"px";
    ShadowNode.style.height="70px";
    ShadowNode.style.width="400px";

    ModalNode.style.zIndex=170;
    ModalNode.style.display="block";
    ModalNode.style.position="absolute";
    ModalNode.style.overflow="visible";
    ModalNode.style.left=x+"px"
    ModalNode.style.top=y+"px";
    ModalNode.style.height="70px";
    ModalNode.style.width="400px";

	

    ModalNode.myInfo=new popups_myInfoType();
    ModalNode.myInfo.Form=Form;
	if (IsModal==true)
		ModalNode.myInfo.BackgroundNode=BackgroundNode;
	ModalNode.myInfo.ShadowNode=ShadowNode;
	ModalNode.myInfo.IsModal=IsModal;
	ModalNode.myInfo.theCaller=thecaller;
    //ModalNode.style.border="solid 1px black";
    //ModalNode.style.backgroundColor="whitesmoke";

	popups_AppendRoundedTop(doc,ModalNode,HeaderBackgroundColor);
	popups_AppendRoundedTop(doc,ShadowNode,ShadowColor);

    var HTML="<div style=\"position:relative;color:"+HeaderTextColor+";Top:0px;Left:0px;Width:400px;Height:24px;background-Color:"+HeaderBackgroundColor+";background-repeat:repeat-x;font-size:larger;font-weight:bold;text-align:left;\">"
    HTML+="&nbsp;&nbsp;"+s_caption+"\n";
    HTML+="</div>\n";
    HTML+="<div style=\"z-index:170;position:relative;Top:25px;Left:10px;Width:380px;color:black;\">&nbsp;"+s_message+"<br>&nbsp;</div><br>";
    var Header=doc.createElement("div");
    Header.innerHTML=HTML;
    Header.style.backgroundColor=OtherBackgroundcolor;
	Header.style.color=OtherTextcolor;
    Header.style.position="relative";

	var ShadowContent=doc.createElement("div");
	HTML="<div style=\"position:relative;color:"+HeaderTextColor+";Top:0px;Left:0px;Width:400px;Height:24px;background-Color:"+ShadowColor+";background-repeat:repeat-x;font-size:larger;font-weight:bold;text-align:left;\">"
    HTML+="&nbsp;&nbsp;"+s_caption+"\n";
    HTML+="</div>\n";
    HTML+="<div style=\"z-index:170;position:relative;Top:25px;Left:10px;Width:380px;color:black;\">&nbsp;"+s_message+"<br>&nbsp;</div><br>";
	ShadowContent.innerHTML=HTML;
    ShadowContent.style.backgroundColor=ShadowColor;
	ShadowContent.style.color=OtherTextcolor;
    ShadowContent.style.position="relative";
	ShadowNode.appendChild(ShadowContent);
    ModalNode.appendChild(Header);
 //   ModalNode.innerHTML=HTML;

    var spantag=doc.createElement("div");
	//ShadowNode.appendChild(spantag);
   // ModalNode.appendChild(spantag);
    spantag.style.position="relative";
    spantag.style.display="block";
    spantag.style.left="0px";
    spantag.style.top="0px";
    spantag.style.width="100%";
    spantag.style.height="30px";
    spantag.style.textAlign="center";
    spantag.style.backgroundColor=OtherBackgroundcolor;
    //spantag.innerHTML="<br>";
   
    //var okbutton=doc.createElement("input");
    //okbutton.type="button";
    //spantag.appendChild(okbutton);
    //okbutton.value="OK";
    //okbutton.style.position="relative";
    //okbutton.style.left="170px";
    //okbutton.style.top="2px";
    //okbutton.style.width="60px";
    //okbutton.style.height="24px";
    //okbutton.focus();
   
    //okbutton.onclick=function(){return popups_ReturnPopup(ModalNode,true,null);};

	thecaller.onmouseout=function(){return popups_ReturnPopup(ModalNode,true,null);};
	ModalNode.onMouseout=function(){return popups_ReturnPopup(ModalNode,true,null);};
	
	popups_AppendRoundedBottom(doc,ModalNode,OtherBackgroundcolor);
	popups_AppendRoundedBottom(doc,ShadowNode,ShadowColor);
    ShadowNode.style.height=ModalNode.style.height;

}

function popups_ReturnPopup(ModalNode,Result,ResponseEvent)
{
    try
    {
        //popups_setOpacity(ModalNode,3);
		try
		{
	       ResponseEvent(Result);
		}
		catch (e2)
		{
		}
		if (ModalNode.myInfo.IsModal==true)
			ModalNode.myInfo.Form.removeChild(ModalNode.myInfo.BackgroundNode);
       ModalNode.myInfo.Form.removeChild(ModalNode.myInfo.ShadowNode);
       ModalNode.myInfo.Form.removeChild(ModalNode);
        return Result;
    }
    catch(e)
    {
        return false;
    }
}


function popups_Popup(ParentElement,Caption,Text,IsModal,ajaxLink,poststring)
{
	var Body=document.body;
	var x=parseInt(popups_GetAbsoluteLeft(ParentElement));
	var y=parseInt(popups_GetAbsoluteTop(ParentElement))+24;
	if (ajaxLink==null || ajaxLink=="undefined")
		popups_CreatePopUp(x,y,Caption,Text,ParentElement,IsModal);
	else
	{
		popups_CreateAjaxPopUp(x,y,Caption,Text,ParentElement,IsModal,ajaxLink,poststring);
	}
}

function OpenNewWindow(loc)
{
	window.open (loc);//, "_blank", "height=480, width=800, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=no, status=yes");
}

//this function requires Ajax.js
function popups_CreateAjaxPopUp(x,y,Caption,Text,ParentElement,IsModal,ajaxLink,poststring)
{
	var MessageText="<div style=\"width:100%;position:relative;overflow:visible;\" id=\"theresponsecontainer\"></div>"+Text;
	popups_CreatePopUp(x,y,Caption,MessageText,ParentElement,IsModal,true);
	GetPage(document.getElementById("theresponsecontainer"),ajaxLink,poststring);
}


