YAHOO.namespace ("Unitutor.Util");

YAHOO.Unitutor.Util.redirectToUrl = function(url) {
	window.location.href = url;
};

YAHOO.Unitutor.Util.padDigits = function(n, totalDigits) 
{ 
    n = n.toString(); 
    var pd = ''; 
    if (totalDigits > n.length) 
    { 
        for (i=0; i < (totalDigits-n.length); i++) 
        { 
            pd += '0'; 
        } 
    } 
    return pd + n.toString(); 
};

YAHOO.Unitutor.Util.getValueFromSelect = function(domId) {

	var pagesObj = document.getElementById(domId);
	return pagesObj.options[pagesObj.selectedIndex].value; 

};

YAHOO.Unitutor.Util.popupWindow = function(url_add, width, height) { 
	window.open(url_add,'welcome', 'width=' + width + ',height=' + height);
	//,menubar=yes,status=yes, location=yes,toolbar=yes,scrollbars=yes
};

YAHOO.Unitutor.Util.getCurrentUrl = function() {
	return window.location.href;	
};

YAHOO.Unitutor.Util.getUrlParameter = function(name) {
	
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  
  var urlToParse = window.location.href;
  var results = regex.exec( urlToParse );
  
  if( results == null ) {
    return "";
  }
  else {
    return results[1];
  }
};

YAHOO.Unitutor.Util.getParsedUri = function() {
	return YAHOO.Unitutor.Util.parseUri(YAHOO.Unitutor.Util.getCurrentUrl());
};

YAHOO.Unitutor.Util.isValidEmail = function(email) {
	return (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

/*
parseUri 1.2.1
(c) 2007 Steven Levithan <stevenlevithan.com>
MIT License
*/

YAHOO.Unitutor.Util.parseUri = function(str) {
	var	o   = YAHOO.Unitutor.Util.parseUri.options,
	m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
	uri = {},
	i   = 14;
	
	while (i--) uri[o.key[i]] = m[i] || "";
	
	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	
	return uri;
};

YAHOO.Unitutor.Util.parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
