﻿$(document).ready(function() {
    $('#product-detail-sub-images img').hover(
            function() {
                $('#product-detail-main-image img').attr('src', $(this).attr('src').replace('small', 'large'));
            }
    );

    $('li.headlink').hover(
	        function() {
	            $('.mega-dropdown', this).show();
	            $(this).addClass('active');
	            //get the position of the placeholder element
	            var pos = $(this).position();
	            var width = $(this).width();
	            if (pos.left > 800) {//600
	                var megawidth = $('.mega-dropdown', this).width();
	                var left = (width - (megawidth + 1))
	                $('.mega-dropdown', this).css({ left: left });
	                $('.mega-dropdown', this).addClass('mega-dropdown-rev');
	            }
	        },
		    function() {
		        $('.mega-dropdown', this).hide();
		        $(this).removeClass('active');
		    });
    $('li.active').hover(
	        function() {
	            $('.mega-dropdown', this).show();

	            var pos = $(this).position();
	            var width = $(this).width();
	            if (pos.left > 800) {//600
	                var megawidth = $('.mega-dropdown', this).width();
	                var left = (width - megawidth)

	                $('.mega-dropdown', this).css({ left: left });
	                $('.mega-dropdown', this).addClass('mega-dropdown-rev');
	            }
	        },
	        function() {
	            $('.mega-dropdown', this).hide();
	        });
});

//Side navigation menu
$(document).ready(function() {

    //for modal window on checkout page if close button is clicked
    $('.window .close').click(function() {
        $('#mask').hide();
        $('.window').hide();
    });


    $('.ModalSubmit').click(function() {
        var fname = $('#ModalFName').val();
        var lname = $('#ModalLName').val();
        var email = $('#ModalEmail').val();

        if (fname != '' && lname != '' && email != '') {

            var url = "/account/join.asp?e=" + email + "&fn=" + fname + "&ln=" + lname;
            $(location).attr('href', url);
        }
        else {
            $('.MarketingContent .ModalError').html('Please complete all fields');
        }

    });


    //Switch the "Open" and "Close" state per click
    $(".SideMenu ul li.closed .ProductGroup").toggle(function() {
        //$(this).parent().css("background-image", "url(/sites/1000/images/website_images/side-nav-arrow-down.png)");
        $(this).parent().removeClass('closed');
        $(this).parent().addClass('open');
    }, function() {
        //$(this).parent().css("background-image", "url(/sites/1000/images/website_images/side-nav-arrow-right.png)");
        $(this).parent().removeClass('open');
        $(this).parent().addClass('closed');
    });

    $(".SideMenu ul li.open .ProductGroup").toggle(function() {
        //$(this).parent().css("background-image", "url(/sites/1000/images/website_images/side-nav-arrow-right.png)");
        $(this).parent().removeClass('open');
        $(this).parent().addClass('closed');
    }, function() {
        ///$(this).parent().css("background-image", "url(/sites/1000/images/website_images/side-nav-arrow-down.png)");
        $(this).parent().removeClass('closed');
        $(this).parent().addClass('open');
    });

    //Slide up and down on click
    $(".SideMenu ul li .ProductGroup").click(function() {
        $(this).next("ul").slideToggle("fast");
    });


    //Make mega-dropdown fade in/out again
    /*
    $(".nav-image").hover(function() {
    $(".mega-dropdown").fadeIn("fast");
    }, function(){
    $(".mega-dropdown").fadeOut("fast");
    });
    */
    //Make mega-dropdown slide down/up again
    $(".nav-image").hover(function() {
        $(this).addClass("selected");
        $(".mega-dropdown").slideDown("fast");
    }, function() {
        $(this).removeClass("selected");
        $(".mega-dropdown").slideUp("fast");
    });
    /*
    //Make mega-dropdown fade in only
    $(".nav-image").mouseover(function() {
    $(".mega-dropdown").fadeIn("fast");
    });	
    */
    /*
    //Make mega-dropdown slide down only
    $(".nav-image").mouseover(function() {
    $(".mega-dropdown").slideDown("fast");
    });	
    */
});


