// 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()
});

/* Site Search */
function checkSiteSearch() {
    if (document.sitesearch.Criteria.value == '' || document.sitesearch.Criteria.value == 'Search site by keywords') {
        alert('Please enter your Search Keywords.');
        return false;
    }
    else {
        return true;
    }
}

/* Tile Finder */
function getCheckedValue(radioObj) {
    if (!radioObj)
        return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined)
        if (radioObj.checked)
        return radioObj.value;
    else
        return "";
    for (var i = 0; i < radioLength; i++) {
        if (radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function lon() {
    document.getElementById("mainload").style.display = 'block';
}

function loff() {
    document.getElementById("mainload").style.display = 'none';
}


function controlTileSearch(control) {
    // HERE
    var colourcount = document.searchform.ColourCount.value;
    var finishcount = document.searchform.FinishCount.value;
    var sizecount = document.searchform.SizesCount.value;
    
    var colquery = '';
    for (var i = 0; i < colourcount; i++) {
        if (document.searchform.ColourID[i].checked) {
            colquery += 'ColourID=' + getCheckedValue(document.searchform.ColourID[i]);
        }
    }

    var finishquery = '';
    for (var i = 0; i < finishcount; i++) {
        if (document.searchform.FinishID[i].checked) {
            finishquery += 'FinishID=' + getCheckedValue(document.searchform.FinishID[i]);
        }
    }
    
    var sizequery = '';
    /*
    for (var i = 0; i < sizecount; i++) {
    if (document.searchform.SizeID[i].checked) {
    sizequery += 'SizeID=' + getCheckedValue(document.searchform.SizeID[i]);
    }
    }
    */

    var sizerangequery = '';
    if (document.searchform.SizeRange.value!== '') {
        sizerangequery += 'SizeRange=' + document.searchform.SizeRange.value;
    }

    var dest = 'Scripts/TileSearch.asp?DoSearch=True';

    if (control) {
        var thecriteria = document.searchform.RangeID.value;
        dest += '&RangeID=' + thecriteria;
        if (thecriteria == '') {
            alert('Please select a Range.');
            return false;
        }
    }
    else {
        if (colquery !== '') {
            dest += '&' + colquery;
        }

        if (finishquery !== '') {
            dest += '&' + finishquery;
        }

        if (sizequery !== '') {
            dest += '&' + sizequery;
        }

        if (sizerangequery !== '') {
            dest += '&' + sizerangequery;
        }
    }

    //alert(dest);

    // Application
    // Material
    // Finish
    // LargeFormat

    //alert(dest);

    // show loading
    lon();

    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("tilefinderright").innerHTML = xmlhttp.responseText;

            // Enable or Disable jQuery
            $(document).ready(function() {
                loff();
            
                $(function() {
                    $('.scroll-pane-results').jScrollPane({
			            showArrows: true,
			            horizontalGutter: 10
			        });        
                });
				
				$('.openbasket').fancybox({
					'titleShow' : false,
					'type' : 'iframe',
					'width'	: 480,
					'height' : 300
				});
				
				
				
            });
            
        }
    }

    //alert(dest);

    // 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);

}

function resetTileSearch() {
    if (confirm('Are you sure you want to reset your search?')) {
        document.searchform.reset();
    }
}

