/* 
	global objects
*/
window.SendArticleForm = new SendArticleForm();


function showSendArticleForm(sendType){   
    var errMsgSendArticleForm = document.getElementById("errMsgSendArticleFormId");
    var sendTypeField = document.getElementById("sendTypeId"); 
    var sendArticleFormTitle = document.getElementById("sendArticleForm_title");
    var formTitleField = document.getElementById("formTitleFieldId");
    var mailMessageField = document.getElementById("mailMessageId");
    
    sendTypeField.value = sendType;

    if(sendType == "html"){
      sendArticleFormTitle.innerHTML = "Send article as mail"
      formTitleField.value = "Send article as mail";
      mailMessageField.innerHTML = "Find below the content of this web page on the IPTC website: " 
                                   + linkToArticleJs;      
    }
    else{
      sendArticleFormTitle.innerHTML = "Send article-pdf as mail"
      formTitleField.value = "Send article-pdf as mail";
      mailMessageField.innerHTML = "Find attached a PDF of the content of this web page on the IPTC website: " 
                               + linkToArticleJs;   
    } 
    
    if(errMsgSendArticleForm.innerHTML != ""){
      errMsgSendArticleForm.style.height = "0px";
      errMsgSendArticleForm.innerHTML = "";
    }

    SendArticleForm.LightboxDialog.overlay('sendArticleFormDiv');
    
    // get sessionId (only if session does not exist already)       
    var sendArticleForm = document.getElementById("sendArticleFormId");
    if(sendArticleForm.action.indexOf(";jsessionid=") == -1){
      $.get("getSession.html", {type:"xml"}, callbackSession); 
    }       
  }
  
  function callbackSession(data){ 
    // get sessionId
    var sessionId = $(data).text();     
    sessionId = jQuery.trim(sessionId);

    // load captcha image
    var captchaImg = document.getElementById("captchaImage");
    captchaImg.src = "/gfxgen;jsessionid=" + sessionId;
    
    // set sessionId in form-action
    var sendArticleForm = document.getElementById("sendArticleFormId");
    var action = sendArticleForm.action;
    var reqStr = action.split("?")[0];
    var reqQueryStr = action.substr(reqStr.length);
    sendArticleForm.action = reqStr + ";jsessionid=" + sessionId + reqQueryStr;
  }
  
  function hideSendArticleForm(){  
    SendArticleForm.LightboxDialog.overlay('sendArticleFormDiv');
  }
  
  function checkSendArticleForm(){
    var sendArticleForm = document.getElementById("sendArticleFormId");
    var recipientEmailField = document.getElementById("recipientEmailId");
    var mailSubjectField = document.getElementById("mailSubjectId");
    var mailMessageField = document.getElementById("mailMessageId");
    var gfxcodegenresField = document.getElementById("gfxcodegenresId");
    
    if(recipientEmailField.value == "" 
       || mailSubjectField.value == "" 
       || mailMessageField.value == "" 
       || gfxcodegenresField.value == ""){
 
      errMsgSendArticleForm = document.getElementById("errMsgSendArticleFormId");
      articleFormDiv = document.getElementById("sendArticleFormDiv");  
      
      errMsgSendArticleForm.style.height = "20px";
      errMsgSendArticleForm.innerHTML = "Please fill in all fields!";
    }
    else{ 
      sendArticleForm.submit();
    }    
  }
  

/* 
	Wrapper object
*/
function SendArticleForm() {
	
	var that = this;
	
	this.LightboxDialog = new LightboxDialog();
	
	function LightboxDialog() { 
		
		var that = this;
		
		this.getPageWidth = function getPageWidth() {
			var pageWidth = -1;
			if( document.body.clientWidth ) { // Firefox 
				pageWidth = document.body.clientWidth;
			}
			else if( document.body.scrollWidth > document.body.scrollWidth ) { // all but Explorer Mac 
				pageWidth = document.body.scrollWidth;
			}
			else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
				pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
			}
			if (window.innerWidth && window.innerWidth > pageWidth) {
				pageWidth = window.innerWidth; 
			}
			return pageWidth;
		}
		
		this.getPageHeight = function getPageHeight() {
		  var pageHeight = -1;
			if( window.innerHeight && window.scrollMaxY ) { // Firefox 
				pageHeight = window.innerHeight + window.scrollMaxY;
			}
			else if( document.body.scrollHeight > document.body.offsetHeight ) { // all but Explorer Mac 
				pageHeight = document.body.scrollHeight;
			}
			else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
				pageHeight = document.body.offsetHeight + document.body.offsetTop; 
			}
			if (window.innerHeight && window.innerHeight > pageHeight) {
				pageHeight = window.innerHeight; 
			}
			return pageHeight;
		}
		
		this.getScrollPos = function getScrollPos() {
			var scrollPos = 0;
			if (window.pageYOffset && window.pageYOffset > 0) { // IE  
				scrollPos = window.pageYOffset;
			} else if (document.documentElement.scrollTop && document.documentElement.scrollTop > 0) { 
	 		  scrollPos = document.documentElement.scrollTop;
			} else {
				scrollPos = document.body.scrollTop;
			}
			return scrollPos;
		}
		
		this.getElemWidth = function getElemWidth(elem) {
			return elem.offsetWidth;
		}

		this.getElemHeight = function getElemHeight(elem) {
			return elem.offsetHeight;
		}
		
		this.getWindowHeight = function getWindowHeight() {
		  var myHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myHeight = document.body.clientHeight;
		  }
		  return myHeight;
		}
		
		this.overlay = function overlay(dialogId, overlayId) {
			
			var overlayDiv = document.getElementById("overlay");
			
			if(overlayId != null)
				{
				overlayDiv = document.getElementById(overlayId);
				}
			
			var dialogDiv = document.getElementById(dialogId);
			
			overlayDiv.style.display = (overlayDiv.style.display == "block") ? "none" : "block";
			dialogDiv.style.display = (dialogDiv.style.display == "block") ? "none" : "block";
			
			if (overlayDiv.style.display == "block") {
				
				/* overlayDiv.setAttribute("onClick","SendArticleForm.LightboxDialog.overlay('"+dialogId+"')"); */
				/* this works in IE6 too ! */
				
        //overlayDiv.onclick = function () {SendArticleForm.LightboxDialog.overlay(dialogId, overlayDiv.id);}

				overlayDiv.style.width = this.getPageWidth() + "px";
				//overlayDiv.style.height = this.getPageHeight() + "px";
				overlayDiv.style.height = this.getWindowHeight() + "px";				

				var scrollPos = this.getScrollPos();	
				var topPos = (this.getWindowHeight() - this.getElemHeight(dialogDiv))/2;
				
				dialogDiv.style.top = (scrollPos + topPos) + "px";
				dialogDiv.style.left = ((this.getPageWidth()  - this.getElemWidth(dialogDiv)) / 2) + "px";
			} 
			
			// this.bugfixIE6(overlayDiv.style.visibility, "selectXY");
						
		}

		/*
		this.bugfixIE6 = function bugfixIE6(visibility, selectElemId) {
			// bugfix IE6 for drowdowns
			var selectElem = document.getElementById(selectElemId);
		
			if(document.all) {
				if (visibility == "visible") {
					// hide dropdowns
					if (selectElem != null) {
						selectElem.style.display='none';
					}
				} else {
					// show dropdowns
					if (selectElem != null) {
						selectElem.style.display='block';
					}
				}	
			}
		}

		*/


  }	// END LightboxDialog
	
	return true;
}

