// JS Functions
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function emailCloak() {
    if (document.getElementById) {
        var alltags = document.all ? document.all : document.getElementsByTagName("*");
        for (i = 0; i < alltags.length; i++) {
            if (alltags[i].className == "emailCloak") {
                var oldText = alltags[i].firstChild;
                var emailAddress = alltags[i].firstChild.nodeValue;
                var user = emailAddress.substring(0, emailAddress.indexOf("("));
                var website = emailAddress.substring(emailAddress.indexOf(")") + 1, emailAddress.length);
                var newText = user + "@" + website;
                var a = document.createElement("a");
                a.href = "mailto:" + newText;
                var address = document.createTextNode(newText);
                a.appendChild(address);
                alltags[i].replaceChild(a, oldText);
            }
        }
    }
}
addLoadEvent(emailCloak);


// JScript source code
function isValidEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
		return true;
	}
	else {
		return false;
	}
}

function checkenquiry(){
	var ftxt = '';

	if (document.enquiry.Name.value==''){
		ftxt += '\n- Please enter your Name.';
	}
	
	if (document.enquiry.Company.value==''){
		ftxt += '\n- Please enter your Company.';
	}
	
	if (document.enquiry.Telephone.value==''){
		ftxt += '\n- Please enter your Telephone Number.';
	}
	
	if (isValidEmail(document.enquiry.email.value)==false){
		ftxt += '\n- Please enter your Email Address.';
	}
	
	if (document.enquiry.subject.value==''){
		ftxt += '\n- Please select an enquiry Subject.';
	}
	
	if (ftxt!==''){
		alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
		return false;
	}
	else {
		return true;
	}
}




function clearemailfield(fieldVal){
	if (isValidEmail(fieldVal)==false){
		document.newsletterform.email.value='';
	}
}

function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

function mainmenu(){
	$(" #navlist ul ").css({display: "none"}); // Opera Fix
	$(" #navlist li").hover(function(){
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(0);
			},function(){
			$(this).find('ul:first').css({visibility: "hidden"});
			});
	}

 $(document).ready(function(){					
	mainmenu()
});

/* Which Form */
function checkwhichform() {
    if (document.whichform.TypeID.value == '' && document.whichform.CollectionID.value == '') {
        return false;
    }
    else {
        return true;
    }
}

/* Quick Basket Form */
function checkQuickBasketForm(theId) {
    if (eval('document.Prod' + theId + '.Qty.value') == '') {
        alert('Please enter a Quantity.');
        return false;
    }
    else {
        return true;
    }
}

/* Quick Search */
function StartQuickSearch() {
    if (checkwhichform()) {
        var dest = '/Scripts/SearchResults.asp?TypeID=' + document.whichform.TypeID.value + '&CollectionID=' + document.whichform.CollectionID.value;

        //alert(document.whichform.CollectionID.value);
        //alert(dest);
    
        try {
            // Moz supports XMLHttpRequest. IE uses ActiveX.
            // browser detction is bad. object detection works for any browser
            xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            // browser doesn't support ajax. handle however you want		
        }
        // the xmlhttp object triggers an event everytime the status changes
        // triggered() function handles the events
        //xmlhttp.onreadystatechange = triggered;	

        xmlhttp.onreadystatechange = function() {
            if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
                //alert(xmlhttp.responseText)
                document.getElementById("SearchResults").innerHTML = xmlhttp.responseText;
            }
        }

        // open takes in the HTTP method and url.
        xmlhttp.open("GET", dest);
        // send the request. if this is a POST request we would have
        // sent post variables: send("name=aleem&gender=male)
        // Moz is fine with just send(); but
        // IE expects a value here, hence we do send(null);
        xmlhttp.send(null);
    }
    else {
        alert('Please specify your Search Criteria.');
        return false;
    }
}

/* Main Login */
function checkMainLogin() {
    var ftxt = '';

    if (isValidEmail(document.mainlogin.EmailAddress.value) == false) {
        ftxt += '\n- Please enter your Email Address.';
    }

    if (document.mainlogin.Password.value == '') {
        ftxt += '\n- Please enter your Password.';
    }

    if (ftxt !== '') {
        alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
        return false;
    }
    else {
        return true;
    }
}

/* Main Register */
function checkRegisterForm() {
    var ftxt = '';

    if (document.registerform.ecomms_Name.value == '') {
        ftxt += '\n- Please enter your Name.';
    }

    if (document.registerform.ecomms_Telephone.value == '') {
        ftxt += '\n- Please enter your Telephone Number.';
    }

    if (document.registerform.ecomms_Mobile.value == '') {
        ftxt += '\n- Please enter your Mobile Number.';
    }

    if (isValidEmail(document.registerform.ecomms_Email.value) == false) {
        ftxt += '\n- Please enter your Email Address.';
    }

    if (document.registerform.ecomms_Address1.value == '') {
        ftxt += '\n- Please enter your Address Line 1.';
    }

    if (document.registerform.ecomms_Town.value == '') {
        ftxt += '\n- Please enter your Town.';
    }

    if (document.registerform.ecomms_County.value == '') {
        ftxt += '\n- Please enter your County.';
    }

    if (document.registerform.ecomms_Postcode.value == '') {
        ftxt += '\n- Please enter your Postcode.';
    }

    if (ftxt !== '') {
        alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
        return false;
    }
    else {
        return true;
    }
}