//modal window
function launchWindow(id, fadeSpeed, bgOcapacity, pos, blnFadeIn) {

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

    if (blnFadeIn) {
        //transition effect
        $('#mask').fadeIn(fadeSpeed);
        $('#mask').fadeTo(fadeSpeed, bgOcapacity);
    }
    else {
        //transition effect
        $('#mask').fadeTo(0, bgOcapacity);
        $('#mask').show();
    }


    var winH = $(window).height();
    var winW = $(window).width();

    switch (pos) {
        case 1:
            $(id).css('bottom', 0);
            $(id).css('left', 0);
            break;
        default:
            $(id).css('top', winH / 2 - $(id).height());
            $(id).css('left', winW / 2 - $(id).width() / 2);
    }

    //transition effect
    $(id).fadeIn(2000);
}


function validateSignupForm() {
	if (document.getElementById('SignupFirstName').value == '') {
		window.alert('Please enter your first name');
		return false;
	}
	if (document.getElementById('SignupLastName').value == '') {
		window.alert('Please enter your last name');
		return false;
	}
	if (document.getElementById('Address1').value == '') {
		window.alert('Please enter your address');
		return false;
	}	
	if (document.getElementById('City').value == '') {
		window.alert('Please enter your city');
		return false;
	}
	if (document.getElementById('Postcode').value == '') {
		window.alert('Please enter your postcode');
		return false;
	}	
	if ((document.getElementById('CountryID').value == 225) && (document.getElementById('StateID').value == 0)) {
		window.alert('Please choose a state for US addresses');
		document.getElementById('Postcode').focus();
		return false;
	}	
	if (document.getElementById('SignupEmail1').value == '') {
		window.alert('Please enter your email address');
		return false;
	}
	if (document.getElementById('SignupEmail2').value == '') {
		window.alert('Please enter the same email address into both boxes');
		return false;
	}
	bEmailAvailable = checkForAvailableEmailAddress(document.getElementById('SignupEmail1').value);
	if (bEmailAvailable == false) {
		window.alert('This email address is already registered on our system');
		return false;
	}
	if (document.getElementById('SignupPassword1').value == '') {
		window.alert('Please enter a password');
		return false;
	}
	sPassword1 = document.getElementById('SignupPassword1').value;
	if (sPassword1.length < 6) {
		window.alert('Your password should be 6 - 10 characters long');
		return false;
	}	
	if (document.getElementById('SignupPassword1').value != document.getElementById('SignupPassword2').value) {
		window.alert('Please enter the same password into both boxes');
		return false;
	}
}

function validateDeliveryAddressForm() {
	if (document.getElementById('FirstName').value == '') {
		window.alert('Please enter a first name');
		document.getElementById('FirstName').focus();
		return false;
	}
	if (document.getElementById('LastName').value == '') {
		window.alert('Please enter a last name');
		document.getElementById('LastName').focus();
		return false;
	}
	if (document.getElementById('Address1').value == '') {
		window.alert('Please enter a postal address');
		document.getElementById('Address1').focus();
		return false;
	}
	if (document.getElementById('City').value == '') {
		window.alert('Please enter a city');
		document.getElementById('City').focus();
		return false;
	}
	if ((document.getElementById('CountryID').value == 225) && (document.getElementById('StateID').value == 0)) {
		window.alert('Please choose a state for US addresses');
		document.getElementById('Postcode').focus();
		return false;
	}
}

