window.addEvent('domready', function () {

  LoginForm();
    MegaMenu();
    CurrentLinks();
    //LinksEffect();
    if ($('signUpBgImage')) {
        GlobalSignUp();
        var SignUpButton = $$('#signUpButton2');
        SignUpButton.each(function (el, i, a) {
            el.addEvents({ 'mouseenter': function () {
                el.setStyle('background-position', "bottom");
            },
                'mouseleave': function () {
                    el.setStyle('background-position', "top");
                }

            });
        });
    }
    if ($('signUp2')) {
        Buttons();
        GlobalSignUp();
    }

    if ($('WhitepaperSignUp')) {
         WhitepaperSignUp();
    }


});



function MegaMenu() {

 


    var MegaDropDownLink = $$('#megaDropDownLink');
    MegaDropDownLink.each(function (el, i, a) {
        el.addEvents({ 'mouseenter': function () {
            //$('megaLink').set('morph', { duration: '500', transition: 'expo:out' });
            //$('dropdown').set('morph', { duration: '500', transition: 'expo:out' });
            //$('dropdown').morph({ 'top': '43px' });
	    document.getElementById("dropdown").style.top = "43px";	            
            //$('megaLink').morph({ 'opacity': 1, 'top': '0px', 'display': 'block' });
	    document.getElementById("megaLink").style.display= "block";
            if ($('signUp2')) 
                $('signUp2').morph({ 'top': '-63px' });
	    MegaMouseOut = false;
    },'mouseleave': function () {
	    MegaMouseOut = true;
            MegaDropUp.delay(100);
        }

        });
    });
    $$('#dropdown').addEvents({ 'mouseenter': function () {

 
        MegaMouseOut = false;
    },'mouseleave': function () {
	    MegaMouseOut = true;
            MegaDropUp.delay(100);
        }
    });
    var MegaDropUp = function () {
        if (MegaMouseOut == true) {
            //  $('dropdown').set('morph', { duration: '1200', transition: 'back:InOut' });
            //$('dropdown').morph({ 'top': '-500px' });
            //$('megaLink').morph({ 'opacity': 0, 'top': '42px' });
	    document.getElementById("dropdown").style.top = "-500px";	            
 	    document.getElementById("megaLink").style.display = "none";	            
         }
    }


}

function LoginForm() {
    $('LoginForm').set('morph', { duration: '500', transition: 'expo:out' });

    $('topLogin').addEvent('click', function () {
        $('LoginForm').morph({ 'top': '0px' });


    });

    $('loginClose').addEvent('click', function () {
        $('LoginForm').morph({ 'top': '-170px' });
    });


    $$(".loginTick").each(function (e, i, a) {
        e.setStyle("opacity", 0);
    });
    $$(".loginCross").each(function (e, i, a) {
        e.setStyle("opacity", 0);
    });

    $$('.overtext').each(function (e, i, a) {
        new OverText(e);
    });
    var val1 = new Form.Validator($("TopLoginForm"), {
        stopOnFailure: true,
        onElementPass: function (e, arr) {
            if (e.hasClass("overtext") == true) {
                e.getParent().getFirst(".loginCross").morph({ "opacity": 0 });
                e.getParent().getFirst(".loginTick").morph({ "opacity": 1 });
                //e.getParent().getFirst(".loginTextbox").setStyle("border-color", "#6ede23");
            }
        },
        onElementFail: function (e, arr) {
            if (e.hasClass("overtext") == true) {
                e.getParent().getFirst(".loginCross").morph({ "opacity": 1 });
                e.getParent().getFirst(".loginTick").morph({ "opacity": 0 });
                // e.getParent().getFirst(".loginTextbox").setStyle("border-color", "#ff0000");
            }
        },
        onFormValidate: function (passed, form, event) {
            if (passed == true) {
                return true;
            }

            return false;
        }
    });
}


function Buttons() {

    var SignUpButton2 = $$('#signUpButton2');
    SignUpButton2.each(function (el, i, a) {
        el.addEvents({ 'mouseenter': function () {
            el.setStyle('top', "-62px");
        },
            'mouseleave': function () {
                el.setStyle('top', "0px");
            }

        });
    });

    $('signUpBg2').set('morph', { duration: '600', transition: Fx.Transitions.Expo.easeInOut });
    var cost = $$('#signUp2');
    cost.each(function (el, i, a) {
        el.addEvents({ 'click': function () {
            $('signUpBg2').morph({ 'top': '0px' });
        }
        });
    });

    var SignUpFormUp = $$('#signUpBg2');
    SignUpFormUp.each(function (el, i, a) {
        el.addEvents({ 'mouseenter': function () {

            SignupMouseOut = false;
        },
            'mouseleave': function () {
                if ($('homeFlashBanner')) {
                    SignupMouseOut = false;
                }

                else if ($('packagesWrapper')) {
                    SignupMouseOut = false;
                }

                else {
                    SignupMouseOut = true;
                }
                FormSignUp.delay(1000);
            }
        });
    });
    var FormSignUp = function () {
        if (SignupMouseOut == true) {

            if ($('homeFlashBanner')) {
                $('signUpBg2').morph({ 'top': '0px' });
            }
            else if ($('packagesWrapper')) {
                $('signUpBg2').morph({ 'top': '0px' });
            }

            else {

                $('signUpBg2').morph({ 'top': '-960px' });
            }
        }
    }
}


