function homeSection()
{
    var strUrl = location.search.split("&");
    switch (strUrl[0]) {
        case '?module=pfamily':
            location.href = "index.php?module=pfamily";
            break;
        case '?module=ministry':
            location.href = "index.php?module=pfamily";
            break;
        case '?module=myperimeter':
            location.href = "index.php?module=pfamily";
            break;
        case '?module=visitors':
            location.href = "index.php?follow=yes";
            break;
        case '?module=registration':
            location.href = "index.php?follow=yes";
            break;
        default:
            location.href = "index.php?follow=yes";
            break;
    }
}

function IsNumeric(sText)

{
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;

 
    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1)
        {
            IsNumber = false;
        }
    }
    return IsNumber;
   
}



function validateSignin(){

    var fname = document.getElementById('fname').value;
    var sfname = document.getElementById('sfname').value;
    var lname = document.getElementById('lname').value;
    var age = document.getElementById('age').value;
    var address = document.getElementById('address').value;
    var city = document.getElementById('city').value;
    var zip= document.getElementById('zip').value;
    var phone = document.getElementById('phone').value;
    var email = document.getElementById('email').value;
    var amount = document.getElementById('amount').value;
	
    if(amount==0){
        alert('You have to select "Number of people coming"');
        document.getElementById('amount').focus();
        return false;
    }
	
    if(fname==''){
        alert('First Name is required');
        document.getElementById('fname').focus();
        return false;
    }
	
    if(amount==2 && sfname==''){
        alert('Spouse Name is required');
        document.getElementById('sfname').focus();
        return false;
    }
	
    if(lname==''){
        alert('Last Name is required');
        document.getElementById('lname').focus();
        return false;
    }
	
    if(age==''){
        alert('Age is required');
        document.getElementById('age').focus();
        return false;
    }
	
    if(!IsNumeric(age)){
        alert('Age must be numeric');
        document.getElementById('age').focus();
        return false;
    }
	
    if(address==''){
        alert('Address is required');
        document.getElementById('address').focus();
        return false;
    }
    if(city==''){
        alert('City is required');
        document.getElementById('city').focus();
        return false;
    }
    if(zip.length!=5){
        alert('Zip code must be 5 characters length');
        document.getElementById('zip').focus();
        return false;
    }
    if(zip==''){
        alert('Zip is required');
        document.getElementById('zip').focus();
        return false;
    }
    if(!IsNumeric(zip)){
        alert('Zip must be numeric');
        document.getElementById('zip').focus();
        return false;
    }
    if(phone==''){
        alert('Phone is required');
        document.getElementById('phone').focus();
        return false;
    }
    if(email==''){
        alert('Email is required');
        document.getElementById('email').focus();
        return false;
    }
    return true;
}

