function clickButton(e, buttonid){ 

  var evt = e ? e : window.event;
  var bt = document.getElementById(buttonid);

  if (bt){ 
      if (evt.keyCode == 13){ 
         bt.click(); 
        return false; 
      } 
  } 
}

function addToFavorites(urlName, pageName) { 
    window.external.AddFavorite(urlName,pageName);
    return false;
}

function ShowAlternativeImage(imageName, clientID)    {
    
    document.getElementById(clientID).src = '/images/ecommerce/standard/' + imageName;
    //document['mainImage'].src = '/images/ecommerce/mini/' + imageName;
}

function zoomProductImage(productImage) {
    
	if (productImage) {
		window.open('/Ecommerce/zoom.aspx?image=' + productImage,'ZOOM','width=600,height=600,scrollbars=no,resize=no');
    }
    
    //[RK] 21/12/06 - triggered from ImageButton so MUST return false
    return false;
}

function fitPic() {
	if (window.innerWidth){
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	}else{
		iWidth = document.body.clientWidth;
		iHeight =document.body.clientHeight;
	}
	
//	alert(iHeight);
	
	iWidth = document.images[0].width - iWidth;
//	iHeight = document.images[0].height - iHeight;
    
    iHeight = document.images[0].height
	window.resizeBy(iWidth, iHeight);
}

function copyAddress()  {
    //[RK] 27/03/2007.
    //Uses hidden fields to allow us to access ASP.NET controls from Javascript.
    
    var prefix = document.forms['aspnetForm'].elements['jsPrefix'].value;
    var oForm = document.forms['aspnetForm']
    //alert(prefix)

    oForm.elements[prefix + '_DeliveryLine1TextBox'].value = oForm.elements[prefix + '_Line1TextBox'].value
    oForm.elements[prefix + '_DeliveryLine2TextBox'].value = oForm.elements[prefix + '_Line2TextBox'].value
    oForm.elements[prefix + '_DeliveryLine3TextBox'].value = oForm.elements[prefix + '_Line3TextBox'].value
    oForm.elements[prefix + '_DeliveryTownTextBox'].value = oForm.elements[prefix + '_TownTextBox'].value
    oForm.elements[prefix + '_DeliveryCountyTextBox'].value = oForm.elements[prefix + '_CountyTextBox'].value
    oForm.elements[prefix + '_DeliveryPostcodeTextBox'].value = oForm.elements[prefix + '_PostcodeTextBox'].value
    oForm.elements[prefix + '_DeliveryPhoneNumberTextBox'].value = oForm.elements[prefix + '_PhoneNumberTextBox'].value
    oForm.elements[prefix + '_DeliveryCountryDropDownList'].selectedIndex = oForm.elements[prefix + '_CountryDropDownList'].selectedIndex

}


function ConfirmOrder() {
    
    var prefix = document.forms['aspnetForm'].elements['jsPrefix'].value;
    var oForm = document.forms['aspnetForm']
    //alert('prefix=' + prefix)
    
    
    var confirmButton =  document.getElementById(prefix + '_OrderOptionsPanelNextButton');
    var cvnNumber = document.getElementById(prefix + '_CVNTextBox').value;
    
    
    //If the issue number is valid too, then disable the button so we dont get multiple submissions
    if (cvnNumber.length == 3)  {
        confirmButton.src = '/images/buttons/pleasewait.gif';
        confirmButton.style.cursor = 'wait';
        //confirmButton.onclick.value = '';
        return true;
        
        //oForm.submit();

        //confirmButton.disabled = true;
    }
}

function SubmitSecureIFrame() {
    document.forms['secureForm'].submit()
}

function openETicketWindow(path) {
    
	if (path) {
		window.open(path,'ETicket','width=590,height=600,scrollbars=yes,resize=no');
    }
    
    //[RK] 21/12/06 - triggered from ImageButton so MUST return false
    return false;
}

function PerformGlossarySearch()    {
    var prefix = document.forms['aspnetForm'].elements['jsPrefix'].value;
    var oForm = document.forms['aspnetForm']
    
    var searchtext = document.getElementById(prefix + '_glossarySearch').value
    window.location = "/glossary/default.aspx?glossaryterm=" + searchtext;
}