/* Logout */
function confirmLogout() {
    if (confirm('Are you sure you want to logout?')) {
        location.href = '/Scripts/LoginFunctions.asp?Logout=True';
    }
}

/* Delivery Address */
function deliveryAddressFields(theval) {
    var fieldArr = new Array("Name", "Company", "Address1", "Address2", "Address3", "City", "County", "Postcode", "Country");
    for (var i = 0; i < fieldArr.length; i++) {
        if (theval) {
            eval('document.ordersubmit.Delivery' + fieldArr[i] + '.value=document.ordersubmit.Billing' + fieldArr[i] + '.value');
            document.getElementById("Delivery" + fieldArr[i]).readOnly = true;
        }
        else {
            eval('document.ordersubmit.Delivery' + fieldArr[i] + '.value=""');
            document.getElementById("Delivery" + fieldArr[i]).readOnly = false;
        }
    }
}

/* Check Apply Discount */
var bcheckcards = true;
function checkApplyDiscount() {
    if (document.ordersubmit.DiscountCode.value == '') {
        alert('Please enter a Discount Code.');
        return false;
    }
    else {
        document.ordersubmit.FromForm.value = 'ApplyDiscount';
        bcheckcards = false;
        return true;
    }
}

function submitTheOrder() {
    bcheckcards = true;
}

/* Order Submit */
function checkordersubmit() {
    var ftxt = '';

    /* Billing */
    if (document.ordersubmit.CheckBilling.value == 'True') {
        if (document.ordersubmit.BillingName.value == '') {
            ftxt += '\n- Please enter a Billing Name.';
        }

        if (document.ordersubmit.BillingAddress1.value == '') {
            ftxt += '\n- Please enter a Billing Address 1.';
        }

        if (document.ordersubmit.BillingCity.value == '') {
            ftxt += '\n- Please enter a Billing City.';
        }

        if (document.ordersubmit.BillingCounty.value == '') {
            ftxt += '\n- Please enter a Billing County.';
        }

        if (document.ordersubmit.BillingPostcode.value == '') {
            ftxt += '\n- Please enter a Billing Postcode.';
        }

        if (isValidEmail(document.ordersubmit.BillingEmailAddress.value) == false) {
            ftxt += '\n- Please enter your Email Address.';
        }

        if (document.ordersubmit.BillingTelephone.value == '') {
            ftxt += '\n- Please enter a Billing Telephone Number.';
        }
    }

    if (ftxt !== '') {
        ftxt = '\n** Billing Details **\n' + ftxt + '\n\n** Delivery Details **\n';
    }

    /* Delivery */
    if (document.ordersubmit.DeliveryName.value == '') {
        ftxt += '\n- Please enter a Delivery Name.';
    }

    if (document.ordersubmit.DeliveryAddress1.value == '') {
        ftxt += '\n- Please enter a Delivery Address 1.';
    }

    if (document.ordersubmit.DeliveryCity.value == '') {
        ftxt += '\n- Please enter a Delivery City.';
    }

    if (document.ordersubmit.DeliveryCounty.value == '') {
        ftxt += '\n- Please enter a Delivery County.';
    }

    if (document.ordersubmit.DeliveryPostcode.value == '') {
        ftxt += '\n- Please enter a Delivery Postcode.';
    }
    
    if (ftxt !== '') {
        alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
        return false;
    }
    else {
        if (bcheckcards) {
            var x = false;

            for (var i = 0; i < 3; i++) {
                if (document.ordersubmit.PaymentMethod[i].checked) {
                    x = true;
                }
            }

            if (x == false) {
                alert('Please select a Payment Method.');
                return false;
            }
            else {
                return true;
            }
        }
        else {
            return true;
        }
    }
}

