/***************************************************************************************/
// General functions
/***************************************************************************************/

//Flash functions

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a date entered is a valid date or not.
/////////////////////////////////////////////////////////////////////////////////////////
function checkDate(ddVal, mmVal, yyVal) {
	if(ddVal <= 0 || ddVal > 31 || mmVal <= 0 || mmVal > 12 || yyVal <= 0 || yyVal.length != 4) {
		alert("Please enter a valid date");
		return false;
	}
	monthArray = new Array("January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	if(mmVal == 2) {
		if(yyVal % 4 == 0) {
			if(ddVal > 29) {
				alert("February " + yyVal + " has only 29 days");
				return false;
			}
		}
		else {
			if(ddVal > 28) {
				alert("February " + yyVal + " has only 28 days");
				return false;
			}
		}
	}
	else {
		if(mmVal == 4 || mmVal == 6 || mmVal == 9 || mmVal == 11) {
			if(ddVal > 30) {
				alert(monthArray[mmVal-1] + " has only 30 days");
				return false;
			}
		}
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a string is a valid email address.
/////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(emailString) {
	splitVal = emailString.split('@');
	if(splitVal.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
		alert("Please enter a valid email address");
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Removes the leading and trailing spaces in a strings and returns the trimmed string
/////////////////////////////////////////////////////////////////////////////////////////
function trimSpaces(stringValue) {
	// Checks the first occurance of spaces and removes them
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Check whether a string contain permitted characters only
/////////////////////////////////////////////////////////////////////////////////////////
function checkAllowedChars(strToCheck, allowedChars)
{
     var acLen     = allowedChars.length;
     var stcLen     = strToCheck.length;
     strToCheck     = strToCheck.toLowerCase();
     var i;
     var j;
     var rightCount = 0;
     for(i = 0; i < acLen; i++)
     {
          switch(allowedChars.charAt(i))
          {
          case 'A':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= 'a' && strToCheck.charAt(j) <= 'z';
               }
               break;
          case 'N':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= '0' && strToCheck.charAt(j) <= '9';
               }
               break;
          default:
               for(j = -1; -1 != (j = strToCheck.indexOf(allowedChars.charAt(i), j + 1)); rightCount++);
               break;
          }
     }
     if(rightCount == stcLen)
     {
          return true;
     }
     return false;
}

//contact us form functions.
v_fields = new Array('sender_name','sender_email','sender_message');alert_on = true;thanks_on = true; thanks_message = "Thank you. Your message has been sent.";	
function validateForm(){

//alert(v_fields);

//init errors
var err = "";

//start checking fields
for(i=0;i<v_fields.length;i++)
{
	//store the field value
	var _thisfield = eval("document.contact."+v_fields[i]+".value");
	
	//check the field value
	if(v_fields[i] == "sender_name")
	{
		if(!isAlpha(_thisfield))
		{
			err += "Please enter a valid name\n";
		}
	}
	else if(v_fields[i] == "sender_subject")
	{
		if(!isAlpha(_thisfield))
		{
			err += "Please enter a valid subject\n";
		}
	}
	else if(v_fields[i] == "sender_email")
	{
		if(!isEmail(_thisfield))
		{
			err += "Please enter a valid email address\n";
		}
	}
	else if(v_fields[i] == "sender_message")
	{
		if(!isText(_thisfield))
		{
			err += "Please enter a valid message\n";
		}
	}
	
}//end for

if(err != "")
{ 
	if(alert_on)
	{
		alert("The following errors have occurred\n"+err);
	}
	else
	{
		showErrors(err);
	}
	return false;
}

return true;}

//function to show errors in HTML
function showErrors(str)
{
	var err = str.replace(/\n/g,"<br />");
	document.getElementById("form_errors").innerHTML = err;
	document.getElementById("form_errors").style.display = "block";
}

//function to show thank you message in HTML
function showThanks(str)
{
	var tym = str.replace(/\n/g,"<br />");
	document.getElementById("form_thanks").innerHTML = tym;
	document.getElementById("form_thanks").style.display = "block";
}

function isEmail(str)
{
	if(str == "") return false;
	var regex = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return regex.test(str);
}

function isText(str)
{
	if(str == "")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isURL(str)
{
	var regex = /[a-zA-Z0-9\.\/:]+/
	return regex.test(str);
}

// returns true if the number is formatted in the following ways:
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhone(str)
{
	var regex = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return regex.test(str);
}

// returns true if the string contains A-Z, a-z or 0-9 or . or # only
function isAddress(str)
{
	var regex = /[^a-zA-Z0-9\#\.]/g
	if (regex.test(str))
	{
		return true;
	}
	else
	{
		return false;
	}
}

// returns true if the string is 5 digits
function isZip(str)
{
	var regex = /\d{5,}/;
	if(regex.test(str))
	{
		return true;
	}
	else
	{
		return false;
	}
}

// returns true if the string contains A-Z or a-z only
function isAlpha(str)
{
	var regex = /[a-zA-Z]/g
	if (regex.test(str))
	{
		return true;
	}
	else
	{
		return false;
	}
}

// returns true if the string contains A-Z or a-z or 0-9 only
function isAlphaNumeric(str)
{
	var regex = /[^a-zA-Z0-9]/g
	if (regex.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}
}