/*
 * JavaScript by Jeffrey Ouma.
 * Copyright© 2008 artkenya.net Ltd.
 * All rights reserved.
 */
/**
 * YAHOO GLOBAL VARIABLES
 * ======================
 */

Connect = YAHOO.util.Connect;
Dom = YAHOO.util.Dom;
Easing = YAHOO.util.Easing;
Event = YAHOO.util.Event;

/**
 * BLEEZ NAMESPACE
 * ===============
 * The namespace that holds all common site functions.
 */

BLEEZ = {};


/**
 * UTILITIES
 * =========
 * Defines common utility functions that help with the deployment of the CMS sitewide.
 */

BLEEZ.util = {};


/**
 * SCRUB LINKS 
 * ===========
 * Removes the dotted border that appears around links after they have been clicked.
 */

BLEEZ.util.Scrub = function(obj){
	if (obj.blur) {
		obj.blur();
	}
	// End of BLEEZ.util.Scrub
};


/**
 * DIMENSIONS
 * ==========
 * An extension of YUI's getRegion Dom utility.
 * Returns left, right, top and bottom coordinates as well as height and width dimensions.
 */

BLEEZ.util.Dimensions = function(elm) {
	var d = new Array;
	var region = Dom.getRegion(elm);
	
	d["left"] = region.left;
	d["right"] = region.right;
	d["width"] = region.right - region.left;

	d["top"] = region.top;
	d["bottom"] = region.bottom;
	d["height"] = region.bottom - region.top;
	
	return d;
	
	// End of BLEEZ.util.Dimensions
};


/**
 * SCROLL
 * ======
 * Set of functions to scroll the main window.
 */

BLEEZ.util.scroll = {
	GetXY: function() {
		var x = 0, y = 0;
		if (typeof(window.pageYOffset) == 'number') {
			//Netscape compliant
			y = window.pageYOffset;
			x = window.pageXOffset;
		} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			//DOM compliant
			y = document.body.scrollTop;
			x = document.body.scrollLeft;
		} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
			//IE6 standards compliant mode
			y = document.documentElement.scrollTop;
			x = document.documentElement.scrollLeft;
		}
		return [x, y];
	},
	Up: function(limit, interval) {
		var s = BLEEZ.util.scroll.GetXY();
		BLEEZ.util.scroll.interval = parseInt(s[1]/interval);
		if(s[1] > limit) {
			scroll(0, s[1] - BLEEZ.util.scroll.interval);
		} else {
			clearTimeout(BLEEZ.util.scroll.to);
			return false;
		}
		BLEEZ.util.scroll.to = setTimeout("BLEEZ.util.scroll.Up(" + limit + ", " + interval + ")", 1);
	},
	Down: function(limit, interval) {
		var s = BLEEZ.util.scroll.GetXY();
		BLEEZ.util.scroll.interval = parseInt(limit/interval);
		if(s[1] < limit) {
			scroll(0, s[1] + BLEEZ.util.scroll.interval);
		} else {
			clearTimeout(BLEEZ.util.scroll.to);
			return false;
		}
		BLEEZ.util.scroll.to = setTimeout("BLEEZ.util.scroll.Up(" + limit + ", " + interval + ")", 1);
	}
};

/**
 * KeyCode
 * =======
 * Checks keypress events for non-fuction characters.
 * Returns true if a letter, number, space or special character was pressed.
 */

BLEEZ.util.KeyCode = function(c) {
	if(c > 46 && c != 91 && c != 92 && c != 93 
		&& c != 112 && c != 113 && c != 114 && c != 115 
		&& c != 116 && c != 117 && c != 118 && c != 119 
		&& c != 120 && c != 121 && c != 122 && c != 123 
		&& c != 144 && c != 145) {
		return true;	
	} else {
		return false;
	}
	
	// End of BLEEZ.util.KeyCode
};


/************************************************************************************
 * REGULAR EXPRESSIONS
 ************************************************************************************
 * These functions use a set of predefined regular expressions to validate common 
 * form fields. Used mainly in conjunction with the BLEEZ.form
 ************************************************************************************/

