////////////////////////////////////////////////////////////////////////////////////
//        main.js      Version 1.0                                                //
//            Copyright (C) 2006 ClientWorX Business Solutions                    //
//                               All rights reserved.                             //
//          This program is protected by U.S. and International copyright laws.   //
//                   This SOFTWARE PRODUCT is licensed, not sold.                 //
//                        This is not freeware or shareware.                      //
//        For more information please visit us at http://www.clientworx.com       //
//                    YOU MAY NOT REMOVE THIS COPYRIGHT NOTICE                    //
////////////////////////////////////////////////////////////////////////////////////

// modified:
// 11/22/2008  09:42:34

//////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
// changes the text size of the page
function changesize(newSize)
{
  var myElement = document.getElementById('contentID');
  if(myElement) {
	  if(newSize == 1) {
	    myElement.style.fontSize = "11pt";	
	  }
	  if(newSize == 2) {
	    myElement.style.fontSize = "12pt";
	  }
	  if(newSize == 3) {
	    myElement.style.fontSize = "13pt";
	  }
  }
  return;
}

//////////////////////////////////////////////////////////////////////////////////////
function pagePrint()
{
   window.print();
}

//////////////////////////////////////////////////////////////////////////////////////
function mailPage()
{
  mail_str = "mailto:?subject=Check out this page from  " + document.title;
  mail_str += "&body= \nI thought you might be interested in this " + document.title;
  mail_str += ". You can view it at, " + location.href; 
  location.href = mail_str;
}

// the mouse is hovered over our menu item.
//////////////////////////////////////////////////////////////////////////////////////
function MenuItemOver(id)
{
  id.style.color = "red"; 
  id.style.fontWeight = "bold"; 
}

//////////////////////////////////////////////////////////////////////////////////////
function MenuItemOut(id)
{
  id.style.color = "black";
  id.style.fontWeight = "normal"; 
}

//////////////////////////////////////////////////////////////////////////////////////
function CreateBookmarkLink() { 
  
 title = document.title;  
 url = window.location.href; 
  
    if (window.sidebar) { // Mozilla Firefox Bookmark 
        window.sidebar.addPanel(title, url,""); 
    } else if( window.external ) { // IE Favorite 
        window.external.AddFavorite( url, title); } 
    else if(window.opera && window.print) { // Opera Hotlist 
        return true; } 
 } 
 
  
 
//////////////////////////////////////////////////////////////////////////////////////
////////////////////////  form validation functions  /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

// call onsubmit="return validateFormOnSubmit(this)"
//////////////////////////////////////////////////////////////////////////////////////
// generic validation routine decides what fields and types based on the form 
// submitted this allows us to use the same helper functions all through the web site
// and call the correct validation based on the form
function validateFormOnSubmit(theForm) {
  var reason = "";
  var page = ""; // what page did we come from 
  for(prop in theForm) {
      if(prop == "id") {
         page += theForm[prop];
	  }
  }
 
  // first use the page name to make decision on which fields
  switch (page)
  {
	  case "contact_us" : 
		 reason += validateEmpty(theForm.name);
         reason += validateEmail(theForm.email);
		 reason += validateEmpty(theForm.body);
		 break;
	  case "purchase" : 
	     reason += validateEmpty(theForm.first_name);
		 reason += validateEmpty(theForm.last_name);
		 reason += validateEmpty(theForm.address);
		 reason += validateEmpty(theForm.city);
		 reason += validateEmpty(theForm.state);
		 reason += validateEmpty(theForm.zip);
		 reason += validatePhone(theForm.telephone);
		 reason += validateEmail(theForm.email);		 
	     break;
	  default : 
	     alert("There is no such form on our site");
	     break;
  }
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

//////////////////////////////////////////////////////////////////////////////////////
function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "A required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

//////////////////////////////////////////////////////////////////////////////////////
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

//////////////////////////////////////////////////////////////////////////////////////
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

//////////////////////////////////////////////////////////////////////////////////////
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

//////////////////////////////////////////////////////////////////////////////////////
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//////////////////////////////////////////////////////////////////////////////////////
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}
