/* ***********************************************************
Example 4-3 (DHTMLAPI.js)
"Dynamic HTML:The Definitive Reference"
2nd Edition
by Danny Goodman
Published by O'Reilly & Associates  ISBN 1-56592-494-0
http://www.oreilly.com
Copyright 2002 Danny Goodman.  All Rights Reserved.
************************************************************ */
// DHTMLapi.js custom API for cross-platform
// object positioning by Danny Goodman (http://www.dannyg.com).
// Release 2.0. Supports NN4, IE, and W3C DOMs.

// Global variables
var isCSS, isW3C, isIE4, isNN4, isIE6CSS;
// Initialize upon load to let all browsers establish content objects
function initDHTMLAPI() {
    if (document.images) {
        isCSS = (document.body && document.body.style) ? true : false;
        isW3C = (isCSS && document.getElementById) ? true : false;
        isIE4 = (isCSS && document.all) ? true : false;
        isNN4 = (document.layers) ? true : false;
        isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    }
}
// Set event handler to initialize API
window.onload = initDHTMLAPI;

// Seek nested NN4 layer from string name
function seekLayer(doc, name) {
    var theObj;
    for (var i = 0; i < doc.layers.length; i++) {
        if (doc.layers[i].name == name) {
            theObj = doc.layers[i];
            break;
        }
        // dive into nested layers if necessary
        if (doc.layers[i].document.layers.length > 0) {
            theObj = seekLayer(document.layers[i].document, name);
        }
    }
    return theObj;
}

// Convert object name string or object reference
// into a valid element object reference
function getRawObject(obj) {
    var theObj;
    if (typeof obj == "string") {
        if (isW3C) {
            theObj = document.getElementById(obj);
        } else if (isIE4) {
            theObj = document.all(obj);
        } else if (isNN4) {
            theObj = seekLayer(document, obj);
        }
    } else {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

// Convert object name string or object reference
// into a valid style (or NN4 layer) reference
function getObject(obj) {
    var theObj = getRawObject(obj);
    if (theObj && isCSS) {
        theObj = theObj.style;
    }
    return theObj;
}

// Position an object at a specific pixel coordinate
function shiftTo(obj, x, y) {
    var theObj = getObject(obj);
    if (theObj) {
        if (isCSS) {
            // equalize incorrect numeric value type
            var units = (typeof theObj.left == "string") ? "px" : 0 
            theObj.left = x + units;
            theObj.top = y + units;
        } else if (isNN4) {
            theObj.moveTo(x,y)
        }
    }
}

// Move an object by x and/or y pixels
function shiftBy(obj, deltaX, deltaY) {
    var theObj = getObject(obj);
    if (theObj) {
        if (isCSS) {
            // equalize incorrect numeric value type
            var units = (typeof theObj.left == "string") ? "px" : 0 
            theObj.left = getObjectLeft(obj) + deltaX + units;
            theObj.top = getObjectTop(obj) + deltaY + units;
        } else if (isNN4) {
            theObj.moveBy(deltaX, deltaY);
        }
    }
}

// Set the z-order of an object
function setZIndex(obj, zOrder) {
    var theObj = getObject(obj);
    if (theObj) {
        theObj.zIndex = zOrder;
    }
}

// Set the background color of an object
function setBGColor(obj, color) {
    var theObj = getObject(obj);
    if (theObj) {
        if (isNN4) {
            theObj.bgColor = color;
        } else if (isCSS) {
            theObj.backgroundColor = color;
        }
    }
}

// Set the visibility of an object to visible
function show(obj) {
    var theObj = getObject(obj);
    if (theObj) {
        theObj.visibility = "visible";
    }
}

// Set the visibility of an object to hidden
function hide(obj) {
    var theObj = getObject(obj);
    if (theObj) {
        theObj.visibility = "hidden";
    }
}

// Retrieve the x coordinate of a positionable object
function getObjectLeft(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView) {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("left");
    } else if (elem.currentStyle) {
        result = elem.currentStyle.left;
    } else if (elem.style) {
        result = elem.style.left;
    } else if (isNN4) {
        result = elem.left;
    }
    return parseInt(result);
}

// Retrieve the y coordinate of a positionable object
function getObjectTop(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView) {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("top");
    } else if (elem.currentStyle) {
        result = elem.currentStyle.top;
    } else if (elem.style) {
        result = elem.style.top;
    } else if (isNN4) {
        result = elem.top;
    }
    return parseInt(result);
}

// Retrieve the rendered width of an element
function getObjectWidth(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (elem.offsetWidth) {
        result = elem.offsetWidth;
    } else if (elem.clip && elem.clip.width) {
        result = elem.clip.width;
    } else if (elem.style && elem.style.pixelWidth) {
        result = elem.style.pixelWidth;
    }
    return parseInt(result);
}

