/**
 * Javascript Document by Jeffrey Ouma.
 * Copyright© 2008 artkenya.net Ltd.
 * All rights reserved.
 * 
 * Utilities:
 * ============
 * General functions that are used widely to
 * manipulate text and check conditions
 * 
 */
//Create the Bleez namespace
Bleez = {}

// Create the utilities namespace
Bleez.util = {}

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));
	},
	
	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));
	},
	
	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	
}

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) {
		str = str.replace(/\-/g, "");
		str = Bleez.util.Format.Trim(str);
		return str.toLowerCase();
	}

	// End of Bleez.util.Format
}
	
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
}
	
Bleez.util.Scrub = function(obj){
	if (obj.blur) {
		obj.blur();
	}
	// End of Bleez.util.Scrub
},
	
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.SelectDropDownItem
}
