/*
 * Author      : Zeeshan Muhammad
 * Contact     : Web: www.nfwebsolutions.com - Email: enquiries@nfwebsolutions.com - Voicemail/fax: +44 (0)709 2300 838
 * Version     : 1.00
 * Created     : 05/04/2007
 * Last updated: 05/04/2007
 *
 * CONTENTS
 * -------------------
 * ==METHODS
 * ==over
 *    ==item ADDEVENT
 *    ==item CANCELEVENT
 *    ==item TRIM
 *    ==item ADDCLASS
 *    ==item REMOVECLASS
 *    ==item HASCLASS
 *    ==item EXTERNALLINKS
 *    ==item INCLUDESCRIPT
 *    ==item N
 * ==back
 * ==GENERAL
 * ==_INCLUDESCRIPTS
 * ==_CONTACTFORM
 * ==N
 *
 * NOTES
 * -------------------
 * Methods addClass, removeClass and hasClass have the following text apply to them:
 *     This code is copyright 2002-2003 by Gavin Kistner and Refinery; www.refinery.com
 *     License: http://phrogz.net/JS/_ReuseLicense.txt
 *
 * Method loadScript has the following text apply to it:
 *     Copyright 2006 Dao Gottwald, http://en.design-noir.de/webdev/JS/loadScript/
 */

/* ==METHODS: ADDEVENT
--------------------------------------------------------------------- */
function addEvent(obj, evType, fn) {  
     if (obj.addEventListener){  
          obj.addEventListener(evType, fn, false);  
          return true;  
     } else if (obj.attachEvent){  
          var r = obj.attachEvent('on'+evType, fn);  
          return r;  
     } else {  
          return false;  
     }
}


/* ==METHODS: CANCELEVENT
--------------------------------------------------------------------- */
function cancelEvent(e) {
	e = e || event; var src = e.target || e.srcElement;
	if(typeof e.preventDefault != 'undefined')	{
		 e.preventCapture(); e.preventDefault(); e.preventBubble();
	}
	else {
		 e.returnValue = false;
		 e.cancelBubble = true;
	}
}


/* ==METHODS: TRIM
--------------------------------------------------------------------- */
function trim(value) {
   var re = new RegExp("^(\s*)([\W\w]*)(\b\s*$)");
   return value.replace(re, '$2');
}


/* ==METHODS: ADDCLASS
--------------------------------------------------------------------- */
function addClass(obj,cName) {
	removeClass(obj,cName);
	return obj && (obj.className+=(obj.className.length>0?' ':'')+cName);
}


/* ==METHODS: REMOVECLASS
--------------------------------------------------------------------- */
function removeClass(obj,cName) {
	return obj && (obj.className=obj.className.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''));
}


/* ==METHODS: HASCLASS
--------------------------------------------------------------------- */
function hasClass(obj,cName){
	return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className)
}


/* ==METHODS: EXTERNALLINKS
--------------------------------------------------------------------- */
function externalLinks() {
	if(!document.getElementsByTagName) return;
	var newWindowText = '[External Website: Opens in a new window]';
	var anchors = document.getElementsByTagName('a');
	var external = new RegExp("external");
	var start = new RegExp("^https?://");
	for(var i=0; i<anchors.length; i++) {
		var anchor  = anchors[i];
		var rel     = anchor.getAttribute('rel');
		if(anchor.getAttribute('href') && (external.test(rel))) {
			anchor.target = "_blank";
			if(anchor.title && trim(anchor.title)!='') anchor.title += ' ' + newWindowText;
			else anchor.title = newWindowText;
		}	
	}
}


/* ==METHODS: INCLUDESCRIPT
--------------------------------------------------------------------- */
function loadScript (url, callback) {
	var script = document.createElement('script');
	script.type = 'text/javascript';
	if (callback)
		script.onload = script.onreadystatechange = function() {
			if (script.readyState && script.readyState != 'loaded' && script.readyState != 'complete')
				return;
			script.onreadystatechange = script.onload = null;
			callback();
		};
	script.src = url;
	document.getElementsByTagName('head')[0].appendChild (script);
}


/* ==_INCLUDESCRIPTS
--------------------------------------------------------------------- */
function _includeScripts() {
	// A list of external Javascripts to load into the page which
	// reference global.js - Use includeScript() to load

}


/* ==_CONTACTFORM
--------------------------------------------------------------------- */
function _contactForm () {
	// Check to see if calling page is 'contact us'
	if (hasClass(document.body, 'contactus')) {
		for (var i = 0; i < document.forms.length; i++) {

                         // Avoid all forms other than the contact one
                         if (hasClass(document.forms[i], 'contactus')) {
                              var f = document.forms[i];

                              addEvent(f,'submit',function (e) {

                                   if (f.email.value && f.subject.value && f.captcha.value && f.message.value) {
                                        if (/(?:^|\s)[\-A-Za-z0-9\_\.]+@([\-A-Za-z0-9]+\.)+[A-Za-z]{2,6}(?:\s|$)/.test(f.email.value) == false) {
                                             alert("The email address you provided ('"+ f.email.value +"') is not of a valid syntax.\nExpected format: alias@example.com");
                                             // IE
                                             e.returnValue = false;
                                             // For rest of the W3C compliant browsers
                                             if (e.preventDefault) e.preventDefault();
                                        };
                                   } else {
                                        alert("One or more form fields are empty, fill in all form fields and try submitting again.")

                                        // IE
                                        e.returnValue = false;
                                        // For rest of the W3C compliant browsers
                                        if (e.preventDefault) e.preventDefault();
                                   };
                              });

                              // No need to for() loop anymore, we've found form
                              break;
                         };
		};
	};
}

/* ==GENERAL
--------------------------------------------------------------------- */
addEvent(window,'load',externalLinks);
addEvent(window,'load',_includeScripts);
addEvent(window,'load',_contactForm);