//Used for Ajax autocomplete search.
function IAmSelected( source, eventArgs ) {
   //alert( " Key : "+ eventArgs.get_text() +"  Value :  "+eventArgs.get_value()); 
   window.location = "/Search.aspx?event=" + eventArgs.get_value();
}

//Populates a hidden field with all the selected Facebook users


//******We need to populate an ARRAY instead so we can SPLICE and remove items too. Will also be faster.
//Initialise our contact arrays
var minimumContacts = 10;
var myArray = [];
var myWebmailArray = [];

function populateFaceBookHiddenField(fbUserID, clientID, holdingDivID) {

    var userRemoved = false;

    if (myArray.length > 0) {
        var j = 0;
        while (j < myArray.length) {

            //alert(myArray[j]);

            if (myArray[j] == fbUserID) {
                //user already exists so remove
                //alert('Removing...' + myArray[j]);
                myArray.splice(j, 1);

                document.getElementById(holdingDivID).style.border = '1px solid white';

                userRemoved = true;
            }
            j++;
        }

        //alert('User removed = ' + userRemoved);

        if (!userRemoved) {
            
            var currentTotalFriends = getTotalCurrentFriends();

            //check if we have already reached max
            if (currentTotalFriends >= minimumContacts) {
                alert('You have already selected the maximum number of friends required for this offer.\n\n Please click the "Apply Discount" at the top of the page to continue with your discount purchase.');

                //deselect the item
                document.getElementById(holdingDivID).style.border = '1px solid white';
            } else {
                //alert('Adding unremoved user...' + fbUserID);
                myArray.push(fbUserID);
            }
        }
    }
    else {
        //alert('Adding 1st item...' + fbUserID);
        myArray.push(fbUserID);
    }

    checkArrayContents();
    

    return false;
}

function getTotalCurrentFriends() {

    //fb array
    var j = 0;
    while (j < myArray.length) {
        //alert(myArray[j]);
        j++;
    }

    //webmail array
    var k = 0;
    while (k < myWebmailArray.length) {
        //alert(myArray[j]);
        k++;
    }

    return j + k;
}

function checkArrayContents() {
    
    //fb array
    var j = 0;
    while (j < myArray.length) {
        //alert(myArray[j]);
        j++;
    }
    
    //webmail array
    var k = 0;
    while (k < myWebmailArray.length) {
        //alert(myArray[j]);
        k++;
    }

    //populate some hidden fields with our flat array so we can access these on post back in .net
    var prefix = document.forms['aspnetForm'].elements['jsPrefix'].value;
    var oForm = document.forms['aspnetForm']

    
    var totalSelected = j+k;

    document.getElementById('totalUsers').innerHTML = (j + k) + '/' + minimumContacts;

    //var selectMoreUsersObj = document.getElementById('selectMoreUsers')
    //var continueButtonObj = oForm.elements[prefix + '_applyDiscountButton'];
    var continueButtonObj = document.getElementById(prefix + '_applyDiscountButton');
   

    if (totalSelected < minimumContacts) {
        //selectMoreUsersObj.style.visibility = 'visible';
        //selectMoreUsersObj.innerHTML = 'Please select ' + (minimumContacts - totalSelected) + ' more friends to qualify for your discount';

        continueButtonObj.disabled = true;
    }
    else {
        //selectMoreUsersObj.style.visibility = 'hidden';

        //enable continue button
        continueButtonObj.disabled = false;
        continueButtonObj.focus();
//        continueButtonObj.style.backgroundColor = 'red';
//        continueButtonObj.style.border = '3px solid #e60303';
//        continueButtonObj.style.color = 'white';
        continueButtonObj.src = '/images/customeroffer/applydiscount.gif';
    }
    


    oForm.elements[prefix + '_fbArrayHiddenField'].value = myArray.join(",");
    oForm.elements[prefix + '_webmailArrayHiddenField'].value = myWebmailArray.join(",");

    //alert('Hidden field content=' + oForm.elements[prefix + '_fbArrayHiddenField'].value);
    //alert('Hidden field webmail content=' + oForm.elements[prefix + '_webmailArrayHiddenField'].value);
    
}