/* Home Subscription Form */
function checkPostCode(toCheck) {

    // Permitted letters depend upon their position in the postcode.
    var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
    var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
    var alpha3 = "[abcdefghjkstuw]";                                // Character 3
    var alpha4 = "[abehmnprvwxy]";                                  // Character 4
    var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5

    // Array holds the regular expressions for the valid postcodes
    var pcexp = new Array();

    // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));

    // Expression for postcodes: ANA NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));

    // Expression for postcodes: AANA  NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));

    // Exception for the special postcode GIR 0AA
    pcexp.push(/^(GIR)(\s*)(0AA)$/i);

    // Standard BFPO numbers
    pcexp.push(/^(bfpo)(\s*)([0-9]{1,4})$/i);

    // c/o BFPO numbers
    pcexp.push(/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

    // Overseas Territories
    pcexp.push(/^([A-Z]{4})(\s*)(1ZZ)$/i);

    // Load up the string to check
    var postCode = toCheck;

    // Assume we're not going to find a valid postcode
    var valid = false;

    // Check the string against the types of post codes
    for (var i = 0; i < pcexp.length; i++) {
        if (pcexp[i].test(postCode)) {

            // The post code is valid - split the post code into component parts
            pcexp[i].exec(postCode);

            // Copy it back into the original string, converting it to uppercase and
            // inserting a space between the inward and outward codes
            postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();

            // If it is a BFPO c/o type postcode, tidy up the "c/o" part
            postCode = postCode.replace(/C\/O\s*/, "c/o ");

            // Load new postcode back into the form element
            valid = true;

            // Remember that we have found that the code is valid and break from loop
            break;
        }
    }

    // Return with either the reformatted valid postcode or the original invalid 
    // postcode
    if (valid) { return postCode; } else return false;
}


function checkHomeSubscribe() {
    if (isValidEmail(document.homesubscribe.EmailAddress.value) == false) {
        alert('Please enter an Email Address.');
        return false;
    }
    else {
        return true;
    }
}

/* Rep Search */
function getLocalRep(thePostcode, postcodeSearch, theId) {
    if (postcodeSearch) {
        if (checkPostCode(thePostcode) == false) {
            alert('Please enter a Valid Postcode.');
            return false;
        }
    }
    else {
        if (theId == '') {
            alert('Please select a Local Representative.');
            return false;
        }
    }

    var dest = 'Scripts/RepSearch.asp?Postcode=' + thePostcode + '&ID=' + theId;

    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		
    }

    //alert(dest);

    xmlhttp.onreadystatechange = function() {
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
            //alert(xmlhttp.responseText)
            document.getElementById("repbottom").innerHTML = xmlhttp.responseText;

            $(document).ready(function() {
                $('#contactbot').slideUp(500);
                $('#repbottom').slideDown(500);
            });

        }
    }

    xmlhttp.open("GET", dest);    
    xmlhttp.send(null);
}

/* Basket Functions */
function removeProductFromBasket(submitstring) {
    if (confirm('Are you sure you want to remove this tile from your basket?')) {
        location.href = submitstring;
    }
}

/* Sample Functions */
function checkregister() {
    var ftxt = '';

    if (document.registerform.Name.value == '' || document.registerform.Name.value == 'Name') {
        ftxt += '\n- Please enter your Name.';
    }

    if (document.registerform.Company.value == '' || document.registerform.Company.value == 'Company') {
        ftxt += '\n- Please enter your Company Name.';
    }

    if (isValidEmail(document.registerform.EmailAddress.value) == false) {
        ftxt += '\n- Please enter your Email Address.';
    }

    if (document.registerform.Telephone.value == '' || document.registerform.Telephone.value == 'Telephone') {
        ftxt += '\n- Please enter your Telephone Number.';
    }

    if (document.registerform.Address.value == '' || document.registerform.Address.value == 'Address') {
        ftxt += '\n- Please enter your Address.';
    }

    if (document.registerform.Postcode.value == '' || document.registerform.Postcode.value == 'Postcode') {
        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;
    }
}

function checklogin() {
    var ftxt = '';

    if (isValidEmail(document.loginform.EmailAddress.value) == false) {
        ftxt += '\n- Please enter an Email Address.';
    }

    if (document.loginform.Password.value == '' || document.loginform.Password.value == 'Password') {
        ftxt += '\n- Please enter a 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;
    }
}

function checkforgottenpassword() {
    if (isValidEmail(document.forgottenpassword.EmailAddress.value) == false) {
        alert('Please enter a Valid Email Address.');
        return false;
    }
    else {
        return true;
    }
}


/* Change Details */
function checkcustomeredit() {
    var ftxt = '';

    if (document.samplesform.Name.value == '') {
        ftxt += '\n- Please enter your Name.';
    }

    if (isValidEmail(document.samplesform.EmailAddress.value) == false) {
        ftxt += '\n- Please enter your Email Address.';
    }

    if (document.samplesform.Telephone.value == '') {
        ftxt += '\n- Please enter your Telephone Number.';
    }

    if (document.samplesform.Address.value == '') {
        ftxt += '\n- Please enter your Address.';
    }

    if (document.samplesform.Postcode.value == '') {
        ftxt += '\n- Please enter your Postcode.';
    }

    if (document.samplesform.Password.value !== document.samplesform.RepeatPassword.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;
    }
}

/* Reqest Samples Page */
function checkrequestsamples() {
    var ftxt = '';

    if (document.samplesform.Name.value == '') {
        ftxt += '\n- Please enter a Name.';
    }

    if (document.samplesform.Telephone.value == '') {
        ftxt += '\n- Please enter a Telephone Number.';
    }

    if (document.samplesform.Address.value == '') {
        ftxt += '\n- Please enter an Address.';
    }

    if (document.samplesform.Postcode.value == '') {
        ftxt += '\n- Please enter a 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;
    }
}

