// JavaScript Document

// function to capitalise form entries
function capitalizeMe(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        obj.value = newVal;
}
//function to convert string to uppercase
function allUpperCase(obj){
 val = obj.value;
 obj.value = val.toUpperCase();
}

// Function to limit number of characters in a text area
function limitText(liField, liCount, limitNum) {
   var limitField = document.getElementById(liField);
   var limitCount = document.getElementById(liCount);
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.innerHTML = "Characters left: "+ (limitNum - limitField.value.length);
	}
}
// Function to display nuber characters left
function charsLeft(countField, displayField, limit)
{
displayField.value = limit - countField.value.length;
}

// Function to remove non-numerical characters from a string
function stripAlphaChars(obj){
var strSource = document.getElementById(obj).value;
var m_strOut = new String(strSource);
    m_strOut = m_strOut.replace(/[^0-9. ]/g, '');
    document.getElementById(obj).value = m_strOut;
}



// form submission check
var IE = document.all;
function check_form() {
if  (document.registration.FirstName.value == ""){ alert("Please enter your First Name"); document.registration.FirstName.focus(); return false; }
if  (document.registration.LastName.value == ""){ alert("Please enter your Last Name"); document.registration.LastName.focus(); return false; }
if  (document.registration.BillingPhoneNumber.value == ""){ alert("Please enter your Telephone Number"); document.registration.BillingPhoneNumber.focus(); return false; }
if  (document.registration.CustomerEmail.value == ""){ alert("Please enter your Email address"); document.registration.CustomerEmail.focus(); return false; }
if  (document.registration.Password.value == ""){ alert("Please create your Password. Remember to keep a note of this for future reference."); document.registration.Password.focus(); return false; }
if  (document.registration.BillingAddress.value == ""){ alert("Please enter the first line of your address"); document.registration.BillingAddress.focus(); return false; }
if  (document.registration.BillingCity.value == ""){ alert("Please enter your Town or City name"); document.registration.BillingCity.focus(); return false; }
if  (document.registration.BillingCountry.value == ""){ alert("Please enter your Country"); document.registration.BillingCountry.focus(); return false; }
if  (document.registration.BillingPostalCode.value == ""){ alert("Please enter a Postcode"); document.registration.BillingPostalCode.focus(); return false; }

}


// Layer function
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}


function MM_showHideHelp() { //v6.0
  var i,p,v,obj,txt,args=MM_showHideHelp.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
  	txt = args[i+1];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v;
	document.getElementById("helptext").innerHTML=(txt); 
	}
}


// Function to popup an alert to confirm an action
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete this item? ")) {
    document.location = delUrl;
  }
}

// Function to popup an alert to confirm a form submit
function confirmSubmit()
{
var agree=confirm("Are you sure you wish to continue?");
if (agree)
	return true ;
else
	return false ;
}

// Function to display an alert
function MM_popupMsg(msg) {
  alert(msg);
}


// function to toggle display of items
function showhidecontent(trID) {
  var oContent = document.all.item(trID);
  if (oContent.style.display='none'){
    oContent.style.display='visible';}
  else {
    oContent.style.display='none';}
}


function toggle(obj) {
	var el = document.getElementById(obj);
	var objName = obj
	var objbtn = objName+'_btn'
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
		SetCookie(objName,'none');
		document.getElementById(objbtn).src = 'images/icon_plus.gif';
	}
	else {
		el.style.display = '';
		SetCookie(objName,'visible');
		document.getElementById(objbtn).src = 'images/icon_minus.gif';
	}
}

function SetCookie(cookieName,cookieValue) {
 var today = new Date();
 var expire = new Date();
 var nDays = 1;
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}


function copyValue(source,dest) {
	//document.getElementById(dest).value = document.getElementById(source).value;
	document.getElementById(dest).style.backgroundColor = document.getElementById(source).value;
}

// JQuery calls
$(document).ready(function(){
  //Assign the ColorBox event to elements
  $(".help").colorbox();
  $(".popup").colorbox();
  //Popup call page
  $("a[rel='popupbox']").colorbox({iframe:true, innerWidth:500, innerHeight:320, rel:'nofollow', scrolling:true});
  //Form button input
  $("input#login").colorbox({iframe:true, innerWidth:500, innerHeight:320, href:'/login',
    onClosed:function(){ location.reload(true); }});
  $("a#login").colorbox({iframe:true, innerWidth:500, innerHeight:320, href:'/login',
    onClosed:function(){ location.reload(true); }});
  $('#btnClose').click(function(){
                $.fn.colorbox.close();
                return false;
            });



// Sliding menu
// On Hover Over
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery);

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}

//Set custom configurations
var config = {
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 500, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};

$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations


//Hide (Collapse) the toggle containers on load
$(".newsstory").hide();

//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
$(".newsbutton").click(function(){

$(this).toggleClass("active").prev().slideToggle("slow");
$(this).toggleClass("active").next().slideToggle("slow");
//$(".newsstory").toggleClass("active").slideToggle("slow");
//$(".newstitle").toggleClass("active").slideToggle("slow");
return false; //Prevent the browser jump to the link anchor
});



});

window.___gcfg = {lang: 'en-GB'};
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();


