﻿/****
* "Toggles" the DIV-visibility by moving out
*/
function toggleDiv(divID) {
	var elem = document.getElementById(divID);
	if (elem.style.position != 'absolute') {
		elem.style.position = 'absolute';
		elem.style.left = '-1000px';
	}
	else {
		elem.style.position = 'relative';
		elem.style.left = '0px';
	}
}
/****
* Stripe a table adapting css-classes
* on all rows and cells without class-attribute
* or background-color
*
* pass as arguments:
* arg[0] id of element with table to stripe
* further argument are optional:
* arg[1] class-name for even rows (else defaults to 'evenRow'),
* arg[2] class-name for odd rows (else defaults to 'oddRow'),
* arg[3] class-name for header row (else defaults to 'headerRow', applied on caption)
*
* NOTE: script fits table-layout-definition of mgbahn
*/
function stripe(id) {
	var even = false;
	var hiddenHeader = false;

	// if arguments are provided to specify the classes
	// of the even & odd rows and the header(caption) row, then use them;
	// otherwise use the following defaults:
	var evenClassRow = arguments[1] ? arguments[1] : "evenRow";
	var oddClassRow = arguments[2] ? arguments[2] : "oddRow";
	var headerClassRow = arguments[3] ? arguments[3] : "headerRow";

	// obtain a reference to the desired table
	// if no such table exists, abort
	var table = document.getElementById(id);
	if (!table) { return; }

	//if there's a caption, the headerRow-class is applied
	//if the caption has a class (hidden), set the hiddenHeader flag
	//ASSUMPTION: only one caption is defined in table
	var caption = table.getElementsByTagName("caption");
	if (caption.length > 0) {
		if (!hasClass(caption[0])) {
			setStyleClass(caption[0], headerClassRow);
		}
		else {
			hiddenHeader = true;
		}
	}

	//stripe header
	//1.header-row with header-data is first row to stripe
	//IF a caption with a class is present (hidden), the first gets the headerRowClass
	//ASSUMPTION: max. one head is defined in table
	var theads = table.getElementsByTagName("thead");
	if (theads.length > 0) {
		var trs = theads[0].getElementsByTagName("tr");
		for (var i = 0; i < trs.length; i++) {
			//if a hiddenHeader is present, set first header-row as header
			//else stipe it like a normal row
			if (hiddenHeader) {
				setThisRowCellsStyleClasses(trs[i], headerClassRow);
			}
			else {
				//define style-class for this row
				var styleClass = even ? evenClassRow : oddClassRow;
				//set style-class on all TDs and THs
				setThisRowCellsStyleClasses(trs[i], styleClass);
				//flip even
				even = !even;
			}
		}
	}	
	
	//stripe body, depending on header
	//ASSUMPTION: max. one body is defined in table
	var tbodies = table.getElementsByTagName("tbody");
	if (tbodies.length > 0) {

		var trs = tbodies[0].getElementsByTagName("tr");
		for (var i = 0; i < trs.length; i++) {
			//define style-class for this row
			var styleClass = even ? evenClassRow : oddClassRow;
			//set style-class on all TDs and THs
			setThisRowCellsStyleClasses(trs[i], styleClass);
			//flip even
			even = !even;
		}
	}

}
/****
* Loops through a row and calls funtion "setStyleClass" for each cell
* to set a style-class-name on the cell
*
* arg[0] row to loop
* arg[1] style-class-name to set
*/
function setThisRowCellsStyleClasses(row, styleClass) {
	// avoid rows that have a class attribute or backgroundColor style
	if (!hasClass(row) && !row.style.backgroundColor) {
		//loop through all cells in this row
		for (var j = 0; j < row.childNodes.length; j++) {
			//only child with a valid tagname (TD or TH) must be processed
			if (row.childNodes[j].tagName != null) {
				// avoid cells that have a class attribute or backgroundColor style
				if (!hasClass(row.childNodes[j]) && !row.childNodes[j].style.backgroundColor) {
					setStyleClass(row.childNodes[j], styleClass);
				}
			}
		}
	}
}
/****
* Append a style-class on an element 
*
* arg[0] element to set class on
* arg[1] style-class-name to set
*/
function setStyleClass(elem, stClass) {
	var classAttr = document.createAttribute("class");
	classAttr.nodeValue = stClass;
	elem.setAttributeNode(classAttr);
}
/****
* Checks if object has a style-class
*/
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}

/****
* Adjusts the div-margin for displaying amount-total depending on baskettable-cellwidth
*
* arg[0] id of the basket table
* arg[1] id of the div displaying the basket total
*/
function adjustTotalLayout(basketTableName, divName) {
	var tbl = document.getElementById(basketTableName);
	var lastCellWidth = 0;
	var theads = tbl.getElementsByTagName("thead");
	if (theads.length > 0) {
		var trs = theads[0].getElementsByTagName("tr");
		if (trs.length > 0) {
			var ths = trs[0].getElementsByTagName("th");
			if (ths.length > 0) {
				lastCellWidth = ths[ths.length - 1].offsetWidth;
			}
		}
	}
	var tBox = document.getElementById('totalDiv');
	var currentPadR = tBox.style.paddingRight.replace("px", "");
	tBox.style.paddingRight = (parseInt(lastCellWidth) + parseInt(currentPadR)) + "px";
}


