/** przone.ie
*
*  BROWSER SCRIPTS
*  Copyright (c) 2006 adecsys.com - All Rights Reserved
*/

function activateTab(btn, panel) {
    btn = $(btn);
    panel = $(panel);

    Selector.findChildElements(btn.parentNode, $A(["a"])).each(function(el, index) {
        el.removeClassName('active');
    });
    btn.addClassName('active');
    Selector.findChildElements(btn.parentNode.parentNode, $A([".tab-panel"])).each(function(el, index) {
        if(el.id == panel.id) {
            el.show();
        } else {
            el.hide();
        }
    });
}

function initTabControl(tabctl) {
    Selector.findChildElements($(tabctl), $A([".tab.active"])).each(function(el, index) {
        el.onclick();
    });

}

function showDropForm(el, form) {

    el = $(el), form = $(form);

    if(!el.hasClassName("dropped")) {

        el.addClassName("dropped");
    //    var dims = el.getDimensions();
    //    var pdims = $(el.parentNode).getDimensions();
    //    var pos = el.positionedOffset();
    //    var ppos = $(el.parentNode).positionedOffset();

        form.show();
    //    form.makePositioned();

    //    form.style.cssText += 'top:'+(pos[1]+dims.height)+'px;';

        $('signin-email').focus();

    } else {

        el.removeClassName("dropped");
        form.hide();

    }
    return false;
}


function extensibleDropList(el, ex) {
    Element[(el.options[el.selectedIndex].value == "-1")? "show" : "hide"](ex);
}


function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  return false;
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function gatherValues(form) {
    var V = Form.serializeElements(Form.getElements(form), true);
    for (var K in V) {
        if(K.indexOf('[') >0) {
            RSetMDV(V, K.replace(/\]/g,'').split('['), V[K]);
            delete V[K];
        }
    }
    return V;
}
function RSetMDV(Va, Kb, V) {

    Kb[0] = Kb[0].replace(/\s/g,"");
    if(Kb.length == 1) {
        if(Kb[0] == '') {
            if(Object.isArray(V)) {
                for (var key in V) {
                    if (Object.isFunction(V[key])) continue;
                    Va[key] = V[key];
                }
            } else {
                Va[Va.length] = V;
            }
        } else {
            Va[Kb[0]]=V;
        }
    } else {
        Ka = Kb.shift();
        if(!Va[Ka]) Va[Ka] = [];
        RSetMDV(Va[Ka], Kb, V);
    }
}

function vRegFormOne(form) {

    var msg='';
    var V = gatherValues(form);

    if(!V._tandcs || V._tandcs!='Y') {
        msg = "> Please confirm that you have read and understood the terms & conditions\n"+msg;
    }
    if(!V.company.sector || V.company.sector=='') {
        msg = "> Sector must be selected\n"+msg;
    }
    if(!V.company.region || V.company.region=='') {
        msg = "> Region must be selected\n"+msg;
    }
    if(!V.company.vat_number || V.company.vat_number=='') {
        msg = "> VAT Number must be provided\n"+msg;
    }
    if(!V.company.address || V.company.address=='') {
        msg = "> Registered Address must be entered\n"+msg;
    }
    if(!V.company.cro_number || V.company.cro_number=='') {
        msg = "> Registered Number must be entered\n"+msg;
    }
    if(!V.company.legal_name || V.company.legal_name=='') {
        msg = "> Legal Business Name must be entered\n"+msg;
    }

    if(!V.contact.password || V.contact.password=='') {
        msg = "> Password must be entered\n"+msg;
    } else if (!V.contact.p_confirm || V.contact.password != V.contact.p_confirm) {
        msg = "> Passwords do not match\n"+msg;
    }

    if(!V.contact.email || V.contact.email=='') {
        msg = "> Email Address is required\n"+msg;
    }
    if(!V.contact.title || V.contact.title=='') {
        msg = "> Title must be entered\n"+msg;
    }
    if(!V.contact.name || V.contact.name=='') {
        msg = "> Name is required\n"+msg;
    }

    if(msg != '') {
        alert(msg);
        return false;
    }
    return true;
}