function openSlidePage(){
    if(typeof(currSlideUrl) != 'undefined'){
        if(typeof(currSlideTarget) != 'undefined'){
            switch(currSlideTarget){
                case '_blank':
                    window.open(currSlideUrl);
                    break;
                case '_self':
                    location.href = currSlideUrl;
                    break;
            }
        }
    }
}
var pCal = function(w,h){
    this.email = '';
    this.ran = 0;
    this.width = (typeof(w) != 'undefined') ? w : 300;
    this.height = (typeof(h) != 'undefined') ? h : 300;
    this.ministry_id = 0;
    this.article_id = 0; // will be converted to `Newsletter` in db;
    this.event_id = 0;
    this.init = function(){
        var t = this;
        t.ran = Math.floor(Math.random()*10000);
        darkScreen(true, function(){
            Dialog.create('cal_window'+t.ran,'',
            {
                width:t.width+20,
                height:t.height,
                title:'Add to my calendar',
                closebutton:true,
                content: '<div id="div'+t.ran+'"><h4>Enter your email address to add this event to your calendar</h4><form name="eventForm" id="eventForm" action="javascript:void(0)">'+
                'Email address&nbsp;<input onkeyup="javascript:cal.email = this.value;" id="eventEmail" name="eventEmail" type="text" style="border:1px solid #cdcdcd;font-size:11px;width:170px" />'+
                '<input type="hidden" name="ministry_id" id="ministry_id" value="'+t.ministry_id+'"/>'+
                '<input type="hidden" name="article_id" id="article_id" value="'+t.article_id+'"/>'+
                '<button onclick="javascript:void(cal.send());" style="cursor:pointer;margin-left:2px;font-size:11px;border:1px solid #acacac">Add</button></form><br /><span id="calStatus" /></div>'
            });
            j('#cal_window'+t.ran+'_cont').css({
                overflow: '',
                height: '',
                width: ''
            });
            j('#cal_window'+t.ran+'_handle').css({
                background: '#4E4E4E',
                border:'0!important'
            });
        });
    }
    this.send = function(){
        var t = this;
        if(t.email == '' || (!t.email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/gi) && !t.isSetup)){
            if(typeof(j('#calStatus').html()) != 'undefined'){
                j('#calStatus').text("Write a valid email address.");
            }
        }else{
            if(j('#eventForm').serialize() != ''){
                var close = function(){
                    Dialog.close('cal_window'+t.ran);
                }
                j.ajax({
                    url:'/includes/ajax/event_handler.php',
                    type:'POST',
                    data:j('#eventForm').serialize(),
                    async:false,
                    success: function(data){
                        if(typeof(j('#calStatus').html()) != 'undefined'){
                            j('#calStatus').text(data);
                            setTimeout(close,1500);
                        }
                    }
                });
                return;
            }else{
                return;
            }
        }
    }
    this.Title = '',
    this.Description = '',
    this.Place = '',
    this.EventDateStart = '',
    this.EventDateEnd = '',
    this.isSetup = false, // this is activated when we're on myPerimeter (backend function), to avoid email check
    this.backend = function(){
        this.isSetup = true;
        var newForm = '<form action="javascript:void(0);" name="eventForm" id="eventForm">';
        newForm += '<div style="position:relative;width:100%">';
        newForm += '<div style="float:left;width:50%">';
        newForm += 'Starts on (click on the calendar)<br /><input type="image" src="/images/icons/greyscale_38.gif" border="0" id="calOpenStart"/>&nbsp;';
        newForm += '<input value="'+unescape(this.EventDateStart)+'" id="eventDateStart" name="EventDateStart" style="background:transparent;border:none"/><br />';
        newForm += 'Ends on (click on the calendar)<br /><input type="image" src="/images/icons/greyscale_38.gif" border="0" id="calOpenEnd"/>&nbsp;';
        newForm += '<input value="'+unescape(this.EventDateEnd)+'"id="eventDateEnd" name="EventDateEnd" style="background:transparent;border:none"/><br />';
        newForm += '<input type="hidden" value="'+this.ministry_id+'" name="ministry_id" id="ministry_id" />';
        newForm += '<input type="hidden" value="'+this.article_id+'" name="article_id" id="article_id" />';
        newForm += '<input type="hidden" value="'+this.event_id+'" name="event_id" id="event_id" />';
        newForm += 'Where<br /><input value="'+unescape(this.Place).replace(/"/gi,'\"')+'" id="Place" name="Place" /></div>';
        newForm += '<div style="float:left;width:49%">';
        newForm += 'Title<br /><input value="'+unescape(this.Title).replace(/"/gi,'\"')+'" style="width:100%" type="text" id="Title" name="Title" /><br />';
        newForm += 'Short description<br /><textarea id="eventDesc" name="Description" style="height:80px">'+unescape(this.Description)+'</textarea></div>';
        newForm += '</div>';
        if(j('#isevent').attr('checked')){
            j('#eventSetup_content').html(newForm);
            j('#eventSetup').slideDown('fast');
            Calendar.setup({
                inputField     :    "eventDateStart",
                ifFormat       :    "%Y-%m-%d %H:%M",
                showsTime      :    true,
                button         :    "calOpenStart",
                step           :    1,
                onSelect       : function(widget,dateTime){
                    j('#eventDateStart').attr('value',dateTime);
                    var c = 'cal.EventDateStart=\''+dateTime+'\'';
                    eval(c);
                    if (widget.dateClicked) {
                        widget.callCloseHandler();
                    }
                }
            });
            Calendar.setup({
                inputField     :    "eventDateEnd",
                ifFormat       :    "%Y-%m-%d %H:%M",
                showsTime      :    true,
                button         :    "calOpenEnd",
                step           :    1,
                onSelect       : function(widget,dateTime){
                    j('#eventDateEnd').attr('value',dateTime);
                    var c = 'cal.EventDateEnd=\''+dateTime+'\'';
                    eval(c);
                    if (widget.dateClicked) {
                        widget.callCloseHandler();
                    }
                }
            });
            j('#eventForm input[type=text]').each(function(ix,el){
                j(el).bind('change',function(ev){
                    var cur = (ev.target) ? ev.target : ev.currentTarget;
                    var c = 'cal.'+cur.name+'=\''+escape(j(cur).attr('value'))+'\'';
                    eval(c);
                });
            });
            j('#eventDesc').bind('keyup',function(ev){
                var cur = (ev.target) ? ev.target : ev.currentTarget;
                var c = 'cal.'+cur.name+'=\''+escape(cur.value)+'\'';
                eval(c);
            });
        }else{
            j('#eventSetup').slideUp('fast');
        }
    }
}

function updateInfo(){
    var ran = Math.ceil(Math.random()*100000000);
    Dialog.create('updateinfo_window'+ran,'',
    {
        width:400,
        title:'Update your Profile / Church Records',
        closebutton:true,
        content: '<div id="div'+ran+'">To update your profile / church records, please do one of the following.'+
        '<br /><br /><a href="/index.php?module=myperimeter">Login and click Update Account Information</a>'+
        '<br /><br /><a href="/index.php?module=registration">New Online User Registration</a>'+
        '<br /><br /><a target="_blank" href="mailto:nicolek@perimeter.org?subject=update_my_information">Email Perimeter your changes.</a>'+
        '<br /><br /><br /></div>'
    });
}

function adjustmentTool(){
    var html = '';
    j.ajax({
        url: '/includes/adjustment_tool.php',
        cache:false,
        success:function(data){
            html = data;
            Dialog.create('colors_editor','',
            {
                width:640,
                height:350,
                title:'Adjustment tool',
                closebutton:true,
                content: html
            });
        }
    });
}

function openPage5(name, options){
    var id = (Math.ceil(Math.random()*100000000))+'-page5';
    if(typeof(options) != 'undefined'){
        options.width = (typeof(options.width) != 'undefined') ? options.width: '590';
        options.app = (typeof(options.app) != 'undefined') ? options.app: {};
    }else{
        options = {
            width: '590',
            app:{}
        };
    }
    if(typeof(name) != 'undefined'){
        var s = 'border:1px outset #e5e5e5;background-color:#fcfcfc;padding:5px';
        //var div = '<div style="'+s+';display:none;position:fixed;top:100px;right:2px;width:'+options.width+';z-index:105" id="'+id+'">';
        var iframe = '<iframe frameborder="0" allowtransparency="true" src="/page5/index.php?app='+escape(name)+'&options='+escape(JSON.stringify(options.app))+'" width="'+options.width+'" class="autoHeight"></iframe>';
        if(!j('#'+id).html()){
            Dialog.create(id,'',
            {
                width:parseInt(options.width)+10,
                height:590,
                title:'Image Gallery: '+options.app.gallery,
                closebutton:true,
                content: iframe
            });
        }
    }
}

j(function(){
    window.adjustments = {}
    j('a').each(function(ix,el){
        if(el.href.match(/\/updateinfo$/gi)){
            j(el).click(function(){
                updateInfo();
                return false;
            });
        }
    });
    if(j('#contents_list').is('div')){
        j('#contents_list p').each(function(i,e){
            if(j(e).text()==''){
                j(e).remove();
            }
        });
        var act = (location.href.match(/^.*#\d+$/gi))? parseInt(location.href.replace(/^.*#(\d+)$/gi,'$1')):false;
        j('#contents_list').accordion({
            collapsible:true,
            clearStyle:true,
            header:'h3',
            active:false
        });
	if(act != false)
	    j('#contents_list').accordion('activate',act-1);
    }
});

function buildTree(){

}


