/* popup window functions */

function popup(link){
    var popup = window.open(link, 'popup', 'width=750,height=310,scrollbars=yes,resizable=yes');
    popup.focus();
    return false;
}

function longPopup(link){
    var longpopup = window.open(link, 'longpopup', 'width=750,height=700,scrollbars=yes,resizable=yes');
    longpopup.focus();
    return false;
}

/* form manipulation */

// Show field when "other" is selected in handset drop-down on preregister.html
function showOtherHandsetField() {
  showElementForChoice("otherRow", "span", "handset", "-- other model --"  );
}

// Show field when "none" is selected in securitytype drop-down on manualapentry.html
function showSecurityKeyInputField() {
  showElementNotForChoice("securityKeyRow", "span", "wifiSecurityType", "none"  );
}

/**
 * elementToShowId - e.g. "preregister:otherRow"
 * elementToShowTagType - e.g. "span"
 * choiceId - e.g. preregister:handset
 * choiceAppearValue - e.g. "other"
 **/
function showElementForChoice(elementToShowId, elementToShowTagType, selectId, selectAppearValue  ) {
  appearingObj = findTag(elementToShowTagType, elementToShowId );
  selectObj = findTag("select", selectId);
  if (appearingObj && selectObj && (selectObj.options[selectObj.selectedIndex].text.toLowerCase() == selectAppearValue)) {
    removeClass(appearingObj, 'hidden');
    addClass(appearingObj, 'row');
    return;
  }
  else {
    removeClass(appearingObj, 'row');
    addClass(appearingObj, 'hidden');
    return;
  }
}

/**
 * elementToShowId - e.g. "preregister:otherRow"
 * elementToShowTagType - e.g. "span"
 * choiceId - e.g. preregister:handset
 * choiceAppearValue - e.g. "other"
 **/
function showElementNotForChoice(elementToShowId, elementToShowTagType, selectId, selectAppearValue  ) {
  appearingObj = findTag(elementToShowTagType, elementToShowId );
  selectObj = findTag("select", selectId);
  if (appearingObj && selectObj && (selectObj.options[selectObj.selectedIndex].text.toLowerCase() != selectAppearValue)) {
    removeClass(appearingObj, 'hidden');
    addClass(appearingObj, 'row');
  }
  else {
    removeClass(appearingObj, 'row');
    addClass(appearingObj, 'hidden');
  }
}

/**
 * Find a tag by a combination of tag type and the tail of it's id.
 * Useful for finding jsf id tags where the id is generate dynamically but the ending of 
 * the id is known.
 **/
function findTag(tagName, idTail ) {
  var tags = document.getElementsByTagName(tagName);
    for (var i = 0; i < tags.length; i++) {
        if (tags[i].id !== null && tags[i].id.indexOf(idTail) > -1) {
            return tags[i];
        }
    }
    return null;
}

/**
 * Find tags by a combination of tag type and the tail of name.
 * Useful for finding jsf name tags where the name is generate dynamically but the ending of 
 * the name is known.
 * Returns an array.
 **/
function findTagsByName(tagName, nameTail ) {
  var tags = document.getElementsByTagName(tagName);
  var foundTags = new Array();
  var foundLength = 0;

    for (var i = 0; i < tags.length; i++) {
        if (tags[i].name !== null && tags[i].name.indexOf(nameTail) > -1) {
            foundTags[foundLength] = tags[i];
            foundLength++;
        }
    }
    return foundTags;
}

function toggle(id) {
  var elem = document.getElementById(id);
  if (elem.style.display == 'none') { elem.style.display = ''; }
  else { elem.style.display = 'none'; }
}

/* class modifying functions */

function togglePanel(clickObj, toggleObjID, offText, onText, className) {
  toggleObj = document.getElementById(toggleObjID);
  toggleClass(toggleObj, className);
  if ( typeof clickObj.value != 'undefined'){
	  clickObj.value = (hasClass(toggleObj,className)) ? onText : offText;
	  setCookie(toggleObjID, clickObj.value, 30);
  }
  else{
	  clickObj.innerHTML = (hasClass(toggleObj,className)) ? onText : offText;
	  setCookie(toggleObjID, clickObj.innerHTML, 30);
  }
}

function toggleClass(domObj, className) {
  if (hasClass(domObj, className)) removeClass(domObj, className);
  else addClass(domObj, className);
}

function addClass(obj, className) { // adds the passed className to the passed DOm object
  if (obj && obj.id) obj.className = (obj.className == '') ? className : obj.className + ' ' + className;
}

function removeClass(obj, className) { // replaces the passed classname with an empty string
  if (obj && obj.id) obj.className = obj.className.replace(new RegExp('^'+className+'| '+className),'');
}

function hasClass(obj, className) {
  // function returns true if the class for the passed DOM objct contains the passed class
  return (obj && obj.id) ? new RegExp('^'+className+'| '+className).test(obj.className) : false;
}

function toggleControl(clickObj, toggleObjID, offText, onText, className){
	var value=getCookie(toggleObjID);
	if (value && value==offText){
		togglePanel(clickObj, toggleObjID, offText, onText, className);
	}
}

/* cookie functions */

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) { return null; }
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' + ( ( path ) ? ';path=' + path : '') + ( ( domain ) ? ';domain=' + domain : '' ) + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
