
 
 YAHOO.namespace ("socialroster.calendar");
 YAHOO.namespace ("socialroster.container");
 

var TEAM_LOGOS_LOC = "/team/img_logos/";
 

SRoster = new function() {
	this.baseUrl = '/ci/';
	this.processingFee = 5.00;
	
	this.accessKey;
	
	this.setAccessKey = function(key) {
		this.accessKey = key;	
	}
	
	this.monthNames = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");

	this.pad = function(val, ch, num) {
        var re = new RegExp(".{" + num + "}$");
        var pad = "";

        do  {
            pad += ch;
        }while(pad.length < num)

        return re.exec(pad + val);
    }
	
	// Limit input length of field
	this.limitField = function(fieldId, length) {
		YAHOO.util.Event.addListener(fieldId, 'keyup', SRoster.limitFieldLength, length);
		YAHOO.util.Event.addListener(fieldId, 'keydown', SRoster.limitFieldLength, length);
	}
	
	this.limitFieldLength = function(e, length) {

		var el = this;
		
		if (el.value.length > length) {
			el.value = el.value.substring(0, length); 
		}	
	}
	
	
	this.alertResponseSuccess = function(o){
		SRoster.Display.alert('Success', o.responseText);
	}

	
	this.alertResponseFailure = function(o){
		SRoster.Display.alert('Action Failed', o.statusText + o.responseText);
	}
	
	
	
	
	this.alertCallback = {
	  success:this.alertResponseSuccess,
	  failure:this.alertResponseFailure
	}
	
	
	this.dummyMethod = function() {
		return true;
	}
	
	
	this.setCookie = function(cookieName,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = cookieName+"="+value+expires+"; path=/";
	}


	
	this.getCookie = function(cookieName) {
		var cookieEqual = cookieName + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(cookieEqual) == 0) return c.substring(cookieEqual.length,c.length);
		}
		return null;
	}
	
	
	this.eraseCookie = function(cookieName) {
		setCookie(cookieName,"",-1);
	}
	
	this.trim = function(str) { 
	
	     var str1 = ''; 
	     var i = 0; 
	     while ( i != str.length) 
	     { 
	         if(str.charAt(i) != ' ') str1 = str1 + str.charAt(i); i++; 
	     }
	     var retval = SRoster.isNumeric(str1); 
		 
         return str1; 
	}
	
	this.removeSpaces = function(str) { 

	    var str1 = ''; 
	    var i = 0; 
	    while(i != str.length) 
	    { 
	        if(str.charAt(i) != ' ') 
	            str1 = str1 + str.charAt(i); i ++; 
	    } 
	    return str1; 
	}
	
	this.isEmpty = function(value) {
		if (value == '') {
			return true;
		}	
		return false;
	}
	
	this.isValidTime = function(value) {
		value = SRoster.trim(value);
	   var hasMeridian = false;
	   var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
	   if (!re.test(value)) { return false; }
	   if (value.toLowerCase().indexOf("p") != -1) { hasMeridian = true; }
	   if (value.toLowerCase().indexOf("a") != -1) { hasMeridian = true; }
	   var values = value.split(":");
	   if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) { return false; }
	   if (hasMeridian) {
		  if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) { return false; }
	   }
	   if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) { return false; }
	   if (values.length > 2) {
		  if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) { return false; }
	   }
	   return true;
	}
	
	this.isNumeric = function(strString, allowDecimals) { 
	    var strValidChars = "0123456789"; 
		if (allowDecimals == true) {
			strValidChars += ".";
		}
	    var strChar; 
	    var blnResult = true; 

	    //test strString consists of valid characters listed above 
		
		// okay to be null value
	    if (strString.length == 0) 
	        return true; 
	    for (charIndex = 0; charIndex < strString.length && blnResult == true; charIndex++) 
	    { 
	        strChar = strString.charAt(charIndex); 
	        if (strValidChars.indexOf(strChar) == -1) 
	        { 
	            blnResult = false; 
	        } 
	     } 
		return blnResult; 
	}
	
	this.removeChildNodes = function(ctrl) {
	  while (ctrl.childNodes[0])
	  {
		ctrl.removeChild(ctrl.childNodes[0]);
	  }
	}

}



