// JavaScript Document

//--------------------------------------------------
//TODO: Move these functions to a different file... maybe a global js file, or a formatters.js;
//TODO: this functionality can be achieved w/ dojo.trim(string): Trims whitespace from both sides of the string;
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");//trim whitespace from left and right of string;
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");//trim whitespace from left of string;
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");//trim whitespace from right of string;
}
//--------------------------------------------------



validators = {

	isValidEmailSyntax: function(emailString){
		// Summary:
		//    Check if an email address has been constructed using valid email syntax.
		// Description:
		//    Uses a regular expression to validate the email syntax.
		// Note:
		//    The following regular expression was found at: http://www.regular-expressions.info/email.html
		//    More info on this regex can be found at this url, as well as alternate email validation regex's 
		//    and the trade-offs involved in using them.
		
		var validSyntaxPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		
		return validSyntaxPattern.test(emailString); //returns true or false;
	},

	formatUrl: function(Link){

		//-----------[ORIGINAL CODE]---------------------
		/*
		Link = dojo.trim(Link); //trim whitespace from left and right of string;
		var lower = Link.toLowerCase(); //save a temporary reference to a lower-case version of the link. we'll need this for comparing the string;
		
		//check the front of the string case-insensitively for 'https' and default to http if we don't find it;
		var protocol = (Link.search(/https/i) == 0) ? 'https' : 'http'; 
		
		if(lower.indexOf(protocol) == 0){//since our protocol is in lower-case, we need to search a lower-case version of the string;
			Link = Link.substr(protocol.length); //if the string already contains the protocol, remove it for now;
		}
		
		if(Link.indexOf("://") != 0){
			Link = '://'+Link;
		}
		Link = protocol + Link;
		
		return Link;
		*/
		//-----------[/ORIGINAL CODE]---------------------

		//-----------[NEW CODE]---------------------
		//Example input/output:
		//	in	-> out
		//	'aaa'	->	'http://aaa'
		//	'aaa.com'	->	'http://aaa.com'
		//	'www.aaa.com'	->	'http://www.aaa.com'
		//	'https://www.aaa.com'	->	'https://www.aaa.com'
		//	'://aaa.com'	->	'http://aaa.com'
		//	'ftp://aaa.com'	->	'http://aaa.com'
		
		Link = dojo.trim(Link); //trim whitespace from left and right of string;
		
		//check the front of the string case-insensitively for 'https' and default to http if we don't find it;
		var protocol = (Link.search(/https/i) == 0) ? 'https' : 'http'; 
		
		//if url contains "://" strip it, and everything before it, off the string, so we can rebuild a safe string.
		//e.g. 'ftp://somesite.com' or '://somesite.com' becomes 'somesite.com'
		if(Link.indexOf("://") >= 0){
			Link = Link.substr(Link.indexOf("://") + 3); //3 is the length of "://"
		}
		Link = protocol + '://' + Link;

		return Link;
		//-----------[/NEW CODE]---------------------
	}


};



errorHandler = {

	showErrors: function(errorDivId, errors){
		var errorHtml = 'The following errors were found with the data you entered:<ul>';
		dojo.forEach(errors, function(errorString){
			errorHtml += '<li>' + errorString + '</li>';
		});
		errorHtml += '</ul>';
		dojo.byId(errorDivId).innerHTML = errorHtml;
		
		dojo.style(errorDivId, {
			'display': 'block',
			'visibility': 'hidden'
		});
        dijit.scrollIntoView(errorDivId); //scroll the error div into view if it isn't already;
		setTimeout("dojo.style('"+errorDivId+"', 'visibility', 'visible');", 200);
	}

};