// Retrieve the rendered height of an element
function getObjectHeight(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (elem.offsetHeight) {
        result = elem.offsetHeight;
    } else if (elem.clip && elem.clip.height) {
        result = elem.clip.height;
    } else if (elem.style && elem.style.pixelHeight) {
        result = elem.style.pixelHeight;
    }
    return parseInt(result);
}

// Return the available content width space in browser window
function getInsideWindowWidth() {
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (isIE6CSS) {
        // measure the html element's clientWidth
        return document.body.parentElement.clientWidth
    } else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    }
    return 0;
}

// Return the available content height space in browser window
function getInsideWindowHeight() {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (isIE6CSS) {
        // measure the html element's clientHeight
        return document.body.parentElement.clientHeight
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
    return 0;
}
// Show dynamic menu objects alle
	function show(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
	if (d) {d.style.display='block';}
	}	
// Show dynamic menu objects - disable
	function show_2(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
	if (d) {d.style.display='none';}
	}	
// Show user Agent
    function sayHello(l)  {
	var isW3C = (document.getElementById) ? true : false;
	if (isW3C != false) {
	alert(navigator.userAgent + "   PROBLEM"); 
	if (l==1) {window.location="../Admin/admin.html?more=not_W3C&";}
	if (l==2) {window.location="../../Admin/admin.html?more=not_W3C&";}
	if (l==3) {window.location="../../../Admin/admin.html?more=not_W3C&";}
	if (l==4) {window.location="../../../../Admin/admin.html?more=not_W3C&";}
	else {window.location="./Admin/admin.html?more=not_W3C&";}
	}
	else {alert(navigator.userAgent + "   OK");}
	}
	
// Show user Agent Confirmation	  

   function getUser() {
   var result = navigator.userAgent;
   var reg0=/-/g;
   var reg1=/;/g;
   var reg2=/\(/g;
   var reg3=/\)/g;
   result=result.replace(reg1, " ");
   result=result.replace(reg2, " ");
   result=result.replace(reg3, " ");
   // alert(result);
   return result;
   }   	

    function sayOops()  {
	var isW3C = (document.getElementById) ? true : false;
	if (isW3C == false) {
	if (confirm("Sie benutzen eine nicht zur W3C-Norm kompatible Browser-Version! \n Klicken Sie OK, wenn Sie zu diesem Thema mehr Hilfe benötigen.") ) {
	window.location="./Admin/help.html?more=not_W3C&";} }
	// else {alert(navigator.userAgent + " \n Browser is OK");}
	}
	
// Start Page

    function startPage()  { 
	show();
	sayOops();
	}

/* identString - Reads the search string to figure out what link
//             brought it here.
*/ 
function identString() {
/* 
// start by storing our search string in a handy place (so we do not
// need to type as much)
*/ 
   var inputString = window.location.search;
// 
// find the beginning of our special URL variable
// 
   var startOfSource = inputString.indexOf("from=");
   var startOfmessage = inputString.indexOf("more=");
// 
// if it is there, find the end of it
// 
   if (startOfSource != -1) {
      var endOfSource = inputString.indexOf("&",startOfSource + 5);
      var result = inputString.substring(startOfSource + 5,
                                         endOfSource);
   }
   else
      var result = "Source Unknown";
   return result;
}
 
// identMessage - Reads the search string to figure out what type of
//             message has been sent.
// 
function identMessage() {
//  
// start by storing our search string in a handy place (so we do not
// need to type as much)
// 
   var inputMessage = window.location.search;
// 
// find the beginning of our special URL variable
// 
   var startOfmessage = inputMessage.indexOf("more=");
// 
// if it is there, find the end of it
// 
   if (startOfmessage != -1) {
      var endOfmessage = inputMessage.indexOf("&",startOfmessage + 5);
      var result = inputMessage.substring(startOfmessage + 5,
                                         endOfmessage);
   }
   else
      var result = "Message Unknown";
   return result;
}


//--- Form tests ---------------------

// Check for phone number: look for [0-9], [/] and [-] 

function isPhone(inputString) {
var elementstr = inputString.value + "";
    if (elementstr == "") {
	  		setColor(inputString, bgBad);
	        return false; }
    for ( var i=0; i<elementstr.length; i++ ) {
   			if (( elementstr.charAt(i) < "0" && elementstr.charAt(i) != "-" ) || 
			    ( elementstr.charAt(i) > "9" && elementstr.charAt(i) != "-" )) 
			{
			setColor(inputString, bgBad);
   		    return false; 
		    }  else {
   		    setColor(inputString, bgGood);
            return true; 
			}
		}
}

// Check for date: look for [0-9] and [/] 

function isDate(inputString) {
        var z1="";      // days
        var z2="";     // 1 Trennzeichen 
        var z3="";     // tenthmonths
        var z4="";			// months					 
        var z5="";	  // 2 Trennzeichen
		var z6="";     // y0
        var z7="";     // y1
        var z8="";      // y2
        var z9="";		// y3						
        var z0="";	    // tenthdays
var elementstr = inputString.value + "";
    z2=elementstr.substring(2,3);
 	z5=elementstr.substring(5,6);
	z0=elementstr.substring(0,2);
	z3=elementstr.substring(3,5);
	z6 = elementstr.substring(6,10);
	// alert(z0+" + "+z2+" + "+z3+" + "+z5+" + "+z6);
    if ((elementstr == "") || (z2 != "/") || (z5 != "/") || (z0 < "01") || (z0 > "31") || (z3 < "01") || (z3 > "12") || (z6 < "2003") || (z6 > "2009") ) {
	  		setColor(inputString, bgBad);
	        return false; }
    for ( var i=0; i<elementstr.length; i++ ) {
   			if (( elementstr.charAt(i) < "0" && elementstr.charAt(i) != "/" ) || 
			    ( elementstr.charAt(i) > "9" && elementstr.charAt(i) != "/" )) 
			{
			setColor(inputString, bgBad);
   		    return false; 
		    }  else {
   		    setColor(inputString, bgGood);
            return true; 
			}
		}
}
// Check for alpha string: look for [a-zA-Z], blank and [-] 

function isAlpha(inputString) {
var elementstr = inputString.value + "";
         for ( var i=0; i<elementstr.length; i++ ) {
   			if (( elementstr.charAt(i) >= "0" && elementstr.charAt(i) <= "9") 
			// || ( elementstr.charAt(i) < "A" && elementstr.charAt(i) != "\b")   
			// || ( elementstr.charAt(i) < "A" && elementstr.charAt(i) != "-")
				) 
			{
   		setColor(inputString, bgBad);
		return false;
			}
		}
    setColor(inputString, bgGood);
	return true;
}

// Check for email address: look for [@] and [.] 



function isEmail(inputString) { 					//test if e-mail address was properly formed
		var x1="";
        var x2="";
        var x3="";
        var rest="";								// the min. format is x@x.x
        var string4="";						        // no two @ allowed  - any number of periods
	string4 = string4+inputString.value;			//in front of and behing of @ accepted
	    // alert(string4);
	if (inputString.value.indexOf('@') == "-1" ||    //at least one comercial at mus tbe present
	    inputString.value.indexOf(',') >= "0" ||     //no , allowed
		inputString.value.indexOf(' ') >= "0" ||     //no blanks allowed
		inputString.value == "" )	{				 //the field must be filled in 
	    setColor(inputString, bgBad);
		return false;						 //one of the above condition list was faulty
   }	else { 
		x1=inputString.value.indexOf('@');
		//alert("The position is  "+x1);
		x2=inputString.value.indexOf('.');
		//alert("The position is  "+x2);
		var rest=string4.substring(string4.indexOf('@')+1,string4.length); // extract rest after @
		//alert('The Rest is  ' + rest);
		}
	if (rest.indexOf('@') >= "0" ||  		//no second @ allowed
		rest.indexOf('.') == "-1" )	{		//at least one period should be present
	    setColor(inputString, bgBad);
		return false;
		}  else {								//e-mail address should not start with @
		x3=rest.indexOf('.');				//period just next to @ is wrong
		//alert("The position is  "+x3);
		if (x1=="0" || x3=="0" || (rest.charAt(rest.length -1)==".")) {
	    setColor(inputString, bgBad);
		return false;  					//at least one char after period
	}   else {
	    setColor(inputString, bgGood);
	    return true;
		} 
		setColor(inputString, bgGood);
		return true;						//now we have it!
        }
}

// Check for not null and for not empty field

function isFilled(inputString) {
   if (inputString.value == "" || inputString.value == null) {
    setColor(inputString, bgBad);
	return false;
	}
    else 
	{
	setColor(inputString, bgGood);
	return true;
    }
}

// Check for null and for empty field

function isEmpty(inputString) {
    if (inputString.value == "" || inputString.value == null) 
    return true;
    else return ;
}

// set field color

var bgBad = "#ffcccc";
var bgGood = "white";
function setColor(el, bg) {
  if (el.style) el.style.backgroundColor = bg;
}

//check the text area field
function checkLimit(field, limit) {
   if (field.value.indexOf(';') != "-1" || field.value.length > limit-1)
   {
   setColor(field, bgBad);
   alert ("The length of this field is limited to "+limit+" characters!");
   var cutfield = field.value.slice(0,limit-1);
   field.value = cutfield;
   field.focus();
   return false;
   } else {
          setColor(field, bgGood);
		  return true;
		  }
}


// --------------- Ende ----------------
