/* 
*  Copyright 2006-2007 Dynamic Site Solutions.
*  Free use of this script is permitted for non-commercial applications,
*  subject to the requirement that this comment block be kept and not be
*  altered.  The data and executable parts of the script may be changed
*  as needed.  Dynamic Site Solutions makes no warranty regarding fitness
*  of use or correct function of the script.  Terms for use of this script
*  in commercial applications may be negotiated; for this, or for other
*  questions, contact "license-info@dynamicsitesolutions.com".
*
*  Script by: Dynamic Site Solutions -- http://www.dynamicsitesolutions.com/
*  Last Updated: 2007-06-17
*/

//IE5+/Win, Firefox, Netscape 6+, Opera 7+, Safari, Konqueror 3, IE5/Mac, iCab 3

var addBookmarkObj = {
  linkText:'Bookmark Page',
  addTextLink:function(parId){
    var a=addBookmarkObj.makeLink(parId);
    if(!a) return;
    a.appendChild(document.createTextNode(addBookmarkObj.linkText));
  },
  addImageLink:function(parId,imgPath){
    if(!imgPath || isEmpty(imgPath)) return;
    var a=addBookmarkObj.makeLink(parId);
    if(!a) return;
    var img = document.createElement('img');
    img.title = img.alt = addBookmarkObj.linkText;
    img.src = imgPath;
    a.appendChild(img);
  },
  makeLink:function(parId) {
    if(!document.getElementById || !document.createTextNode) return null;
    parId=((typeof(parId)=='string')&&!isEmpty(parId))
      ?parId:'addBookmarkPage';
    var cont=document.getElementById(parId);
    if(!cont) return null;
    var a=document.createElement('a');
    a.href=location.href;
    if(window.opera) {
      a.rel='sidebar'; // this makes it work in Opera 7+
    } else {
      // this doesn't work in Opera 7+ if the link has an onclick handler,
      // so we only add it if the browser isn't Opera.
      a.onclick=function() {
        addBookmarkObj.exec(this.href,this.title);
        return false;
      }
    }
    a.title=document.title;
    return cont.appendChild(a);
  },
  exec:function(url, title) {
    // user agent sniffing is bad in general, but this is one of the times 
    // when it's really necessary
    var ua=navigator.userAgent.toLowerCase();
    var isKonq=(ua.indexOf('konqueror')!=-1);
    var isSafari=(ua.indexOf('webkit')!=-1);
    var isMac=(ua.indexOf('mac')!=-1);
    var buttonStr=isMac?'Command/Cmd':'CTRL';

    if(window.external && (!document.createTextNode ||
      (typeof(window.external.AddFavorite)=='unknown'))) {
        // IE4/Win generates an error when you
        // execute "typeof(window.external.AddFavorite)"
        // In IE7 the page must be from a web server, not directly from a local 
        // file system, otherwise, you will get a permission denied error.
        window.external.AddFavorite(url, title); // IE/Win
    } else if(isKonq) {
      alert('Press CTRL + B to bookmark this page.');
    } else if(window.opera) {
      void(0); // do nothing here (Opera 7+)
    } else if(window.home || isSafari) { // Firefox, Netscape, Safari, iCab
      alert('Press '+buttonStr+' + D to bookmark this page.');
    } else if(!window.print || isMac) { // IE5/Mac and Safari 1.0
      alert('Press Command/Cmd + D to bookmark this page.');    
    } else {
      alert('In order to bookmark this page, you need to do so manually '+
        'through your browser.');
    }
  }
}

function isEmpty(s){return ((s=='')||/^\s*$/.test(s));}

function dss_addEvent(el,etype,fn) {
  if(el.addEventListener && (!window.opera || opera.version) &&
  (etype!='load')) {
    el.addEventListener(etype,fn,false);
  } else if(el.attachEvent) {
    el.attachEvent('on'+etype,fn);
  } else {
    if(typeof(fn) != "function") return;
    if(typeof(window.earlyNS4)=='undefined') {
      // to prevent this function from crashing Netscape versions before 4.02
      window.earlyNS4=((navigator.appName.toLowerCase()=='netscape')&&
      (parseFloat(navigator.appVersion)<4.02)&&document.layers);
    }
    if((typeof(el['on'+etype])=="function")&&!window.earlyNS4) {
      var tempFunc = el['on'+etype];
      el['on'+etype]=function(e){
        var a=tempFunc(e),b=fn(e);
        a=(typeof(a)=='undefined')?true:a;
        b=(typeof(b)=='undefined')?true:b;
        return (a&&b);
      }
    } else {
      el['on'+etype]=fn;
    }
  }
}

dss_addEvent(window,'load',addBookmarkObj.addTextLink);

// to make multiple links, do something like this:
/*
dss_addEvent(window,'load',function(){
  var f=addBookmarkObj.addTextLink;
  f();
  f('otherContainerID');
});
*/

// below is an example of how to make an image link with this
// the first parameter is the ID. If you pass an empty string it defaults to
// 'addBookmarkPage'.
/*
dss_addEvent(window,'load',function(){
  addBookmarkObj.addImageLink('','/images/add-bookmark.jpg');
});
*/



function IsValid()
{
	var $return_msg='';
	var $results1;
	var $results2;

	var $email = document.getElementById('email').value;

	if ($email!='')
	{
		$email=TrimString($email)
	}

	var $emailReg = "[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?";
	var $regex = new RegExp($emailReg);
	$results1 = $regex.test($email);
	$results2 = isEmail($email);

	if ( ($results1==false)||($results2==false) )
	{
		$return_msg+= "\nEmail address is invalid";
	}

	if ($return_msg!='')
	{
		alert($return_msg);
		return false;
	}
	else
	{
		return true;
	}

}

function isEmail(email_address)
{

	//Assumes that valid email addresses consist of user_name@domain.tld
	at = email_address.indexOf('@');
	dot = email_address.indexOf('.');

	//Debug('at = x' + at + 'x');
	//Debug('dot = x' + dot + 'x');

	if(at == -1 || dot == -1 || dot == 0 || dot == email_address.length - 1)
	{
		return(false);
	}

	user_name = email_address.substr(0, at);
	domain_name = email_address.substr(at + 1, email_address.length);

	//Debug('Validate_String(user_name) = x' + Validate_String(user_name) + 'x');
	//Debug('Validate_String(domain_name) = x' + Validate_String(domain_name) + 'x');

	if(Validate_String(user_name) === false ||  Validate_String(domain_name) === false)
	{
		return(false);
	}

	return(true);
}

function Validate_String(string, return_invalid_chars)
{
	valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	invalid_chars = '';
	if(string == null || string == '')
	return(true);

	//For every character on the string.
	for(index = 0; index < string.length; index++)
	{
		char = string.substr(index, 1);

		//Is it a valid character?
		if(valid_chars.indexOf(char) == -1)
		{
			//If not, is it already on the list of invalid characters?
			if(invalid_chars.indexOf(char) == -1)
			{
				//If it's not, add it.
				if(invalid_chars == '')
				invalid_chars += char;
				else
				invalid_chars += ', ' + char;
			}
		}
	}

	//If the string does not contain invalid characters, the function will return true.
	//If it does, it will either return false or a list of the invalid characters used
	//in the string, depending on the value of the second parameter.
	if(return_invalid_chars == true && invalid_chars != '')
	{
		last_comma = invalid_chars.lastIndexOf(',');
		if(last_comma != -1)
		invalid_chars = invalid_chars.substr(0, $last_comma) + ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
		return(invalid_chars);
	}
	else
		 return(invalid_chars == '');
}

function TrimString($str)
{
	return $str.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
}