/****
* The Class manages the display of pupups to short longer texts.  
*/
function ClassShortFormTextPupUpBox() {
    /*private fields*/
    var IE;
    var x;
    var y;
    var CssClass;
    var style;
    var tagName;
    var innerText;
    var ID;
    var textBoxPupUp;
    var isInnerHtml;
    /*****on start section****/
    ObjectInit();
    /*****private methods*****/
    /*Set the unchangable part of the syle tag*/
    function ConstantSyleParamiters() {
        var strPosition = "absolute"; 
        if (IE) {
            strPosition = "fixed"
        }
        return "position:" + strPosition + ";display:none;z-index:1000;"; 
    }
    /*is fierd on class initialization*/
    function ObjectInit() {
        IE = document.all ? true : false;
        isInnerHtml = false;
        x = 0;
        y = 0;
        CssClass = "";
        style = ConstantSyleParamiters();
        tagName = "div";
        innerText = "";
        ID = "aseanticShotFormTextPupUp";
        /*init the mouse move event*/
        if (!IE) document.captureEvents(Event.MOUSEMOVE)
        document.onmousemove = GetMauseMouseCoordinates;
        /*Creates the pupup element if it doest exist yet*/
        CreatePupupBoxIfNotExixst();
    }
    /*check if the elemnt is null or empty*/
    function IsNullOrEpty(element) {
        try {
            if (element == null || element == "undefined")
                return true;
            return false;
        }
        catch (e) {
            return true;
        }
    }
    /*Creates the pupup element if it doest exist yet*/
    function CreatePupupBoxIfNotExixst() {
        /*if element doesntExist yet*/
        if (IsNullOrEpty(document.getElementById(ID))) {
            textBoxPupUp = document.createElement(tagName);
            textBoxPupUp.setAttribute("id", ID);
            /*add the pupupbox to the document*/
            document.body.insertBefore(textBoxPupUp, document.body.lastChild);
        }
        else {
            textBoxPupUp = document.getElementById(ID);
        }
    }
    /*get the current mouse cordinations*/
    function GetMauseMouseCoordinates(e) {
        if (IE) { // falls IE
            x = event.clientX + document.body.scrollLeft;
            y = event.clientY + document.body.scrollTop;
        }
        else {  // falls NS
            x = e.pageX;
            y = e.pageY;
        }
        if (x < 0) {
            x = 0;
        }
        if (x < 0) {
            y = 0;
        }
    }
    /*****class properties******/
    /*set TheElemnts Css Class*/
    this.SetCssClass = function(Class) {
        if (!IsNullOrEpty(Class)) {
            CssClass = Class;
        }
    }
    /*gets the cssclass*/
    this.GetCssClass = function() {
        if (!IsNullOrEpty(CssClass)) {
            return CssClass;
        }
        return "";
    }
    /*append the given style to the style tag*/
    this.SetStyle = function(styleText) {
        if (!IsNullOrEpty(styleText)) {
            style += styleText;
        }
    }
    /*Get the syle*/
    this.GetStyle = function() {
        if (!IsNullOrEpty()) {
            return style;
        }
        return "";
    }
    /*sets the inner text of the pupupbox*/
    this.SetInnerText = function(txt) {
        if (!IsNullOrEpty(txt)) {
            innerText = txt;
        }

    }
    /*sets the inner text of the pupupbox*/
    this.GetInnerText = function() {
        if (!IsNullOrEpty(innerText)) {
            return innerText;
        }
        return "";
    }
    /*set the isInnerHtml value*/
    this.SetIsInnerHtml = function(value) {
        if (!IsNullOrEpty(value)) {
            isInnerHtml = value;
        }
        else
            isInnerHtml = false;
    }
    /*Gets the isInnerHtml value*/
    this.GetIsInnerHtml = function() {
        if (!IsNullOrEpty(isInnerHtml)) {
            return isInnerHtml;
        }
        return false;
    }

    /*****privilaged function*****/
    /*init the pupup box properties and values*/
    this.InitPupupBox = function() {

        if (!IsNullOrEpty(textBoxPupUp)) {
            //to do must taken away
            textBoxPupUp.setAttribute("class", CssClass);
            textBoxPupUp.setAttribute("style", style);
            if (isInnerHtml) {
                textBoxPupUp.innerHTML = innerText;
            }
            else
                textBoxPupUp.innerText = innerText;
        }

    }
    /*decode the html*/
    this.HtmlDecode = function (text) {
        text = text.replace("&amp;", "&");
        text = text.replace("&lt;", "<");
        text = text.replace("&gt;", ">");
        text = text.replace("&quot;", "\"");
        text = text.replace("&apos;", "'");
        return text;
    }
    this.Show = function(text) {
        this.SetInnerText(text);
        this.InitPupupBox();
        textBoxPupUp.style.top = y + "px";
        textBoxPupUp.style.left = x + "px";
        textBoxPupUp.style.display = "block";
    }
    this.Hidd = function() {
        this.SetInnerText("");
        this.InitPupupBox();
        textBoxPupUp.style.top = 0 + "px";
        textBoxPupUp.style.left = 0 + "px";
        textBoxPupUp.style.display = "none";
        textBoxPupUp.style.zIndex = "10000";
    }    
}


