// Global
var xmlhttp;
var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

function mopen(id)
{	
	mcancelclosetime();
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';
}

function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}


function appOff() {
    document.getElementById('appCalendar').style.visibility = "hidden";
    document.getElementById('appCalendar').style.display = "none";
    document.getElementById('appAudio').style.visibility = "hidden";
    document.getElementById('appAudio').style.display = "none";
    document.getElementById('appVideo').style.visibility = "hidden";
    document.getElementById('appVideo').style.display = "none";
}

function appCalendar() {
    appOff();
    document.getElementById('appCalendar').style.visibility = "visible";
    document.getElementById('appCalendar').style.display = "block";
}

function appAudio() {
    appOff();
    document.getElementById('appAudio').style.visibility = "visible";
    document.getElementById('appAudio').style.display = "block";
}

function appVideo() {
    appOff();
    document.getElementById('appVideo').style.visibility = "visible";
    document.getElementById('appVideo').style.display = "block";
}

function showDesc(id) {
  document.getElementById(id).style.visibility = 'visible';
  document.getElementById(id).style.display = 'block';
}

function hideDesc(id) {
  document.getElementById(id).style.visibility = 'hidden';
  document.getElementById(id).style.display = 'none';
} 

function video() {

    var qs = window.location.search.substring(1);
    var obj;
    var href = window.location.href;
    
    if (href.indexOf('video.php') > -1) {
        if (qs != '') {
            document.getElementById('player').innerHTML = "Loading...";
            link = 'http://www.youtube.com/v/' + qs + '&hl=en&fs=1&rel=0';
            obj = "<object width=425 height=344>";
            obj += "<param name=movie value=" + link + "><\/param>";
            obj += "<param name=allowFullScreen value=true><\/param>";
            obj += "<param name=wmode value=transparent><\/param>";
            obj += "<param name=allowscriptaccess value=always><\/param>";
            obj += "<embed src=" + link + " type=application/x-shockwave-flash ";
            obj += "wmode=transparent allowscriptaccess=always allowfullscreen=true width=425 height=344><\/embed><\/object>";
            document.getElementById('player').innerHTML = obj;
        }    
    }
}

function localVideo(filename) {

    document.getElementById('player').innerHTML = "Loading...";
    link = 'http://www.new-life.org/videos/' + filename;
    obj = "<object width=425 height=344>";
    obj += "<param name=movie value=" + link + "><\/param>";
    obj += "<param name=allowFullScreen value=true><\/param>";
    obj += "<param name=wmode value=transparent><\/param>";
    obj += "<param name=allowscriptaccess value=always><\/param>";
    obj += "<param name=controller value=true><\/param>";
    obj += "<embed src=" + link + " type=application/x-shockwave-flash ";
    obj += "wmode=transparent allowscriptaccess=always allowfullscreen=true width=425 height=344 controller=true><\/embed><\/object>";
    document.getElementById('player').innerHTML = obj;
}

function fbs_click() {
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function shareFB() {
    if (document.getElementById('shareFB')) {
        var link = "http://www.facebook.com/share.php?u=" + window.location.href;
        document.getElementById('shareFB').setAttribute("href", link);
    }    
}    

function rndHead() {

    if (document.getElementById('imgHeader') != null) {
        var header = new Array();
        header[1]="images/header1.jpg";
        header[2]="images/header2.jpg";
        header[3]="images/header3.jpg";

        var num = Math.floor(Math.random()* header.length);
        if (num == 0) {num = 1}
        document.getElementById('imgHeader').src = header[num];
    }
}

//Form validation for security question
function validate_required(field,alerttxt){
    with (field){
        if (value==null || value=="" || value!="4"){
            alert(alerttxt);return false;
        }
        else{
            return true;
        }
    }
}

function validate_form(thisform){
    with (thisform){
        if (validate_required(answer,"You must correctly answer the security question")==false){
            email.focus();return false;
        }
    }
}

function signup() {
    var query = window.location.search.substring(1);
    if (query.indexOf('subject') > -1) {
        position = query.indexOf('subject=')
        position = position + 8 //move to end of 'url='
        subject = query.substr(position);
        $('.page_title').html('Sign-Up');
        $('.intro_text').hide();
        $('#subject').val(subject);
    }
}

//Load Functions
document.onclick = mclose;
video();
shareFB();
rndHead();
