// Javascript LIBRARY
// Contents: ShowHide, Clear Default Text, PopUp, eMail Validation

/////////////////////////////////
// Script: ShowHide
/////////////////////////////////
/* Instructions: Set hidden DIV controler link visible, <p><a href="#change7" onclick="toggle_visibility('change7');">CLICK to add more changes</a></p>
								</div>
								<!--END change6-->
								<div id="change7" style="display:none;"> 
*/

function resettoggle() {
var e = document.getElementById('prob');
e.style.display = 'none';
}

function toggle_visibility(prob) {

var e = document.getElementById(prob);

if(e.style.display == 'none')

e.style.display = 'block';

else

e.style.display = 'none';

}

// END ShowHide

/////////////////////////////////
// to clear default for TEXTAREA fields, add to textarea tag: onChange=applet onFocus="this.value=''"
//////////////////////////////////
 
/////////////////////////////////
// Script: Clear Default Text (for INPUT fields)
//////////////////////////////////
// functions for clearing and replacing default-text in <input> elements
// instructions: apply class="cleardefault" to any input field w/default-text

addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}
// END Clear Default Text
////////////////////////
// Script: Util-Function (clear-default-text + util function) BOTH required for reseting input fields

/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/* 
 * Kills an event's propagation and default action
 */
function knackerEvent(eventObject) {
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
    if (window.event && window.event.cancelBubble ) {
        window.event.cancelBubble = true;
    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
    if (window.event) {
        window.event.returnValue = false;
    }
}

/* 
 * Safari doesn't support canceling events in the standard way, so we must
 * hard-code a return of false for it to work.
 */
function cancelEventSafari() {
    return false;        
}

/* 
 * Cross-browser style extraction, from the JavaScript & DHTML Cookbook
 * <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
 */
function getElementStyle(elementID, CssStyleProperty) {
    var element = document.getElementById(elementID);
    if (element.currentStyle) {
        return element.currentStyle[toCamelCase(CssStyleProperty)];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(element, '');
        return compStyle.getPropertyValue(CssStyleProperty);
    } else {
        return '';
    }
}

/* 
 * CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
 * From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
 */
function toCamelCase(CssProperty) {
    var stringArray = CssProperty.toLowerCase().split('-');
    if (stringArray.length == 1) {
        return stringArray[0];
    }
    var ret = (CssProperty.indexOf("-") == 0)
              ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
              : stringArray[0];
    for (var i = 1; i < stringArray.length; i++) {
        var s = stringArray[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1);
    }
    return ret;
}

/*
 * Disables all 'test' links, that point to the href '#', by Ross Shannon
 */
function disableTestLinks() {
  var pageLinks = document.getElementsByTagName('a');
  for (var i=0; i<pageLinks.length; i++) {
    if (pageLinks[i].href.match(/[^#]#$/)) {
      addEvent(pageLinks[i], 'click', knackerEvent, false);
    }
  }
}

/* 
 * Cookie functions
 */
function createCookie(name, value, days) {
    var expires = '';
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var cookieCrumbs = document.cookie.split(';');
    var nameToFind = name + '=';
    for (var i = 0; i < cookieCrumbs.length; i++) {
        var crumb = cookieCrumbs[i];
        while (crumb.charAt(0) == ' ') {
            crumb = crumb.substring(1, crumb.length); /* delete spaces */
        }
        if (crumb.indexOf(nameToFind) == 0) {
            return crumb.substring(nameToFind.length, crumb.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}

// END util-function
// END (clear-default-text + util function) BOTH required for reseting input fields

///////////////////////////
// Script: PopUp
///////////////////////////
var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	
	closeWin();
	
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=no,location=yes, scrollbars=yes,menubar=no,width="+strWidth+",height="+strHeight+",top=auto,left=auto";
	if (type == "console") tools = "resizable=no,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=auto,top=auto";
	if (type == "scroll") tools = "resizable=no,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=auto,top=auto";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}
// END PopUp

/////////////////////////////////
//Script: eMail validation
/////////////////////////////////

// E-mail-Validation (c) Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm

function emailvalidation(entered, alertbox)
	{
	with (entered)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
			{if (alertbox) {alert(alertbox);} return false;}
		else {return true;}
	}
}

// Emptyfield-Validation (c) Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm

function emptyvalidation(entered, alertbox)
	{
	with (entered)
	{
		if (value==null || value=="")
			{if (alertbox!="") {alert(alertbox);} return false;}
		else {return true;}
	}
}

function formvalidation(thisform)
	{
	with (thisform)
	{
		if (emptyvalidation(realname,"Name required")==false) {realname.focus(); return false;};
		if (emailvalidation(mailfrom,"Valid eMail required")==false) {mailfrom.focus(); return false;};
		if (emptyvalidation(gencode,"Confirmation code required")==false) {gencode.focus(); return false;};
	}
}

function clearField(thefield) {
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
} 
// END eMail validation