$(document).ready(function() {
	
	
	//$(".MenuLink-topmenu div").live(function() {
	/*
	$("a.MenuLink-topmenu div").hover(
		function() {
			$(this).css({'background-color':'#7BC7D7'});
		},
		function() {
			$(this).css({'background-color':'transparent'});
		}
	);
	*/
	//});
	
	
	//$(".MenuLink-topmenu div").live('click',function() { alert ('hi'); return false; });
	
	$(".EqualHeightChildren").equalHeights(true,135);
	$("#FauxClear").css({'min-height':'0px'});



    $("#CalculateSubmit").click(function() {

        var TotalAmount = 0;

        $('input[name="Product[]"]').each(function() {
            if ($(this).val() != "") {

                var ProductID = $(this).attr('id').split('_');
                ProductCount = parseInt($(this).val());

                var loadScript = "../PageTemplates/LA/Scripts/CalculateSalePrice.php";
                var dataToSend = "ProductID=" + ProductID[1] + "&Quantity=" + ProductCount;

                $.ajax({
                    type: "GET",
                    async: false,
                    url: loadScript,
                    data: dataToSend,
                    success: function (data) {
                        TotalAmount += parseInt(data);
                    },
                    error: function (data) {
                        alert('failed to load prices');
                    }
                });
            }
        });

        $('input[name="Resin[]"]:checked').each(function() {
            var ProductID = $(this).attr('id').split('_');

            var loadScript = "../PageTemplates/LA/Scripts/CalculateRentalPrice.php";
            var dataToSend = "ProductID=" + ProductID[1] + "&Quantity=1";

            $.ajax({
                type: "GET",
                async: false,
                url: loadScript,
                data: dataToSend,
                success: function (data) {
                    TotalAmount += parseInt(data);
                },
                error: function (data) {
                    alert('failed to load prices');
                }
            });
        });

        var BuildHTML = '<span style="font-size: 16px; font-weight: bold; color: #005285;">R'+TotalAmount.toFixed(2)+'</span>';
        $("#ProductTotal").html(BuildHTML);
    });
    
    $("#OrderFormSubmit").click(function() {                
                
        if (ValidateEmail("Email") == false) { return false;}        
        
        if ($("#FirstName").val() == '') {
            alert('Please enter your First Name.');            
            document.getElementById('FirstName').focus();
            return false;
        } else if ($("#LastName").val() == '') {
            alert('Please enter your Last Name.');            
            document.getElementById('LastName').focus();
            return false;
        } else if ($("#Phone").val() == '') {
            alert('Please enter your Telephone Number.');            
            document.getElementById('Phone').focus();
            return false;
        }
        
        
        $('input[name="Product[]"]').each(function() {
            if ($(this).val() != "") {

                var ProductID = $(this).attr('id').split('_');
                ProductCount = parseInt($(this).val());

                var loadScript = "../PageTemplates/LA/Scripts/AddSalePriceRecord.php";
                var dataToSend = "ProductID=" + ProductID[1] + "&Quantity=" + ProductCount + "&Key=" + $("#Email").val();                

                $.ajax({
                    type: "GET",
                    async: false,
                    url: loadScript,
                    data: dataToSend,
                    success: function (data) {                        
                        //do nothing just add the record
                    },
                    error: function (data) {
                        alert('failed to add prices');
                    }
                });
            }
        });
        
        $('input[name="Resin[]"]:checked').each(function() {
            var ProductID = $(this).attr('id').split('_');

            var loadScript = "../PageTemplates/LA/Scripts/AddRentalPriceRecord.php";            
            var dataToSend = "ProductID=" + ProductID[1] + "&Quantity=1" + "&Key=" + $("#Email").val();                
            
            $.ajax({
                type: "GET",
                async: false,
                url: loadScript,
                data: dataToSend,
                success: function (data) {
                    //do nothing just add the record
                },
                error: function (data) {
                    alert('failed to load prices');
                }
            });
        });
        
        $("#Form-Product").submit();
    });    
});

function EmailCheck(str) {

        var at="@";
        var dot=".";
        var lat=str.indexOf(at);
        var lstr=str.length;
        var ldot=str.indexOf(dot);
        
        if (str.indexOf(at)==-1){
           alert("Invalid E-mail Address");
           return false;
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           alert("Invalid E-mail Address");
           return false;
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            alert("Invalid E-mail Address");
            return false;
        }

         if (str.indexOf(at,(lat+1))!=-1){
            alert("Invalid E-mail Address");
            return false;
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            alert("Invalid E-mail Address");
            return false;
         }

         if (str.indexOf(dot,(lat+2))==-1){
            alert("Invalid E-mail Address");
            return false;
         }
        
         if (str.indexOf(" ")!=-1){
            alert("Invalid E-mail Address");
            return false;
         }
          return true;
    }

function ValidateEmail(email){
    var emailID = document.getElementById(email);
    
    if ((emailID.value==null)||(emailID.value=="")){
        alert("Please Enter your Email Address");
        emailID.focus();
        return false;
    }
    if (EmailCheck(emailID.value)==false){
        emailID.value="";
        emailID.focus();
        return false;
    }
    return true;
 }