function toggleImageCss(imageID, cssClass) {
    var element = document.getElementById(imageID);

   // element.setAttribute("class", cssClass);
   // element.setAttribute("className", cssClass);    //For IE

    element.style.border='1px solid red';
}


function populateWebmailArray(emailAddress, clientID, checkboxID) {
    var userRemoved = false;

    if (myWebmailArray.length > 0) {
        var j = 0;
        while (j < myWebmailArray.length) {

            //alert(myArray[j]);

            if (myWebmailArray[j] == emailAddress) {
                //user already exists so remove
                //alert('Removing...' + myWebmailArray[j]);
                myWebmailArray.splice(j, 1);

                //uncheck the checkbox
                document.getElementById(checkboxID).checked = false;

                userRemoved = true;
            }
            j++;
        }

        //alert('User removed = ' + userRemoved);

        if (!userRemoved) {

            var currentTotalFriends = getTotalCurrentFriends();

            //check if we have already reached max
            if (currentTotalFriends >= minimumContacts) {
                alert('You have already selected the maximum number of friends required for this offer.\n\n Please click the "Apply Discount" at the top of the page to continue with your discount purchase.');

                //deselect the item
                document.getElementById(checkboxID).checked = false;
            } else {

                //alert('Adding unremoved user...' + fbUserID);
                myWebmailArray.push(emailAddress);
            }
        }
    }
    else {
        //alert('Adding 1st item...' + emailAddress);
        myWebmailArray.push(emailAddress);
    }


    checkArrayContents();
    

    return false;
}

function ShowTab1(tab1, tab2, tab3, tab4, tab1content, tab2content, tab3content, tab4content) {
    var tab1 = document.getElementById(tab1);
    var tab2 = document.getElementById(tab2);
    var tab3 = document.getElementById(tab3);
    var tab4 = document.getElementById(tab4);
    var tab1content = document.getElementById(tab1content);
    var tab2content = document.getElementById(tab2content);
    var tab3content = document.getElementById(tab3content);
    var tab4content = document.getElementById(tab4content);

    tab2content.style.display = 'none'
    tab3content.style.display = 'none'
    tab4content.style.display = 'none'
    tab1content.style.display = ''
    tab2.style.backgroundColor = '#e3ecf1'
    tab2.style.borderBottom = 'solid 1px #b1bfc7'
    tab2.style.color = "#666666"
    tab3.style.backgroundColor = '#e3ecf1'
    tab3.style.borderBottom = 'solid 1px #b1bfc7'
    tab3.style.color = "#666666"
    tab4.style.backgroundColor = '#e3ecf1'
    tab4.style.borderBottom = 'solid 1px #b1bfc7'
    tab4.style.color = "#666666"
    tab1.style.backgroundColor = '#fff'
    tab1.style.borderBottom = 'solid 1px #fff'
    tab1.style.color = "#000000";

}

function ShowTab2(tab1, tab2, tab3, tab4, tab1content, tab2content, tab3content, tab4content) {
    var tab1 = document.getElementById(tab1);
    var tab2 = document.getElementById(tab2);
    var tab3 = document.getElementById(tab3);
    var tab4 = document.getElementById(tab4);
    var tab1content = document.getElementById(tab1content);
    var tab2content = document.getElementById(tab2content);
    var tab3content = document.getElementById(tab3content);
    var tab4content = document.getElementById(tab4content);

    tab1content.style.display = 'none'
    tab3content.style.display = 'none'
    tab4content.style.display = 'none'
    tab2content.style.display = ''
    tab1.style.backgroundColor = '#e3ecf1'
    tab1.style.borderBottom = 'solid 1px #b1bfc7'
    tab1.style.color = "#666666"
    tab3.style.backgroundColor = '#e3ecf1'
    tab3.style.borderBottom = 'solid 1px #b1bfc7'
    tab3.style.color = "#666666"
    tab4.style.backgroundColor = '#e3ecf1'
    tab4.style.borderBottom = 'solid 1px #b1bfc7'
    tab4.style.color = "#666666"
    tab2.style.backgroundColor = '#fff'
    tab2.style.borderBottom = 'solid 1px #fff'
    tab2.style.color = "#000000";
}