/* Update Form */
function checkUpdateForm() {
    var ftxt = '';

    if (document.registerform.ecomms_Name.value == '') {
        ftxt += '\n- Please enter your Name.';
    }

    if (document.registerform.ecomms_Telephone.value == '') {
        ftxt += '\n- Please enter your Telephone Number.';
    }

    if (document.registerform.ecomms_Mobile.value == '') {
        ftxt += '\n- Please enter your Mobile Number.';
    }

    if (isValidEmail(document.registerform.ecomms_Email.value) == false) {
        ftxt += '\n- Please enter your Email Address.';
    }

    if (document.registerform.ecomms_Address1.value == '') {
        ftxt += '\n- Please enter your Address Line 1.';
    }

    if (document.registerform.ecomms_Town.value == '') {
        ftxt += '\n- Please enter your Town.';
    }

    if (document.registerform.ecomms_County.value == '') {
        ftxt += '\n- Please enter your County.';
    }

    if (document.registerform.ecomms_Postcode.value == '') {
        ftxt += '\n- Please enter your Postcode.';
    }

    if (document.registerform.Password.value !== document.registerform.PasswordRpt.value) {
        ftxt += '\n- Please ensure both New Passwords Match.';
    }

    if (ftxt !== '') {
        alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
        return false;
    }
    else {
        return true;
    }
}

/* Register Controls */
function controlRegisterForm() {
    var detailbox = document.getElementById("detailbox");

    if (detailbox.style.display == 'none') {
        detailbox.style.display = 'block';
    }
    else {
        detailbox.style.display = 'none';
    }
}

/* Contact Form */
function checkContactForm() {
    var ftxt = '';

    if (document.contactform.Name.value == '') {
        ftxt += '\n- Please enter your Name.';
    }

    if (document.contactform.Company.value == '') {
        ftxt += '\n- Please enter your Company Name.';
    }

    if (isValidEmail(document.contactform.Email.value) == false) {
        ftxt += '\n- Please enter your Email Address.';
    }

    if (document.contactform.Telephone.value == '') {
        ftxt += '\n- Please enter your Mobile Number.';
    }

    if (document.contactform.Enquiry.value == '') {
        ftxt += '\n- Please enter your Enquiry.';
    }

    if (ftxt !== '') {
        alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
        return false;
    }
    else {
        return true;
    }
}

$(document).ready(function(){
	$('#navbox1').hide();
	$('#navbox2').hide();
	$('#navbox3').hide();
	$('#navbox4').hide();
	$('#navbox5').hide();
	$('#navbox6').hide();
	
	$('#navbox1,#navbox2,#navbox3,#navbox4,#navbox5,#navbox6').hover(function(){
		$(this).show();   
   },function(){
		$(this).hide();   
	});
	
	$('.area1').hover(function(){
		$('#navbox1').slideDown(0);
	},function(){
		$('#navbox1').slideUp(0);
	});
	
	$('.area2').hover(function(){
		$('#navbox2').slideDown(0);
	},function(){
		$('#navbox2').slideUp(0);
	});
	
	$('.area3').hover(function(){
		$('#navbox3').slideDown(0);
	},function(){
		$('#navbox3').slideUp(0);
	});
	
	$('.area4').hover(function(){
		$('#navbox4').slideDown(0);
	},function(){
		$('#navbox4').slideUp(0);
	});
	
	$('.area5').hover(function(){
		$('#navbox5').slideDown(0);
	},function(){
		$('#navbox5').slideUp(0);
	});
	
	$('.area6').hover(function(){
		$('#navbox6').slideDown(0);
	},function(){
		$('#navbox6').slideUp(0);
	});
	
	$('.navtwo').hover(function(){
		$('#navbox').slideDown(0);
	},function(){
		$('#navbox').slideUp(0);
	});
	
	/* Reviews Java Start */
	
	$('#tabone').show();
	$('#tabtwo').hide();
	$('#tabthree').hide();
	
	$('.tabone').addClass('selected');
	
	$('.tabone a').click(function(){
		$('#tablist li').removeClass('selected');
		$(this).parent().addClass('selected');   
		$('#tabone').show();
		$('#tabtwo').hide();
		$('#tabthree').hide();
	});
	
	$('.tabtwo a').click(function(){
		$('#tablist li').removeClass('selected');
		$(this).parent().addClass('selected');   
		$('#tabone').hide();
		$('#tabtwo').show();
		$('#tabthree').hide();
	});
	
	$('.tabthree a').click(function(){
		$('#tablist li').removeClass('selected');
		$(this).parent().addClass('selected');   
		$('#tabone').hide();
		$('#tabtwo').hide();
		$('#tabthree').show();
	});
	
	/* Reviews Java Finish */

});

/* Review Form */
function checkReviewForm() {
    ftxt = '';

    if (document.reviewform.Name.value == '') {
        ftxt += '\n- Please enter your Name.';
    }

    if (document.reviewform.Location.value == '') {
        ftxt += '\n- Please enter your Location.';
    }

    if (document.reviewform.TheRating.value == '') {
        ftxt += '\n- Please select a Rating.';
    }

    if (ftxt !== '') {
        alert('One or more errors were found while submitting this form. The errors found are displayed below.\n' + ftxt + '\n\nPlease correct the above errors and try again.');
        return false;
    }
    else {
        return true;
    }
}