BLEEZ.util.regX = {
	
	Name:function(str) {
		var re = /^([A-Za-z]([A-Za-z\-\']*[A-Za-z\',\.]\s?)?)+$/;
		return re.test(BLEEZ.util.format.Trim(str));
	},
	
	Surname:function(str) {
		var re = /^([A-Za-z]([A-Za-z\-\']*[A-Za-z\']\s?)?)+$/;
		return re.test(BLEEZ.util.format.Trim(str));
	},
	
	Password:function(str) {
		var re = /^[A-Za-z0-9\!\@\#\$\%\^\&\*]{4,32}$/;
		return re.test(BLEEZ.util.format.Trim(str));
	},
	
	Email:function(str) {
		var re = /^[A-Za-z0-9]([A-Za-z0-9_\-\.]*[A-Za-z0-9])?@[A-Za-z0-9]([A-Za-z0-9_\-\.\']*\.)+[A-Za-z0-9]+$/;
		return re.test(BLEEZ.util.format.Trim(str));
	},
	
	URIDomain:function(str) {
		var re = new RegExp("^(((ht|f)tp(s?))\\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\\-\\.]+\\.([A-Za-z]{2,3})(\\.[A-Za-z]{2})?(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\;\\?\\'\\\\\\+&amp;%\\$#\\=~_\\-]+))*$");
		return re.test(BLEEZ.util.format.Trim(str));
	},

	URIScheme:function(str) {
		var re = new RegExp("^(((ht|f)tp(s?))\\://)");
		return re.test(BLEEZ.util.format.Trim(str));
	},
	
	Phone:function(str) {
		var re = /^(((\+?\d{2,3}(\s|(\s?\-\s?)))?((\(\d{1,4}\)\s*)|(\d{1,4}(\s|(\s?\-\s?)))))?\d{4,}(\-\d{1,4})?(\s([xX]|[eE][xX][tT])\.*\s(\d{1,7}))?((\s*,\s*)|(\s*\/\s*)|(\s*)))+$/;
		return re.test(BLEEZ.util.format.Trim(str));
	},
	
	Integer:function(str, limit) {
		if(limit) {
			var re =  /^\d{1," + limit + "}$/;
			return re.test(BLEEZ.util.format.Trim(str));
		} else {
			var re =  /^\d$/;
			return re.test(BLEEZ.util.format.Trim(str));
		}
	},
	
	Decimal:function(str) {
		str = BLEEZ.util.format.Trim(str);
		if(str) {
			var re =  /^\d+(\.\d+)?$/;
			return re.test(str);
		}
		return true;
	},
	
	DefaultValue: function(str1, str2) {
		return (BLEEZ.util.format.DefaultValue(str1) == BLEEZ.util.format.DefaultValue(str2));
	},
	
	PasswordStrength: function(str) {
		var re, score;
		if(!str) {str = "";}
		// If the password is valid
		if(!BLEEZ.util.regX.Password(str)) {
			return 0;
		} else {
			str = BLEEZ.util.format.Trim(str);
			score = 1;
			
			// If the password is 7 or more chars, credit one score
			if(str.length > 6) {
				score += 1;
			}
			
			// If the password has a mix of upper and lower case letters
			re=/[a-z]+/;
			var result=re.test(str);
			re=/[A-Z]+/;
			var result2=re.test(str);
			if(result&&result2) {
				score += 1;
			}
			
			// If the password contains numbers
			re=/[0-9]+/;
			if(re.test(str)) {
				score += 1;
			}
			
			// If the password contains special chars
			re=/[\!\@\#\$\%\^\&\*]+/;
			if(re.test(str))
				score += 1;
			
			return score * 20 / 100;
		}
	}
	// End of BLEEZ.util.regX	
};


/************************************************************************************
 * TEXT FORMATTING
 ************************************************************************************
 * These functions apply special formatting to text e.g. title case and trim
 ************************************************************************************/

BLEEZ.util.format = {
	
	Title:function(str){
		var newStr, strArr = str.split(" ");
		for(var i = 0; i < strArr.length; i++) {
			var s = strArr[i].substr(0,1);
			if(i == 0) {
				newStr = "";
			} else {
				newStr += " ";
			}
			newStr += s.toUpperCase() + strArr[i].substr(1, strArr[i].length - 1);
			
		}
		return newStr;
	},
	
	Trim:function(str) {
		if(str) {
			// Remove leading spaces
			str = str.replace(/^\s+/, "");
			// Remove multiple spaces in between
			str = str.replace(/\s{2,}/g, " ");
			// Remove trailing spaces
			str = str.replace(/\s+$/, "");
		}
		return str;
	},
	
	DefaultValue:function(str) {
		if(str) {
			str = str.replace(/\-/g, "");
			str = BLEEZ.util.format.Trim(str);
			return str.toLowerCase();
			
		} else {
			return false;
		}
	}

	// End of BLEEZ.util.format
};
	

/************************************************************************************
 * READ XML
 ************************************************************************************
 * These functions assist in reading results from XML
 ************************************************************************************/

BLEEZ.util.readXML = {
	// Methods for reading xml documents returned by async requests
	
	Data: function(xmlResponse, tag){
		// Retrieve the value of a node by referencing the tagname
		if (xmlResponse.getElementsByTagName(tag).length == 0) {
			return "";
		}
		else 
			if (xmlResponse.getElementsByTagName(tag).item(0).firstChild == null) {
				return "";
			}
			else {
				return xmlResponse.getElementsByTagName(tag).item(0).firstChild.data;
			}
		// End of BLEEZ.util.readXML.Data
	},
	
	Array: function(xmlResponse, tag){
		// Retrieve the value of a node by referencing the tagname
		if (xmlResponse.getElementsByTagName(tag).length == 0) {
			return false;
		}
		
		var xmlParent = xmlResponse.getElementsByTagName(tag).item(0);
		var xmlArray = new Array();
		
		if (!xmlParent.childNodes) {
			return false;
		}
		
		for (var i = 0; i < xmlParent.childNodes.length; i++) {
			if (xmlParent.childNodes[i].childNodes.length) {
				xmlArray[xmlParent.childNodes[i].tagName] = xmlParent.childNodes[i].firstChild.data;
			}
		}
		return xmlArray;
		
		// End of BLEEZ.util.readXML.Array
	}
	
	// End of BLEEZ.util.readXML
};
	


/**
 * SET DROPDOWN ITEM
 * =================
 * Sets the selected property of a given dropdown (select) menu item.
 */

BLEEZ.util.SetDropDownItem = function(target, x){
	if (!target.options) {
		return;
	}
	for (var i = 0; i < target.options.length; i++) {
		if (target.options[i].value == x) {
			target.options[i].selected = true;
		}
	}
	// End of BLEEZ.util.SetDropDownItem
};
		
/**
 * FILE SIZE
 * =========
 * Converts a raw file size into a more meningful number denoted by a byte unit.
 * Arguments:
 * fSize - the raw file size.
 */ 
BLEEZ.util.FileSize = function (fSize)	{
	var suffix = " bytes";
	if(fSize > 1023) {
		suffix = " KB";
		fSize = fSize / 1024;
	}
	
	if(fSize > 1023) {
		suffix = " MB";
		fSize = fSize / 1024;
	}
	
	if(fSize > 1023)	{
		suffix = " GB";
		fSize = fSize / 1024;
	}
	
	if(fSize > 1023)	{
		suffix = " TB";
		fSize = fSize / 1024;
	}
	
	fSize = fSize.toFixed(2);
	fSize = fSize + suffix;
	
	return fSize;			
};


/************************************************************************************
 * BROWSER DETECTION
 ************************************************************************************
 * NOTE: This script will only continue to work if you regularly check whether newer 
 * browsers still follow the rules set forth in the dataBrowser and dataOS arrays. 
 * The dataBrowser array is filled with objects that contain the properties 
 * that help the script detect your users' browser.
 ************************************************************************************/

BLEEZ.util.browserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Google Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Internet Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
	// End of BLEEZ.util.browserDetect
};