function ShowTab3(tab1, tab2, tab3, tab4, tab1content, tab2content, tab3content, tab4content) {
    var tab1 = document.getElementById(tab1);
    var tab2 = document.getElementById(tab2);
    var tab3 = document.getElementById(tab3);
    var tab4 = document.getElementById(tab4);
    var tab1content = document.getElementById(tab1content);
    var tab2content = document.getElementById(tab2content);
    var tab3content = document.getElementById(tab3content);
    var tab4content = document.getElementById(tab4content);

    tab1content.style.display = 'none'
    tab2content.style.display = 'none'
    tab4content.style.display = 'none'
    tab3content.style.display = ''
    tab1.style.backgroundColor = '#e3ecf1'
    tab1.style.borderBottom = 'solid 1px #b1bfc7'
    tab1.style.color = "#666666"
    tab2.style.backgroundColor = '#e3ecf1'
    tab2.style.borderBottom = 'solid 1px #b1bfc7'
    tab2.style.color = "#666666"
    tab4.style.backgroundColor = '#e3ecf1'
    tab4.style.color = "#666666"
    tab4.style.borderBottom = 'solid 1px #b1bfc7'
    tab3.style.backgroundColor = '#fff'
    tab3.style.borderBottom = 'solid 1px #fff'
    tab3.style.color = "#000000";
}

function ShowTab4(tab1, tab2, tab3, tab4, tab1content, tab2content, tab3content, tab4content) {
    var tab1 = document.getElementById(tab1);
    var tab2 = document.getElementById(tab2);
    var tab3 = document.getElementById(tab3);
    var tab4 = document.getElementById(tab4);
    var tab1content = document.getElementById(tab1content);
    var tab2content = document.getElementById(tab2content);
    var tab3content = document.getElementById(tab3content);
    var tab4content = document.getElementById(tab4content);

    tab1content.style.display = 'none'
    tab3content.style.display = 'none'
    tab2content.style.display = 'none'
    tab4content.style.display = ''
    tab1.style.backgroundColor = '#e3ecf1'
    tab1.style.borderBottom = 'solid 1px #b1bfc7'
    tab1.style.color = "#666666"
    tab3.style.backgroundColor = '#e3ecf1'
    tab3.style.borderBottom = 'solid 1px #b1bfc7'
    tab3.style.color = "#666666"
    tab2.style.backgroundColor = '#e3ecf1'
    tab2.style.borderBottom = 'solid 1px #b1bfc7'
    tab2.style.color = "#666666"
    tab4.style.backgroundColor = '#fff'
    tab4.style.borderBottom = 'solid 1px #fff'
    tab4.style.color = "#000000";
}

function showImage(type, profileimage, imageCounter, imageArray) {
    var myArray = imageArray.split(',');

    if (document.getElementById) {
        if (document.images) {
            if (type == "first") {
                imageNo = 0;
            }
            else if (type == "next") {
                imageNo++;
                if (imageNo >= myArray.length) {
                    imageNo = 0;
                }
            }
            else if (type == "previous") {
                imageNo--;
                if (imageNo < 0) {
                    imageNo = myArray.length - 1;
                }
            }
            document.getElementById(profileimage).src = myArray[imageNo]
            document.getElementById(profileimage).alt = 'Image ' + (imageNo + 1)
            document.getElementById(imageCounter).innerHTML = 'pic ' + (imageNo + 1) + '/' + myArray.length;
        }
        return false;
    }
    return true;
}

//Used in TabbedProfileActions_SearchV2.ascx.
function DisableButton(button) {
    var button = document.getElementById(button);

    button.style.position = "absolute";
    button.style.visibility = "visible";
    button.style.height = "37px";
}

// Admin sidebar slide out div
$(function() {
    $('#slideout').hover(function() {
        $(this).animate({ left: '0px' }, { queue: false, duration: 300 });
    }, function() {
        $(this).animate({ left: '-300px' }, { queue: false, duration: 300 });
    });
}); 

