/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | AMCS Website Version 0.0                                                  |
// +---------------------------------------------------------------------------+
// | functions.js                                                              |
// |                                                                           |
// | AMCS function library.                                                    |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2006 by the following authors:                              |
// |                                                                           |
// | Authors: Ricardo Ortiz - ricortiz@gmail.com                               |
// |                                                                           |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | as published by the Free Software Foundation; either version 2            |
// | of the License, or (at your option) any later version.                    |
// |                                                                           |
// | This program is distributed in the hope that it will be useful,           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
// | GNU General Public License for more details.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+
// | See the docs/install.html and docs/config.html files for more information |
// | on configuration.                                                         |
// +---------------------------------------------------------------------------+
//
// $Id: functions.js 124 2006-09-21 19:24:54Z rortizro $

var arImages=new Array(); 
function browser() {
    var b = navigator.appName;
    if (b=="Netscape"){ this.b = "ns"; }
    else this.b = b;
    this.version = navigator.appVersion;
    this.v = parseInt(this.version);
    this.ns = (this.b=="ns" && this.v>=5);
    this.op = (navigator.userAgent.indexOf('Opera')>-1);
    this.safari = (navigator.userAgent.indexOf('Safari')>-1);
    this.op7 = (navigator.userAgent.indexOf('Opera')>-1 && this.v>=7);
    this.ie56 = (this.version.indexOf('MSIE 5')>-1||this.version.indexOf('MSIE 6')>-1);
    this.iewin = (this.ie56 && navigator.userAgent.indexOf('Windows')>-1);
    this.iemac = (this.ie56 && navigator.userAgent.indexOf('Mac')>-1);
    this.moz = (navigator.userAgent.indexOf('Mozilla')>-1);
    this.moz13 = (navigator.userAgent.indexOf('Mozilla')>-1 && navigator.userAgent.indexOf('1.3')>-1);
    this.oldmoz = (navigator.userAgent.indexOf('Mozilla')>-1 && navigator.userAgent.indexOf('1.4')>-1 || navigator.userAgent.indexOf('Mozilla')>-1 && navigator.userAgent.indexOf('1.5')>-1 || navigator.userAgent.indexOf('Mozilla')>-1 && navigator.userAgent.indexOf('1.6')>-1);
    this.ns6 = (navigator.userAgent.indexOf('Netscape6')>-1);
    this.docom = (this.ie56||this.ns||this.iewin||this.op||this.iemac||this.safari||this.moz||this.oldmoz||this.ns6);
}

function Preload()
{
    var temp = Preload.arguments;
    for(i=0; i < temp.length; i++) 
    {
	arImages[i]=new Image();
	arImages[i].src=Preload.arguments[i];
    }
}

function validate()
{
    if (document.form1.yourname.value.length<3)
    {
	   alert("Please enter your full Forname");
	   document.form1.yourname.focus();
	   return false;
    }
    if (document.form1.yoursirname.value.length<3)
    {
	   alert("Please enter your full Sirname.");
	   document.form1.yoursirname.focus();
	   return false;
    }
    if (document.form1.addy.value.length < 5)
    {
	   alert("Please enter a complete email address in the form: yourname@yourdomain.com")
	   document.form1.addy.focus();
	   return false;
    }
    var addystring = document.form1.addy.value;
    var ampIndex = addystring.indexOf("@");
    var afterAmp = addystring.substring((ampIndex + 1), addystring.length);
    var dotIndex = afterAmp.indexOf(".");
    var dotIndex = dotIndex + ampIndex + 1;
    var afterAmp = addystring.substring((ampIndex + 1), dotIndex);
    var afterDot = addystring.substring((dotIndex + 1), addystring.length);
    var beforeAmp = addystring.substring(0,(ampIndex));
    var addy_regex ="^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-\.]+$";
    if ((addystring.indexOf("@") != "-1") && (addystring.length > 5) &&
	       (afterAmp.length > 0) && (beforeAmp.length > 1) &&
	       (afterDot.length > 1) && (addy_regex.test(addystring)) ) 
    {
	   return true;
    }
    else
    {
	   alert("Invalid! Please enter a complete email address in the form: yourname@yourdomain.com")
	   document.form1.addy.focus();
	   return false;
    }
}

function showBlockToggle(varname1,varname2,blocktitle,element_id,element_tag,showfirst) {
  if (document.createTextNode) {
    // Uses DOM calls to avoid document.write + XHTML issues

    var linkHolder = document.getElementById(blocktitle)
    if (!linkHolder) return;

    var outerSpan = document.createElement('span');
    outerSpan.className = 'blocktoggle';

    var toggleLink = document.createElement('a');
    toggleLink.id = 'togglelink'+element_id;
    toggleLink.className = 'internal';
    toggleLink.href = 'javascript:toggleHide('+varname1+','+varname2+')';
    toggleLink.appendChild(document.createTextNode(blockHideText));

    outerSpan.appendChild(document.createTextNode('['));
    outerSpan.appendChild(toggleLink);
    outerSpan.appendChild(document.createTextNode(']'));

    linkHolder.appendChild(document.createTextNode(' '));
    linkHolder.appendChild(outerSpan);

//     var cookiePos = document.cookie.indexOf("hideblock=");
//     if (~(cookiePos > -1 && document.cookie.charAt(cookiePos + 8) == 1))
     if(showfirst == 0 )
	 toggleHide(element_id,element_tag);
  }
}

function changeText(element, newText) {
  // Safari work around
  if (element.innerText)
    element.innerText = newText;
  else if (element.firstChild && element.firstChild.nodeValue)
    element.firstChild.nodeValue = newText;
}

function toggleHide(element, element_tag) {
    var block = document.getElementById(element).getElementsByTagName( element_tag )[0];
    var toggleLink = document.getElementById('togglelink'+element);
    if(block && toggleLink && block.style.display == 'none')
    {
	   changeText(toggleLink, blockHideText);
	   block.style.display = 'block';
	   document.cookie = "hideblock=0";
    }
    else
    {
	   changeText(toggleLink, blockShowText);
	   block.style.display = 'none';
	   document.cookie = "hideblock=1";
    }
}

function multitoggle(f,o) {
    for (var i = 0; i < f.length; i++) {
        if (document.getElementById('fid'+f[i])) { 
            if (f[i] == o) {
                document.getElementById('fid'+f[i]).style.display = "block";
            } else {
                document.getElementById('fid'+f[i]).style.display = "none";
            }
        }
    }
}


function show(foo,f,section) {
    document.getElementById(foo).style.display = "block";
    if (f) { document.cookie = "foo=o"; }
}

function hide(foo,f, section) {
    if (document.getElementById(foo)) {
        document.getElementById(foo).style.display = "none";
        if (f) {
	    var cookiePos = document.cookie.indexOf("hideblock=");
            var wasnot = (cookiePos > -1 && document.cookie.charAt(cookiePos + 8) == 1); //getCookie(foo, section, 'x') == 'x';
            document.cookie = "foo=c";
            if (wasnot) {
                history.go(0);
            }
        }
    }
}

function flip(foo) {
    if (document.getElementById(foo).style.display == "none") {
        show(foo);
    } else {
        if (document.getElementById(foo).style.display == "block") {
            hide(foo);
        } else {
            show(foo);
        }
    }
}

function toggle(foo) {
    if (document.getElementById(foo).style.display == "none") {
        show(foo, true, "menu");
        setCookie(foo, "o");
    } else {
        if (document.getElementById(foo).style.display == "block") {
            hide(foo, true, "menu");
            setCookie(foo, "c");
        } else {
            show(foo, true, "menu");
        }
    }
}