SRoster.Locale = new function() {
	this.getRegionsList = function(e, elements) {
		
			var countryEl = elements[0];
			var regionEl = elements[1];

			function handleSuccess(o){
				
				var regionList = YAHOO.util.Dom.get(regionEl);
				var regions = eval(o.responseText);
				
				regionList.options.length = 0;
				regionList.options[0] = new Option('Select a region...');
				for (var i=0; i<regions.length; i++) {
					regionList.options[i+1] = new Option(regions[i].name, regions[i].id);
				}
			}
			
			var callback = {
				success: handleSuccess,
				failure: SRoster.alertResponseFailure
			};
			
			var countryID = YAHOO.util.Dom.get(countryEl).value;
			
			var transaction = YAHOO.util.Connect.asyncRequest('GET', '/async/public.php?action=getRegions&country=' + countryID, callback, null);
			
		}
}

SRoster.DateTime = new function() {
	this.timeList = ['12:00am', '12:30am', '1:00am', '1:30am', '2:00am', '2:30am', '3:00am', '3:30am', '4:00am', 
					'4:30am', '5:00am', '5:30am', '6:00am', '6:30am', '7:00am', '7:30am', '8:00am', '8:30am', '9:00am',
					'9:30am', '10:00am', '10:30am', '11:00am', '11:30am', '12:00pm', '12:30pm', '1:00pm', '1:30pm', 
					'2:00pm', '2:30pm', '3:00pm', '3:30pm', '4:00pm', '4:30pm', '5:00pm', '5:30pm', '6:00pm', '6:30pm', 
					'7:00pm', '7:30pm', '8:00pm', '8:30pm', '9:00pm', '9:30pm', '10:00pm', '10:30pm', '11:00pm', '11:30pm'];
	
	this.bindTimeAutoComplete = function(elementID, elementContainerID) {
		
		 acTimeList = new YAHOO.widget.DS_JSArray(SRoster.DateTime.timeList);
			
	    // Instantiate second AutoComplete
	    oAutoComp = new YAHOO.widget.AutoComplete(elementID, elementContainerID, acTimeList);
	    oAutoComp.prehighlightClassName = "yui-ac-prehighlight";
	    oAutoComp.useShadow = true;
	    oAutoComp.forceSelection = false;
		oAutoComp.minQueryLength = 0;
		oAutoComp.maxResultsDisplayed = 12;
		oAutoComp.queryDelay = 0;
		// Removed - unnecessary to display the first AM hours off the bat
		
		
		// Check manually entered input
		oAutoComp.textboxBlurEvent.subscribe(function() {
			
			// Trim whitespace
			inputElement = YAHOO.util.Dom.get(elementID);
			var strval = inputElement.value;

	
			// Remove leading zero
			if(strval.indexOf('0') === 0) {
				strval = strval.substring(1, strval.length);
			}
			
			//Removing all space
			strval = SRoster.removeSpaces(strval); 
			
			//Checking AM/PM
			if(strval.charAt(strval.length - 1) != "M" && strval.charAt(strval.length - 1) != "m")
			{
				strval += 'pm';	
			}
			else if(strval.charAt(strval.length - 2) != 'A' && strval.charAt(
			  strval.length - 2) != 'a' && strval.charAt(
			  strval.length - 2) != 'p' && strval.charAt(strval.length - 2) != 'P')
			{
				strval += 'pm';
			}
			
			 
			// check/insert colon
			var pos1 = strval.indexOf(':');
			
			if(pos1 < 0 )
			{
				var timeOfDay = strval.indexOf('m') - 1;
				var ampm = strval.substring(strval.length-2, strval.length);
				strval = strval.substring(0, strval.length-2) + ':00';	// add colons
				strval = strval + ampm;
				pos1 = strval.indexOf(':');
			}
			
			if(pos1 > 2 || pos1 < 1)
			{
				strval = '';
			}
			
			// check that there are only 2 characters between colon and am/pm
			var hourMark = strval.indexOf('m') - 1;
			if ((hourMark - pos1) != 3) {
				strval = '';
			}
			//Checking hours
			
			var horval =  SRoster.trim(strval.substring(0,pos1));
			
			if(horval == -100)
			{
				strval = '';
			}
			  
			if(horval > 12)
			{
				strval = '';
			}
			else if(horval < 0)
			{
				strval = '';
			}
			//Completes checking hours.
			
			//Checking minutes.
			var minval =  SRoster.trim(strval.substring(pos1+1,pos1 + 3));
			
			if(minval == -100)
			{
				strval = '';
			}
			
			if(minval > 59)
			{
				strval = '';
			}   
			else if(minval < 0)
			{
				strval = '';
			}
			
			//minimum lenght is 4. example 3:30
			if(strval.length < 4)
			{
				strval = '';
			}
			
			//Maximum length is 7 after above modificatins have been made (removal of spaces).
			// example 10:45AM
			if(strval.length > 7)
			{
				strval = '';
			}
			
			//Checking minutes completed.  
			inputElement.value = strval;

		});
	}
}

	
SRoster.Display = new function() {
	this.defaultWidth = 350;
	
	
	this.confirm = function(container, title, text, handleYes, handleNo, width, dialogIcon) {
		if (!width) width = SRoster.Display.DefaultWidth;
		if (!dialogIcon) dialogIcon = YAHOO.widget.SimpleDialog.ICON_WARN
		
		// Instantiate simple dialog class
		YAHOO.socialroster.container.confirmDialog = new YAHOO.widget.SimpleDialog(container, 
		{
			width: width + 'px',
			fixedcenter: true,
			effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}, 
			visible: false,
			draggable: false,
			close: true,
			text: text,
			icon: dialogIcon,
			constraintoviewport: true,
		    buttons: [ { text:"Yes", handler:handleYes, isDefault:true },
					  { text:"No",  handler:handleNo } ]
		 });
		
		YAHOO.socialroster.container.confirmDialog.setHeader(title);
		YAHOO.socialroster.container.confirmDialog.render("dialog");
		YAHOO.socialroster.container.confirmDialog.show();
	}
	
	
	this.alert = function(title, text, dialogIcon) {
		text = text.replace(/<br \/>/ig, "\n");
		alert(text);return;
		
		function handleOK() {
			 this.hide();
		 }
	
		if (!dialogIcon) dialogIcon = YAHOO.widget.SimpleDialog.ICON_WARN
		text = "<span class='alertText'>" + text + "</span>";
		
		// Instantiate simple dialog class
		YAHOO.socialroster.container.confirmDialog = new YAHOO.widget.SimpleDialog('sysAlert', 
		{
			width: SRoster.Display.defaultWidth + 'px',
			fixedcenter: true,
			effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}, 
			visible: false,
			draggable: false,
			close: true,
			text: text,
			icon: dialogIcon,
			constraintoviewport: true,
		    buttons: [ { text:"OK", handler:handleOK, isDefault:true } ]
		 });
		
		YAHOO.socialroster.container.confirmDialog.setHeader(title);
		YAHOO.socialroster.container.confirmDialog.render("dialog");
		YAHOO.socialroster.container.confirmDialog.show();
	}
	
	
	this.slideVertical = function(elementID) {
		
		var attributes = { 
        	height: { to: 0 } 
    	}; 
		
		// Remove this element from the DOM
		var removeElement = function() { 
	   		var el = this.getEl(); 
	   		el.parentNode.removeChild(el); 
		} 
		
		// Hide element
		var deleteAnim = new YAHOO.util.Anim(elementID, attributes, 1, YAHOO.util.Easing.easeOut);
		deleteAnim.onComplete.subscribe(removeElement); 
  		deleteAnim.animate();
		
	}
	this.fadeIn = function(elID, subscribeFunc) {
		var attributes = { 
		   opacity: { to: 100 }
		};
		var fadeInAnim = new YAHOO.util.Anim(elID, attributes);
		if (subscribeFunc != undefined) {
			fadeInAnim.onComplete.subscribe(subscribeFunc); 
		}
		fadeInAnim.animate(); 
	}
	
	this.fadeOut = function(elID, subscribeFunc) {
		var attributes = { 
		   opacity: { to: 0 }
		}; 
		var fadeOutAnim = new YAHOO.util.Anim(elID, attributes);
		if (subscribeFunc != undefined) {
			fadeOutAnim.onComplete.subscribe(subscribeFunc); 
		}
		fadeOutAnim.animate(); 
	}
	
	
}