function WhitepaperSignUp() {


 
    $$(".packagesSignupFormTick").each(function (e, i, a) {
        e.setStyle("opacity", 0);
    });
 
    $$(".packagesSignupFormCross").each(function (e, i, a) {
        e.setStyle("opacity", 0);
    });

    $$('.overtext').each(function (e, i, a) {
        new OverText(e);
    });
    var val1 = new Form.Validator($("WhitepaperSignUp"), {
        stopOnFailure: true,
	evaluateOnSubmit:true,

        onElementPass: function (e, arr) {
            if (e.hasClass("overtext") == true) {
                e.getParent().getFirst(".packagesSignupFormCross").morph({ "opacity": 0 });
                e.getParent().getFirst(".whitepaperTextbox").setStyle("border-color", "#6ede23");
            }
        },
        onElementFail: function (e, arr) {
           if (e.hasClass("overtext") == true) {
                e.getParent().getFirst(".packagesSignupFormCross").morph({ "opacity": 1 });
                e.getParent().getFirst(".whitepaperTextbox").setStyle("border-color", "#ff0000");
            }
        },
        onFormValidate: function (passed, form, event) {
  
          if (passed == true) {
    
              downloadWhitepaper();
   		//document never submits                
		return false;
            }
             return false;
        }

    });
}


function downloadWhitepaper(){
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    response=xmlhttp.responseText;
    return;
    }
  }
alert("Thank you for your interest in the whitepaper.  You will receive an email shortly.");

xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
xmlhttp.open("POST","/feefo/contact.jsp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("context=whitepapers&email=" + document.getElementById("subscribeemail").value + "&contactname=" + document.getElementById("contactname").value  + "&companyname=" + document.getElementById("companyname").value  + "&website=" + document.getElementById("website").value);



}


function GlobalSignUp() {

    $$(".cross").each(function (e, i, a) {
        e.setStyle("opacity", 0);
    });

    $$('.overtext').each(function (e, i, a) {
        new OverText(e);
    });
    var val1 = new Form.Validator($("GlobalSignUp"), {
        stopOnFailure: true,
        onElementPass: function (e, arr) {
            if (e.hasClass("overtext") == true) {
                e.getParent().getFirst(".loginCross").morph({ "opacity": 0 });
                e.getParent().getFirst(".signUpTextbox2").setStyle("border-color", "#6ede23");
            }
        },
        onElementFail: function (e, arr) {
            if (e.hasClass("overtext") == true) {
                e.getParent().getFirst(".loginCross").morph({ "opacity": 1 });
                e.getParent().getFirst(".signUpTextbox2").setStyle("border-color", "#ff0000");
            }
        },
        onFormValidate: function (passed, form, event) {
            if (passed == true) {
                return true;
            }

            return false;
        }

    });
}


function GetUrlMinusHostname(url) {
    var urlArray = new Array();
    for (var i = 3, j = 0; i < url.length; ++i, ++j) {
        urlArray[j] = url[i];
    }
    var urlToReturn = urlArray.join("/");
    return "/" + urlToReturn;
}


function CurrentLinks() {
    var contactFormLink = $$('#ContactLink a').getProperty('href').join();
    var currentUrl = document.location.toString().split("/");
    var url = GetUrlMinusHostname(currentUrl);
    if (url == "/") {
        $$('#HomeLink a').addClass("currentTopMenu");
    }
    else if (url.toLowerCase() == contactFormLink.toLowerCase())
    {
        $$('#ContactLink a').addClass("currentTopMenu");
    }
    else {

        var toplinks = $$('.topMenuUrl');
        for (var i = 0; i < toplinks.length; ++i) {
            toplinks[i] = toplinks[i].getProperty('href').toLowerCase();
        }

        var MenuItems = $$(".topLinks");
        // alert('currently at: ' + url);

        var feefoMenuItem = $$('#megaDropDownLink a');
        if (toplinks.contains(url.toLowerCase())) {
            // alert('adding class to: ' + url);
            feefoMenuItem.addClass("currentTopMenu");
        }

    }


    /*
    MenuItems.each(function (e, i, a) {
        var elementLink = e.getProperty("href");
        alert(elementLink);
        if (elementLink.contains(url.toLowerCase())) {
            alert('adding class to: ' + url);
            e.addClass("currentTopMenu");
        }

    });
    */

    //alert(urlArray);  

         /*
        if (urlArray.length > 2) {
        if (urlArrayLink[1].toLowerCase() == urlArray[3].toLowerCase()) {

        e.addClass("currentTopMenu");
        }
        }
        
    });
    */









}






function LinksEffect() {

    //    var TopMenuLinkEffect = $$('#menuLinks li');
    //    TopMenuLinkEffect.each(function (el, i, a) {
    //        el.addEvents({ 'mouseenter': function () {
    //            el.set('morph', { duration: '400', transition: Fx.Transitions.Expo.easeInOut });
    //            el.morph({ 'background-position': "0px -3px" });
    //         
    //        },
    //            'mouseleave': function () {
    //                el.morph({ 'background-position': "-30px -3px" });
    //            }

    //        });
    //    });

    $$('.menuHoverEffect').setStyle('opacity', 0);

    var TopMenuLinkEffect = $$('#menuLinks li');
    TopMenuLinkEffect.each(function (el, i, a) {
        el.addEvents({ 'mouseenter': function () {

            el.getFirst('.menuHoverEffect').set('morph', { duration: '300', transition: Fx.Transitions.Expo.easeInOut });

            el.getFirst('.menuHoverEffect').morph({ 'top': "-9px", "opacity": 1});

        },
            'mouseleave': function () {
                el.getFirst('.menuHoverEffect').morph({ 'top': "0px", "opacity": 0 });
            }

        });
    });

}


 function showItWorking(){
	 				testpageval = document.getElementById("testpage").value;
					 window.open("http:/feefo/integrate.jsp?op=showsample&testpage=" + testpageval,"example","");

 }

 function Customise(){
      window.open("/info/integratewizard-" +  document.getElementById("testpage").value);

 }
 
   
  

