/* =============================================================
General JS functions
(c) Kittelberger media solutions 2006-10 whe
================================================================*/

// CSS property "display": To display TBODY elements, IE requires CSS value "block", other browsers require "table-row-group"
var cssDisplayVisible = ( (navigator.userAgent.indexOf ('MSIE') == -1) || (navigator.userAgent.indexOf ('Opera') > -1) ) ? 'table-row-group' : 'block';


/* -----------------------------------
 Open external windows ("popups")
----------------------------------- */

var POPUP_FOCUS = 1;
var POPUP_STATUSBAR = 2;
var POPUP_RESIZABLE = 4;

var POPUP_DEFAULT = 0;
var POPUP_ASSIGN_CATEGORY = 1;
var POPUP_SET_POSITION = 2;
var POPUP_EDITION_PREVIEW = 3;
var POPUP_USER_SEARCH = 4;
var POPUP_UPDATE_ADDRESS = 5;
var POPUP_OWN_DOCUMENTS = 6;

popupProfiles = new Array();
popupProfiles[POPUP_DEFAULT] = new Array ('defaultPopup', null, null, null, null);
popupProfiles[POPUP_ASSIGN_CATEGORY] = new Array ('assignCategory', 100, 100, 655, 340, POPUP_RESIZABLE);
popupProfiles[POPUP_SET_POSITION] = new Array ('setPosition', 100, 100, 650, 190, POPUP_FOCUS, POPUP_RESIZABLE);
popupProfiles[POPUP_EDITION_PREVIEW] = new Array ('editionPreview', 100, 100, 530, 640, POPUP_FOCUS + POPUP_RESIZABLE);
popupProfiles[POPUP_USER_SEARCH] = new Array ('userSearch', 100, 100, 450, 520, POPUP_FOCUS, POPUP_RESIZABLE);
popupProfiles[POPUP_UPDATE_ADDRESS] = new Array ('userAddress', 100, 100, 670, 260, POPUP_FOCUS, POPUP_RESIZABLE);
popupProfiles[POPUP_OWN_DOCUMENTS] = new Array ('ownDocuments', 100, 100, 650, 335, POPUP_FOCUS, POPUP_RESIZABLE);

function popup (whichElement, winName, winLeft, winTop, winWidth, winHeight, paramsEnable, paramsDisable) {

	if (winName == null) winName = POPUP_DEFAULT;
	if (isFinite (winName) ) {
		var profileId = winName;
		winName = popupProfiles[profileId][0];
		winLeft = popupProfiles[profileId][1];
		winTop = popupProfiles[profileId][2];
		winWidth = popupProfiles[profileId][3];
		winHeight = popupProfiles[profileId][4];
		paramsEnable = popupProfiles[profileId][5];
		paramsDisable = popupProfiles[profileId][6];
	}

	paramsEnable = (paramsEnable == null) ? 0 : paramsEnable;
	paramsDisable = (paramsDisable == null) ? 0 : paramsDisable;

	var params = ''
		+ (winLeft ? 'left=' + winLeft + ',' : '')
		+ (winTop ? 'top=' + winTop + ',' : '')
		+ (winWidth ? 'width=' + winWidth + ',' : '')
		+ (winHeight ? 'height=' + winHeight + ',' : '')
		+ ( (paramsEnable & POPUP_STATUSBAR) ? 'status=yes,' : '')
		+ ( (paramsDisable & POPUP_STATUSBAR) ? 'status=no,' : '')
		+ ( (paramsEnable & POPUP_RESIZABLE) ? 'resizable=yes,' : '')
		+ ( (paramsDisable & POPUP_RESIZABLE) ? 'resizable=no,' : '');

	winNew = window.open (whichElement, winName, params);

	if ( (paramsDisable & POPUP_FOCUS) == 0) winNew.focus();

	return (winNew ? false : true);

} // function popup


function resizeUploadPopupContent () {

	var pageContent = document.getElementById ('pageContent');
	var pageFooter = document.getElementById ('pageFooter');
	var pageFunction = document.getElementById ('pageFunctionRow');
	var pageStatus = document.getElementById ('pageStatusRow');

	var pageHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
	var pageContentHeight = pageHeight - pageContent.offsetTop - pageFooter.clientHeight - pageFunction.clientHeight - pageStatus.clientHeight;

	pageContent.style.height = pageContentHeight + 'px';

	if(window.onresize == null) window.onresize = resizeUploadPopupContent;

}

