function showObj(objId){ var objStyle = getObjStyle(objId); if (objStyle){
    objStyle.visibility = 'visible'; objStyle.display = 'block';}}
function hideObj(objId){ var objStyle = getObjStyle(objId); if (objStyle){ 
    objStyle.visibility = 'hidden'; objStyle.display = 'none';} }
function isVisible(objId)
{
    var objStyle = getObjStyle(objId);
    return ((objStyle != null) ? objStyle.visibility == 'visible' : false);
}
function getObj(objId)
{
    if (document.getElementById) {return document.getElementById(objId);} // DOM3 = IE5, NS6
    else if (document.layers) {return document.objId;} // Netscape 4
    else {return document.all.objId;}// IE 4
}
function getObjStyle(objId) {var obj = getObj(objId); return (obj==null) ? null : obj.style; }

function send()
{
    document.getElementById("msg").src = "mail.php";
}

function checkForm(formName)
{
    form = document.getElementById(formName);
    var bReturn = true;

    if (!form.from.value.length) 
    {
		alert("Please enter your email address.");
        bReturn = false;
    }
    else 
    {
	    if (!validateEmail(form.from.value))
	    {
		    alert("The email address must be valid (eg. myname@earthlink.net)");
            bReturn = false;
	    }
    }

    if (bReturn)
    {
        if (!form.subj.value.length)
        {
		    alert("Please enter a subject.");
            bReturn = false;
        }
        else if (!form.msg.value.length)
        {
		    alert("Please enter a message.");
            bReturn = false;
        }
    }

    return bReturn;  
}

function validateEmail(email)
{
    if(email.length <= 0)
    {
      return false;
    }
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.\+]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null)
      {
        var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
        if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
    return false;
}

function buildQueryString(theFormName)
{
	theForm = document.forms[theFormName];
	var qs = ''
	for (e=0;e<theForm.elements.length;e++) {
		if (theForm.elements[e].name!='') {
			qs+=(qs=='')?'?':'&'
			qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
			}
		}
	return qs
}

var IFrameObj; // our IFrame object
function callToServer(theFormName) {
    if (!checkForm(theFormName))
        return false;

	if (!document.createElement) {return true};
	var IFrameDoc;
	var URL = 'mail.php' + buildQueryString(theFormName);
	if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// callToServer() is called
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['RSIFrame'];
			}
		} catch(exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='<iframe id="RSIFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer("'+theFormName+'")',10);
		return false;
	}
	
	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}
	
	IFrameDoc.location.replace(URL);
	return false;
}

	
function handleResponse()
{
	var theFormDiv = document.getElementById('theFormDiv');
	theFormDiv.style.display = 'none';
	var responseMessage = document.getElementById('responseMessage');
	var message = '<p style="text-align:center;color:green">Your message has been sent! Thank you.<\/p>'
	message += '<p align="center"><a href="#" onclick="showForm(); return false;">Send a message<\/a><\/p>'
	responseMessage.innerHTML = message;
	responseMessage.style.display = 'block';
}

function showForm()
{
	var theFormDiv = document.getElementById('theFormDiv');
	theFormDiv.style.display = 'block';
	document.forms['mailform'].reset();
	var responseMessage = document.getElementById('responseMessage');
	responseMessage.style.display = 'none';
}


function pxNum(pxnum)
{
    return (pxnum.substr(0,pxnum.indexOf('p')));
}

// Snow Effect Javascript
// copyright 19th November 2004, 20th July 2006 by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
// you can change the number of snow flakes if you like
//
// It has been modified to animate popcorn by clicking on a link.
var num_pop = 9;
var popcorn = new Array("/img/popcorn1.gif","/img/popcorn2.gif");

// DOM test
var aDOM = 0, ieDOM = 0, nsDOM = 0; var stdDOM = document.getElementById;
if (stdDOM) aDOM = 1; else {ieDOM = document.all; if (ieDOM) aDOM = 1; else {
var nsDOM = ((navigator.appName.indexOf('Netscape') != -1)
&& (parseInt(navigator.appVersion) ==4)); if (nsDOM) aDOM = 1;}}
function findDOM(objectId, wS) {
if (stdDOM) return wS ? document.getElementById(objectId).style:
document.getElementById(objectId);
if (ieDOM) return wS ? document.all[objectId].style: document.all[objectId];
if (nsDOM) return document.layers[objectId];
}

// window size tests
function findLivePageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth :700;}
function findLivePageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight :500;}
function posX() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
function posY() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}

// make it pop
var speed = 10;var movw = new Array();var movh = new Array();var move = new Array();var stepw = new Array();var steph = new Array();var posw = new Array();var posh = new Array();var dir = new Array();var winWidth;var winHeight;
var poppping = 0;
var baseSpeed = 10;

function morePop()
{
    for (var i = 0; i < num_pop; i++) 
    {
        if ((posh[i] > winHeight))
        {posw[i] = Math.random()*(winWidth-movw[i]);
         posh[i] = 0;
         dir[i]=(Math.random()<0.5)?1:-1;
         stepw[i] = 0.02 + Math.random()/9;
         steph[i] = 1.3 + Math.random();
        }
        move[i] += stepw[i] *dir[i];
    
        if (Math.abs(move[i]) > 3)
        {
            dir[i]=-dir[i];
            posh[i]+=Math.abs(movh[i]*move[i]);
            posw[i]+=movw[i]*move[i]; move[i]=0;
        } 

        objstyle = findDOM('popcorn'+i,1); 
        objstyle.left = (posX()+posw[i] + movw[i]*move[i])+'px';
        objstyle.top = (posY()+posh[i] + movh[i]*(Math.abs(Math.cos(move[i])+move[i])))+'px';
        objstyle.visibility = 'visible';
    } 

    if (popping == 0)
    {
        clearTimeout(popping);
        document.getElementById("corn").innerHTML = "";
    }
    else
    {
        poppping = setTimeout("morePop()", speed);
    }
}

function startPop() 
{
    winWidth = findLivePageWidth();
    winHeight = findLivePageHeight();
    str = "";

    for (var i = 0; i < num_pop; i++)
    {
        move[i] = 0;
        movh[i] = baseSpeed + Math.random()*baseSpeed;
        movw[i] = baseSpeed + Math.random()*baseSpeed;
        posw[i] = Math.random()*winWidth;
        posh[i] = Math.random()*winHeight;
        stepw[i] = 0.02 + Math.random()/5;
        steph[i] = 0.7 + Math.random();
        dir[i] = (Math.random()>0.5)?1:-1;
        str += '<div id="popcorn'+ i +'" style="position: absolute; z-index:99; visibility:hidden; "><img src="'+popcorn[Math.floor(Math.random()*popcorn.length)]+ '" border="0"></div>';
    }

    document.getElementById("corn").innerHTML = str;
    popping = setTimeout("morePop()", speed);
}

function stopPop()
{
    popping = 0;
}

function openFront()
{
    hideObj('front');
    showObj('inside');
}

var curCId = 'welcome';

function setContent(id)
{
    hideObj(curCId);

    objStyle = getObjStyle(id);
    objStyle.height = document.body.clientHeight -75 + 'px';

    showObj(id);
    curCId = id;
}

function startMayhem()
{
    setContent('mayhem');
    document.getElementById("cover").src = "img/cover-blank.gif";
    startPop();
}

function stopMayhem()
{
    stopPop();
    document.getElementById("cover").src = "img/menu.gif";
}
