﻿// JScript File

// Validates the form before it is submitted
function validateForm()
{
    if(!validateUserName())
    {
        return false;       
    }
    if(!validateAge())
    {
        return false;       
    }
    if(!validateTown())
    {
        return false;       
    }
    if(!validateEmail())
    {
        return false;       
    }
    if(!validateComments())
    {
        return false;       
    }
    if(!validateCheckBox())
    {
        return false;       
    }
    // If everything was ok, then we will proceed with encrypting the form and then sending it	
	//cmdEncrypt();
	document.forms[0].submit();
    return true;			
}

// Function that encodes the string base64
function base64encode(str) 
{
	var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var base64DecodeChars = new Array(
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
	-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
	-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
	var out, i, len;
	var c1, c2, c3;

	len = str.length;
	i = 0;
	out = "";
	while(i < len) {
	c1 = str.charCodeAt(i++) & 0xff;
	if(i == len)
	{
		out += base64EncodeChars.charAt(c1 >> 2);
		out += base64EncodeChars.charAt((c1 & 0x3) << 4);
		out += "==";
		break;
	}
	c2 = str.charCodeAt(i++);
	if(i == len)
	{
		out += base64EncodeChars.charAt(c1 >> 2);
		out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
		out += base64EncodeChars.charAt((c2 & 0xF) << 2);
		out += "=";
		break;
	}
	c3 = str.charCodeAt(i++);
	out += base64EncodeChars.charAt(c1 >> 2);
	out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
	out += base64EncodeChars.charAt(c3 & 0x3F);
	}
	return out;
}

// Function that checks if the Comments/Questions textarea is too long
function tooLong(strTest,maxLength)
{
if(strTest.value.length > maxLength)
{
alert("Text is too long and has beeen shortened");
strTest.value = strTest.value.substr(0,maxLength);
}
}

// Checks if a string is empty
function isEmpty(strTextField)
{
	if (strTextField == "" || strTextField==null)
		return true;
	
	var re = /s/g; // Match any white space including space, tab, form-feed, etc.
	RegExp.multiline = true; // IE support 
	var str = strTextField.replace(re, "");
	
	if (str.length == 0) 
		return true;
	else
		return false;
}

// Not needed function that shows the progress
function showProgress(n) 
{
	if (IsLoginStarted)
	{
    	var label = document.getElementById('lblMsg');
		label.innerHTML += '<b>|</b>';
		
		window.setTimeout('showProgress()', 250);
	}
}

// Not needed function that starts a timer				
function startTimer() 
{
	var label = document.getElementById('lblMsg');
	label.innerHTML = '<b>Please wait : </b>';
	
	window.setTimeout('showProgress()', 250);
}


// Checks if the name field is empty
function validateUserName()
{ 
     var tempName=document.forms[0].txtName.value;

     if(isEmpty(tempName))
     {
          alert("Please enter your name.");
          document.forms[0].txtName.focus();
          document.forms[0].txtName.select();
          return false;
     }
     else
     {
     return true;
     }
}

// Checks if the age field is empty
function validateAge()
{ 
     var tempAge=document.forms[0].txtAge.value;

     if(isEmpty(tempAge))
     {
          alert("Please enter your age.");
          document.forms[0].txtAge.focus();
          document.forms[0].txtAge.select();
          return false;
     }
     else
     {
     return true;
     }
}

// Checks if the town field is empty
function validateTown()
{ 
     var tempTown=document.forms[0].txtTown.value;

     if(isEmpty(tempTown))
     {
          alert("Please enter your town.");
          document.forms[0].txtTown.focus();
          document.forms[0].txtTown.select();
          return false;
     }
     else
     {
     return true;
     }
}

// Checks if the email field is empty
function validateEmail()
{ 
    var tempEmail=document.forms[0].txtEmail.value;
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(isEmpty(tempEmail))
    {
        alert("Please enter your email address.");
        document.forms[0].txtEmail.focus();
        document.forms[0].txtEmail.select();
        return false;
    }
    else
    {
        if (filter.test(tempEmail))
        { 
            return true;
        }
        else 
        {
	       alert("Please make sure you have entered a correct email address");
	    }
    }
}

// Checks if the comments/question field is empty
function validateComments()
{ 
     var tempComments=document.forms[0].txtComment.value;

     if(isEmpty(tempComments))
     {
          alert("Please enter your comment/question.");
          document.forms[0].txtComment.focus();
          document.forms[0].txtComment.select();
          return false;
     }
     else
     {
	// "Remove http://" from the email as it causes problems
     	return true;
     }
}

// Checks if the checkbox is ticked
function validateCheckBox()
{
    if(document.forms[0].chkdisclaimer.checked)
    { 
        return true;
    }
    else
    {
        if(document.forms[0].formname.value == "Ask Us")
        {
            alert("We cannot send you an answer unless you tick the checkbox.");
            return false;
        }
        if(document.forms[0].formname.value = "Send a Comment")
        {
            alert("Please tick the checkbox to agree to having your first name and age published on the website.");
            return false;
        }
        if(document.forms[0].formname.value = "Questionnaire")
        {
            alert("Please tick the checkbox to agree to having your first name and age published on the website.");
            return false;
        }
        else
        {
            alert("Please make sure you agree to the disclaimer by ticking the checkbox");
        }
    }
}

