﻿
function ClearReset(myTxt, myDefault){
        if(myTxt.value == myDefault){
            myTxt.value = '';
            myTxt.style.color = "white";
        }
        else if(myTxt.value == ''){
            myTxt.value = myDefault;
            myTxt.style.color = "#888888";
        }
    }

$(document).ready(function() {

    // MENU ROLLOVER
    $('#mainNav li.menu').hover(
    function() {
        $('li.menu ul').addClass('hover');
    },
    function() {
        $('li.menu ul').removeClass('hover');
    });

    // LIGHTBOX
    $('.thumbnails a').lightBox();
    $('.thumbnails a').click(function(){
        //alert('test');
    });

    // Get the contact form
    var $contactForm = $('.ContactForm');

    $contactForm.keypress(function(e) {
        if (e.which == 13 && e.target.type != 'textarea' && e.target.type != "") {
            contact();
        }
    });

    $("#btnSend").click(function() {
        contact();
        return false;
    });

    var $notifyForm = $('.NotifyForm');

    $contactForm.keypress(function(e) {
        if (e.which == 13 && e.target.type != 'textarea' && e.target.type != "") {
            notify();
        }
    });

    $("#btnSign").click(function() {
        notify();
        return false;
    });

});

 function contact(){
 
 	/* Firstly lets make sure that all the fields are valid */
		error = false;
		$("#contactError").html("");
		if ($("#txtContactName").val() == "Name") {
			$("#contactError").append("Please enter your name<br />");
			error = true;
		}
		if ($("#txtContactEmail").val() == "Email Address") {
			$("#contactError").append("Please enter your email address<br />");
			error = true;
		}
		if ($("#txtContactEmail").val() != "Email Address") {
			if (!isValidEmail($("#txtContactEmail").val())) {
				$("#contactError").append("Please a valid email address<br />");
				error = true;
			}
		}
		if ($("#txtContactMessage").val() == "Message") {
			$("#contactError").append("Please enter your message");
			error = true;
		}
		
		if (!error) {
		
			/* Call the Handler */
			$.ajax({
				type: "POST",
				url: 'index.aspx/sendContact',
				data: '{"name" : "' + $("#txtContactName").val() +
				'", "email" : "' +
				$("#txtContactEmail").val() +
                '", "message" : "' +
				$("#txtContactMessage").val() +
				'" }',
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function(data){
					if (data.d == "Success") {
						$('.contactForm').html("<br /><br /><p class=\"bodytext\">Your message has been sent and you will receive a reply shortly</p>");
					}
					else {
						$('.contactForm').html("<br /><br /><p class=\"bodytext\">Failed to send your message, please try again later</p>");
					}
				}
			});
		}
	}
    
function notify(){

    /* Firstly lets make sure that all the fields are valid */
    error = false;
    $("#notifyError").html("");
    if ($("#txtNotifyName").val() == "Name") {
        $("#notifyError").append("Please enter your name<br />");
        error = true;
    }
    if ($("#txtNotifyEmail").val() == "Email Address") {
        $("#notifyError").append("Please enter your email address<br />");
        error = true;
    }
    if ($("#txtNotifyEmail").val() != "Email Address") {
        if (!isValidEmail($("#txtNotifyEmail").val())) {
            $("#notifyError").append("Please a valid email address<br />");
            error = true;
        }
    }
       
    if (!error) {
    
        /* Call the Handler */
        $.ajax({
            type: "POST",
            url: 'index.aspx/addContactToDB',
            data: '{"name" : "' + $("#txtNotifyName").val() +
            '", "email" : "' +
            $("#txtNotifyEmail").val() +
            '", "phone" : "' +
            $("#txtNotifyPhone").val() +
            '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data){
                if (data.d == "Success") {
                    $('.notifyForm').html("<br /><br /><p class=\"bodytext\">Your details have been added</p>");
                }
                else {
                    $('.notifyForm').html("<br /><br /><p class=\"bodytext\">Failed to add your details, please try again later</p>");
                }
            }
        });
    }
      
}

function isValidEmail(Email){

    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(Email))) {
        return false;
    }
    else {
        return true;
    }
    
}

function popUp(URL, width, height) {
    day = new Date();
    id = day.getTime();
    if (width == null)
    {
        width = 350;
    }
    if (height == null)
    {
        height = 350;
    }

    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",heigh=" + height + ",left = 300,top = 0');");
}