function vRegFormTwo(form) {

    var msg='';
    var V = gatherValues(form);

    if(!V.account || !V.account._ref || V.account._ref=='') {
        msg = "> You must select an account type\n"+msg;
    }

    if(msg != '') {
        alert(msg);
        return false;
    }
    return true;
}


function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

Event.observe(window, "load", function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
});

Event.observe(window, "unload", function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
});

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

/********************/

Event.observe(window, "load", function(e) {

  if($('currenttime')) {
    new PeriodicalExecuter(function() {
      var now = new Date();
      $('currenttime').innerHTML = now.print('%d %b %Y %H:%M');
    }, 60);
  }
});



Position.center = function(element){
    var options = Object.extend({
        zIndex: 999,
        update: false
    }, arguments[1] || {});
    element = $(element)
    if(!element._centered){
        Element.setStyle(element, {position: 'absolute', zIndex: options.zIndex });
        element._centered = true;
    }
    var dims = Element.getDimensions(element);
    Position.prepare();
    var winWidth = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;
    var winHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
    var offLeft = (Position.deltaX + Math.floor((winWidth-dims.width)/2));
    var offTop = (Position.deltaY + Math.floor((winHeight-dims.height)/2));
    element.style.top = ((offTop != null && offTop > 0) ? offTop : '0') + 'px';
    element.style.left = ((offLeft != null && offLeft > 0) ? offLeft : '0') + 'px';
    if(options.update){
        Event.observe(window, 'resize', function(evt){ Position.center(element); }, false);
        Event.observe(window, 'scroll', function(evt){ Position.center(element); }, false);
    }
}


  function showpanel(pid) {
    if(pid <0) {

        if(pid == -1) {
            $$('#panels .panel').each(function(el, index) {
                if(el.visible()) {
                    pid = (index >0) ? index -1 : 2;
                }
            });
        } else
        if(pid == -2) {
            $$('#panels .panel').each(function(el, index) {
                if(el.visible()) {
                    pid = (index <3) ? index +1 : 0;
                }
            });
        }
    }

    $$('#panels .panel').each(function(el, index) {
        if(index == pid) {

            Effect.SlideDown(el, {
                duration: 1.0,

                beforeStart : function() {
                    el.style.zIndex='500';
                },

                afterFinish : function() {
                    el.style.zIndex='10';

                    $$('#panels .panel').each(function(eltmp, ii) {
                        if(pid != ii) {
                            eltmp.hide();
                        }
                    });
                }
            });
        }
    });

    $$('#panelindex a').each(function(el, index) {
        if((index-1) == pid) {
            el.addClassName('current');
        } else {
            el.removeClassName('current');
        }
    });
  }

function startslideshow() {

    $('panelindex').innerHTML = "<a onclick='if(window.slideshow) {window.slideshow.stop(); } return showpanel(-1);'>&laquo;</a> <a class='current' onclick='if(window.slideshow) {window.slideshow.stop(); } return showpanel(0);'>1</a>  <a onclick='if(window.slideshow) {window.slideshow.stop(); } return showpanel(1);'>2</a>  <a onclick='if(window.slideshow) {window.slideshow.stop(); } return showpanel(2);'>3</a>  <a onclick='if(window.slideshow) {window.slideshow.stop(); } return showpanel(3);'>4</a>  <a onclick='if(window.slideshow) {window.slideshow.stop(); } return showpanel(-2);'>&raquo;</a> ";

    var panels = $$('#panels .panel');

    panels.each(function(el, index) {
        if(index >0) { el.hide();
        } else { el.show();
        }
    });

    window.slideshow = new PeriodicalExecuter(function() {
        showpanel(-2);
    }, 8);
}