function resizePopupContent () {

	var pageContent = document.getElementById ('pageContent');
	var pageFooter = document.getElementById ('pageFooter');
	var pageHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
	var pageContentHeight = pageHeight - pageContent.offsetTop - pageFooter.clientHeight;
	pageContent.style.height = pageContentHeight + 'px';

	if(window.onresize == null) window.onresize = resizePopupContent;

} // resizePopupContent ()


function absTop (theElement) {
// Returns the absolute top position of any element

	var relTop = theElement.offsetTop;

	if (theElement.offsetParent) {
		relTop = relTop + absTop (theElement.offsetParent);
	}

	return (relTop);

} // function absTop


function absLeft (theElement) {
// Returns the absolute left position of any element

	var relLeft = theElement.offsetLeft;

	if (theElement != document.body) {
		relLeft = relLeft + absLeft (theElement.offsetParent);
	}

	return (relLeft);

} // function absLeft


function toggleSelectDetails (selElement) {
// Show/hide table content depending on selected dropdown option

	var newDisplayValue, optionDetails;

	for (var i = 0; i < selElement.length; i++) {
		newDisplayValue = (selElement.options[i].selected) ? cssDisplayVisible : 'none';
		optionDetails = document.getElementById ('tbd' + selElement.options[i].id.substr (3) ) ;
		if (optionDetails) {
			optionDetails.style.display = newDisplayValue;

			// Disable/Enable hidden form elements so that they will only be submitted when shown
			optionDetailsInputs = optionDetails.getElementsByTagName ('INPUT');
			for (var l = 0; l < optionDetailsInputs.length; l++) {
				optionDetailsInputs[l].disabled = (newDisplayValue == cssDisplayVisible) ? false : true;
			}
		}
	}

} // function toggleSelectDetails


function getCSSPropertyValue (theElement, propertyName, toNumber) {

	var propertyValue;

	if (!toNumber || toNumber == null || toNumber != true) toNumber = false

	if (document.defaultView && document.defaultView.getComputedStyle) {
		propertyValue = document.defaultView.getComputedStyle (theElement, null).getPropertyValue(propertyName);
	} else if (theElement.currentStyle) {
		propertyValue = theElement.currentStyle[propertyName];
	}

	propertyValue = (toNumber) ? Number (propertyValue.replace (/\D/g, '') ) : propertyValue;

	return (propertyValue);

}


/**
 *
 */
function showPopUpImage(lvID) {
	window.setTimeout("image("+lvID+")", 500);
}

/**
 *
 */
function image(lvID) {

	var image = document.getElementById(lvID);

	image.style.display = 'block';
	image.style.width = document.images["IMG_" + lvID].width + 2;
	image.style.height = document.images["IMG_" + lvID].height + 2;

    var availHeight;
    var availWidth;

    if(typeof(window.innerWidth) == 'number'){

		availHeight = window.innerHeight;
        availWidth = window.innerWidth;
    }
    else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){

    	availHeight = document.documentElement.clientHeight;
        availWidth = document.documentElement.clientWidth;
    }
    else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
        availHeight = document.body.clientHeight;
        availWidth = document.body.clientWidth;
    }

	var yOffset
	if (self.pageYOffset){// all except Explorer
		yOffset = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop){// Explorer 6 Strict
		yOffset = document.documentElement.scrollTop;
	}
	else if (document.body){ // all other Explorers
		yOffset = document.body.scrollTop;
	}

	var x = self.pageYOffset ? self.pageXOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollLeft : document.body ? document.body.scrollLeft : null;
	var y = self.pageYOffset ? self.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body ? document.body.scrollTop : null;

    var left = ((screen.AvailWidth/2)) - (parseInt(image.style.width));
    var top = ((screen.AvailHeight/2)+20) - (parseInt(image.style.height)/2);

	image.style.top=top;
    image.style.left=left;

}

/**
 *
 */
function hidePopUpImage(lvID) {
	var image = document.getElementById(lvID);
	image.style.display = 'none';
}