function checkForAvailableEmailAddress(sEmail) {
	if (sEmail != '') {
		// Stuff to work out date (needed to make sure IE doesn't mess me about by caching the callbacks)
		var today = new Date();
		var iMinutes = today.getMinutes();
		var iSeconds = today.getSeconds();
		var iMilliseconds = today.getMilliseconds();
		// Make the URL
		var postURL = '/generic/ajax/check_email_address.asp?now=' + iMinutes + iSeconds + iMilliseconds + '&e=' + sEmail;
		if (window.XMLHttpRequest) {
			// mozilla/firefox etc
			xmlhttp = new XMLHttpRequest();
			xmlhttp.open('GET', postURL, false);
			xmlhttp.send(null);
		}
		else if (window.ActiveXObject) {
			// internet explorer
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			if (xmlhttp) {
				xmlhttp.open('GET', postURL, false);
				xmlhttp.send();
			}
		}
		iResult = xmlhttp.responseText;
		if (iResult == 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function validateChangePasswordForm() {
	if (document.getElementById('CurrentPassword').value == '') {
		window.alert('Please enter your current password');
		return false;
	}
	if (document.getElementById('NewPassword1').value == '') {
		window.alert('Please enter a new password');
		return false;
	}
	if (document.getElementById('NewPassword1').value != document.getElementById('NewPassword2').value) {
		window.alert('Please enter the same password into both new password boxes');
		return false;
	}
	sNewPassword1 = document.getElementById('NewPassword1').value;
	if (sNewPassword1.length < 6) {
		window.alert('Your password should be 6 - 10 characters long');
		return false;
	}
}

function validateChangeEmailForm() {
	if (document.getElementById('NewEmail1').value == '') {
		window.alert('Please enter a new email address');
		return false;
	}
	if (document.getElementById('NewEmail1').value == document.getElementById('CurrentEmail').value) {
		window.alert('You have not changed your email address');
		return false;
	}
	bEmailAvailable = checkForAvailableEmailAddress(document.getElementById('NewEmail1').value);
	if (bEmailAvailable == false) {
		window.alert('This email address is already registered on our system');
		return false;
	}
	if (document.getElementById('NewEmail1').value != document.getElementById('NewEmail2').value) {
		window.alert('Please enter the same email into both new email boxes');
		return false;
	}
	if (document.getElementById('Password').value == '') {
		window.alert('Please enter your current password');
		return false;
	}	
}

function validateClassifiedForm() {
	if (document.getElementById('ProductName').value == '') {
		window.alert('Please enter a product name');
		return false;
	}
	if (document.getElementById('ShortDesc').value == '') {
		window.alert('Please enter a short description');
		return false;
	}
	if (document.getElementById('LongDesc').value == '') {
		window.alert('Please enter a long description');
		return false;
	}
	if (document.getElementById('PriceMin').value == '') {
		window.alert('Please enter a price');
		return false;
	}
	if (document.getElementById('PriceMax').value == '') {
		window.alert('Please enter a maximum price');
		return false;
	}
	if (document.getElementById('ContactName').value == '') {
		window.alert('Please enter a contact name');
		return false;
	}
}

function chooseProductForAd(iProductID, sProductName) {
	// Populate the product name and id onto the opener
	window.opener.document.getElementById('ProductID').value = iProductID;
	window.opener.document.getElementById('ProductName').value = sProductName;
	// Show the div and hide the text box
	window.opener.document.getElementById('ProductName').style.display = 'none';
	window.opener.document.getElementById('ChosenProductName').style.display = 'block';
	// Chuck the product name into the div
	if (window.opener.document.getElementById('ChosenProductName').innerHTML != undefined) {
		window.opener.document.getElementById('ChosenProductName').innerHTML = sProductName;
	} else {
	window.opener.document.getElementById('ChosenProductName').textContent = sProductName;
	}
	window.close();
}

function openProductPickerWindow(iProductCategoryId) {
	// Blank out the product name text box and div
	document.getElementById('ProductName').value = '';
	if (document.getElementById('ChosenProductName').innerHTML != undefined) {
		document.getElementById('ChosenProductName').innerHTML = '';
		document.getElementById('ProductName').value = '';
	} else {
		document.getElementById('ChosenProductName').textContent = '';
	}
	// Reset the hidden field
	document.getElementById('ProductID').value = 0;
	// Show the text box and hide the div
	document.getElementById('ProductName').style.display = 'block';
	document.getElementById('ChosenProductName').style.display = 'none';
	// Open a popup window
	window.open('new_product_popup.asp?pc=' + iProductCategoryId, 'productwindow', 'width=400,height=400');
}

// Voting stars functions
function showOnStars(iItemId, iStarId) {
	for (iCount = 1; iCount <= iStarId; iCount++) {
		sSelectedStarDivId = 'Voting_' + iItemId + '_' + iCount;
		//document.getElementById(sSelectedStarDivId).setAttribute("class", "VotingStarOn");
		document.getElementById(sSelectedStarDivId).className = "VotingStarOn";
	}
}
function showOffStars(iItemId, iStarId) {
	for (iCount = 1; iCount <= iStarId; iCount++) {
		sSelectedStarDivId = 'Voting_' + iItemId + '_' + iCount;
		//document.getElementById(sSelectedStarDivId).setAttribute("class", "VotingStarOff");
		document.getElementById(sSelectedStarDivId).className = "VotingStarOff";
	}
}
function rateItem(iItemId, iRating, iNumberOfStars, sPostURL) {
	var sPostURL = sPostURL + '?i=' + iItemId + '&r=' + iRating
	if (window.XMLHttpRequest) {
		// mozilla/firefox etc
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open('GET', sPostURL, false);
		xmlhttp.send(null);
	}
	else if (window.ActiveXObject) {
		// internet explorer
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		if (xmlhttp) {
			xmlhttp.open('GET', sPostURL, false);
			xmlhttp.send();
		}
	}
	iNewAverageRating = xmlhttp.responseText;
	if (iNewAverageRating > 0) {
		// do stuff here to hide the container
		sVotingContainerName = 'VotingStarsContainer_' + iItemId;
		document.getElementById(sVotingContainerName).style.display = 'none';
		// refresh the little rating stars
		for (iCount = 1; iCount <= iNumberOfStars; iCount++) {
			sSelectedRatingStarDivId = 'Rating_' + iItemId + '_' + iCount;
			if (iCount <= iNewAverageRating) {
				//document.getElementById(sSelectedRatingStarDivId).setAttribute("class", "RatingStarOn");
				document.getElementById(sSelectedRatingStarDivId).className = "RatingStarOn";
			} else {
			//document.getElementById(sSelectedRatingStarDivId).setAttribute("class", "RatingStarOff");
			document.getElementById(sSelectedRatingStarDivId).className = "RatingStarOff";
			}
		}
		// Increment the rating counter
		sRatingCounterId = 'Ratings_total_' + iItemId;
		iRatingCounterTotal = document.getElementById(sRatingCounterId).firstChild.nodeValue;
		iRatingCounterTotal = parseInt(iRatingCounterTotal) + 1;
		document.getElementById(sRatingCounterId).firstChild.nodeValue = iRatingCounterTotal;
	}
}

function openNewWindow(sUrl) {
	window.open(sUrl, 'window', 'width=980,height=600,status=yes,location=yes,menubar=yes,scrollbars=yes,resizable=yes');
}

function changeLayout(obj, objClass, sSessionName)
{
    var thisObj = document.getElementById(obj);
    if (thisObj.className != objClass) {
        thisObj.className = objClass;
        
        var today = new Date();
        var iMinutes = today.getMinutes();
        var iSeconds = today.getSeconds();
        var iMilliseconds = today.getMilliseconds();
        // Make the URL
        var postURL = '/generic/ajax/set_session.asp?now=' + iMinutes + iSeconds + iMilliseconds + '&s=' + sSessionName + '&v=' + objClass
        if (window.XMLHttpRequest) {
            // mozilla/firefox etc
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open('GET', postURL, false);
            xmlhttp.send(null);
        }
        else if (window.ActiveXObject) {
            // internet explorer
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
            if (xmlhttp) {
                xmlhttp.open('GET', postURL, false);
                xmlhttp.send();
            }
        }
    }
}

function checkSearchBox() {
	if (document.getElementById('search').value == '') {
		return false;
	}
}

function addToBasket(iProductId, iProductChildId){
	if (iProductId != '') {
		var today = new Date();
		var iMinutes = today.getMinutes();
		var iSeconds = today.getSeconds();
		var iMilliseconds = today.getMilliseconds();
		// Make the URL
		var postURL = '/generic/ajax/add_to_basket.asp?now=' + iMinutes + iSeconds + iMilliseconds + '&p=' + iProductId + '&pc=' + iProductChildId;
		if (window.XMLHttpRequest) {
			// mozilla/firefox etc
			xmlhttp = new XMLHttpRequest();
			xmlhttp.open('GET', postURL, false);
			xmlhttp.send(null);
		}
		else if (window.ActiveXObject) {
			// internet explorer
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			if (xmlhttp) {
				xmlhttp.open('GET', postURL, false);
				xmlhttp.send();
			}
		}
		sResponse = xmlhttp.responseText;
		if (isNaN(sResponse)) {
			window.alert(sResponse);
		} else {
			// Update the basket items total
			if (document.getElementById('BasketCountField').innerHTML != undefined) {
				document.getElementById('BasketCountField').innerHTML = sResponse;
			} else {
				document.getElementById('BasketCountField').textContent = sResponse;
			}
			// Show the basket popup alert
			$('#basket-popup').fadeIn('fast');
		}
	}
}

function setDeliveryAddress(iAddID) {
	iLastSelectedAddressID = document.getElementById('LastSelectedAddressID').value;
	// make the previous selection be white background
	sCustBox = 'Box' + iLastSelectedAddressID;
	document.getElementById(sCustBox).className = 'UnselectedRow';
	//document.getElementById(sCustBox).style.backgroundColor = '#ffffff';
	// Make the current selection be red
	sCustBox = 'Box' + iAddID;
	document.getElementById(sCustBox).className = 'SelectedRow';
	//document.getElementById(sCustBox).style.backgroundColor = '#FFD3D3';
	document.getElementById('LastSelectedAddressID').value = iAddID;
}

function checkReviewForm() {
	// check one of the rating radio buttons has been selected
	iChosenRating = 0;
	arrRatingRadioBoxes = document.getElementsByName('Rating');
	for (i = 0; i < arrRatingRadioBoxes.length; i++) {
		if (arrRatingRadioBoxes[i].checked) {
			iChosenRating = arrRatingRadioBoxes[i].value;
		}
	}
	if (iChosenRating == 0) {
		window.alert('Please choose a rating');
		return false;
	}
	if (document.getElementById('ReviewTitle').value == "") {
		window.alert('Please give your review a title');
		return false;
	}
	if (document.getElementById('ReviewBody').value == "") {
		window.alert('Please enter some review text');
		return false;
	}	
}

function validateCompEntryForm() {
	if (document.getElementById('FirstName').value == '') {
		window.alert('Please enter your first name');
		return false;
	}
	if (document.getElementById('LastName').value == '') {
		window.alert('Please enter your last name');
		return false;
	}
	if (document.getElementById('Email').value == '') {
		window.alert('Please enter your email address');
		return false;
	}
	if (document.getElementById('Address1').value == '') {
		window.alert('Please enter your address');
		return false;
	}
	if (document.getElementById('City').value == '') {
		window.alert('Please enter your city');
		return false;
	}
	if (document.getElementById('Postcode').value == '') {
		window.alert('Please enter your city');
		return false;
	}
	if (document.getElementById('TelHome').value == '') {
		window.alert('Please enter your telephone number');
		return false;
	}
	if (document.getElementById('CompAcceptTerms').checked == false) {
		window.alert('You must accept the competition\'s Terms and conditions');
		return false;
	}
}	