﻿function NotDoneYet() {
	alert('At ease, friend. This isn\'t done yet.');
}

function swapImage(img,path) {
	img.src = path;
}

//Navigates to a URL
function goTo(strURL) {
	window.location.href = strURL;
}

//Close the delete confirmation modal.
function CloseDeleteModal(id) {
    HideShowModal(id);
    goTo(document.location.href);
}

function refreshMe() {
	var url = document.location.href;
	goTo(url);
}

function Trim(str) {  
	while(str.charAt(0) == (" ") ) { str = str.substring(1); }
	while(str.charAt(str.length-1) == " " ) { str = str.substring(0,str.length-1); }
	return str;
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++){ 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1){
			IsNumber = false;
      }
   }
   return IsNumber;
}

//onError, we want to scroll to the element that has the error.
function ScrollToElement(elem){

  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(elem != null){
    selectedPosX += elem.offsetLeft;
    selectedPosY += elem.offsetTop;
    elem = elem.offsetParent;
  }

 window.scrollTo(selectedPosX,selectedPosY);

}

//Login.aspx: allows user to submit form via enter key press.
//Allow the enter key to point to the correct button to click.
function enterKeySubmit(buttonName, e) {

	var key;
	if(window.event) {
		key = window.event.keyCode;   //IE
	}
	else {
		key = e.which;						//FF
	}
	if (key == 13) { //Enter key
		//Get the button
		var btn = document.getElementById(buttonName);
		if (btn != null) {
			//If we find the button click it
			btn.click();
			//event.keyCode = 0;
		}
	}
}

//If a page is loading in an iframe, break out of it and load the page
//using the full browser.
function clearIFrame() {
	if (top.location!= self.location) { 
		top.location = self.location.href;
	} 
}

function checkModalVisibility() {
	var modal = document.getElementById('welcomeModal');
	if (modal.style.display == 'block') {
		return true;
	}
	else {
		return false;
	}
}

//Gets a subkey cookie value, given the cookie key and subkey.
function readSubkeyCookie(key, subkey) {
	
	key = Trim(key).toLowerCase();
	subkey = Trim(subkey).toLowerCase();

	var keys = document.cookie.split(';');
	for (var n in keys) {
		var i = keys[n].indexOf('=');
		var ckey = Trim(keys[n].substr(0, i)).toLowerCase();
		var cval = Trim(keys[n].substr(i + 1, keys[n].length - (i + 1))).toLowerCase();
		//see if our key matches the arg
		if (ckey == key) {
			//see if we have a subkey
			if (cval.indexOf('=') > -1) {
				var sk = cval.split('=');
				if (sk[0] == subkey) {
					//return the subkey value
					return sk[1];
				}
			}
		}
	}
}

//Shows or hides an item by id.
function ShowHide(id) {
	if (document.getElementById) {
		if (document.getElementById(id)) {
			if (document.getElementById(id).style.display != 'block') {
				document.getElementById(id).style.display = 'block';
			}
			else {
				document.getElementById(id).style.display = 'none';
			}
		}
	}
}

//Get X-Y coords of an element.
//Pass arg 'event' to this function.
function getCoords(e) {
	var coords = [];
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	//alert('posx is: ' + posx + ', and posy is: ' + posy);
	coords.push(parseInt(posx), parseInt(posy));
	return coords;
}

function disableSelects(disable) {
	var form = document.getElementById("aspnetForm");
	var i = 0;
	for (i=0; i < form.length; i++) {
		if ((form.elements[i].type == "select-one") && (form.elements[i].style.display != 'none') 
			&& !(form.elements[i].className.endsWith('popupDdl')) && !(form.elements[i].id == 'lstSecondaryMessages')) 
		{
		    // Excluding any selects inside the modal div itself (because we need them)...
		   	// hide the select elements.
			form.elements[i].style.display = 'none';
		}
		else if ((form.elements[i].type == "select-one") 
			&& (form.elements[i].style.display == 'none')) {
			    //alert("in elseif of disable fxn "+form.elements[i].id);
				form.elements[i].style.display = 'inline';
		}
	}
	return false;   
}

function disableIframes(disable) {
//This code hides the one iframe that is in the site, in the wishes tab of the homepage.
//More generic 'hide ALL iframes' code wasn't working so for now we are explicitly hiding this particular 
//iframe by id, and since it is the only iframe in the site currently this is not a big deal.
	var HideStatus = "inline";
      if ( disable )
      {
            HideStatus = "none";
      }
       var fr = document.getElementById ("wishframe");
       if ( fr == null )
       {
             //alert("No Frame Found");
             return;
       }
       else 
       {
           fr.style.display= HideStatus;
       }
	
	return false;   
}



String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function urlContains(localpath) {
	var pathname = window.document.location.pathname.toLowerCase();
	if (pathname == localpath) { 
		return true;
	}
	else {
		return false;
	}
}

function isMyMessagesPage() {
	var bMyMsgsPg = false;
	var pathname = window.document.location.pathname.toLowerCase();
	if (pathname == '/mycorner/users/mymessages.aspx') { 
		bMyMsgsPg = true; 
	}
	return bMyMsgsPg;	
}

function isHomePage() {
	var bIsHomePage = false;
	var pathname = window.document.location.pathname.toLowerCase();
	//var url = window.location.href.toLowerCase();
	if (pathname == '/mycorner/index.aspx' || pathname == '/mycorner/') { 
		bIsHomePage = true; 
	}
	return bIsHomePage;
}

function hideSwfVideosForModal(bHide) {
	if (bHide) { ShowHomePageFlash(false); }
	else { ShowHomePageFlash(true); }
}

//Shows/hides the home page flash objects.
//Shows/hides garden stats flash object on mymessages page.
function ShowHomePageFlash(bShow) {
	var divLL, divY, divP
	var bHP = isHomePage();
	var bMM = isMyMessagesPage();
	if (bHP || bMM) {
		divLL = document.getElementById('divLeadingLadiesContent');
		divY = document.getElementById('divYogaContent');
		divP = document.getElementById('divPuzzleGameContent');
		divQ = document.getElementById('flash-content');
		divS = document.getElementById('blank-content');
		if (divLL) { divLL.style.display = (bShow ? 'block': 'none'); }
		if (divY) { divY.style.display = (bShow ? 'block': 'none'); }
		if (divP) { divP.style.display = (bShow ? 'block': 'none'); }
		if (divQ) { divQ.style.display = (bShow ? 'block': 'none'); }
		if (divS) { divS.style.display = (bShow ? 'none': 'block'); }
	}
}



//Important: the query string and also the client click events fired on
//tabs 1 and 4 can update these values.
var IS_INIT_LEADING_LADIES = false;
var IS_INIT_YOGA = false;

//Home page swf - js externalInterface invoker method
function resetHomePageSwf(movieName) {
	var obj = getRefToFlashMovie(movieName);
	var isFPUnd = (typeof(obj.jsResetMovie) == 'undefined');
	var isFPFunc = (typeof(obj.jsResetMovie) == 'function');
	var isFPValid = (isFPFunc && !isFPUnd);
	var isSwfInit = isHomePageSwfInitialized(movieName);
	if (isSwfInit) {
		//alert('resetting movie: ' + movieName);
		if (isFPValid) {
			try {
				obj.jsResetMovie();
			}
			catch(e) { } //use catch for FF2 and FF3.
		}
	}
}

//Determine if both swfs are initialized. If they are NOT initialized, don't
//call the external interface methods to stop them.
function isHomePageSwfInitialized(movieName) {
	var mn = movieName.toLowerCase();
	if (mn == 'iyc100_player') { return IS_INIT_LEADING_LADIES; }
	if (mn == 'iyc100_yoga_player') { return IS_INIT_YOGA; }
	return false;
}

function getTabParams() {
	//alert('IS_INIT_LEADING_LADIES: ' + IS_INIT_LEADING_LADIES + '\n'
	//	+ 'IS_INIT_YOGA: ' + IS_INIT_YOGA);
}	

//Checks for param 'tab' in query string.
function checkHomePageTabQueryStringParams() {
	var qs = window.location.search.substring(1);
	if (qs.length <= 0) { 
		IS_INIT_LEADING_LADIES = true;
		return;
	}
	if (qs.length > 0) {
		var pairs = qs.split("&");
		for(var i = 0; i < pairs.length; i++) { 
			var pair = pairs[i];
			if (pair.indexOf('=') > -1) {
				var qkey = pair.split('=')[0];
				if (qkey == 'tab') {
					var qval = pair.split('=')[1];
					if (qval == '4') {
						IS_INIT_YOGA = true;
						return;
					}
					else if (qval == '1') {
						IS_INIT_LEADING_LADIES = true;
						return;
					}
				}
			}
		}
	}
}

function getRefToFlashMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
	  return window[movieName];
	}
	else {
	  return document[movieName];
	}
}


//Welcome modal controller function.
function HandleWelcomeModal(id, bShow) {
	if (bShow) {
		SWM(id);
		HandleModalFlash();
	}
	else {
		HWM(id);
		HandleModalFlash();
	}
}

function HandleModalFlash() {
   //See if the welcome modal is showing. If so, hide the leading ladies Flash content.
   var bModalShowing = checkModalVisibility();
   if (bModalShowing) { 
		ShowHomePageFlash(false); 
	}
   else { 
		ShowHomePageFlash(true); 
	}
}

//Hide welcome modal.
function HWM(id) {
	if (document.getElementById(id)) {
		if (document.getElementById(id).style.display == 'block') {
			//alert('hiding...');
			document.getElementById('modalBkgd').style.display = 'none';
			hideSwfVideosForModal(false);
			document.getElementById(id).style.display = 'none'; 
			disableSelects(false);
		}
	} 
}

//Show welcome modal.
function SWM(id) {
	if (document.getElementById(id)) {
		if (document.getElementById(id).style.display != 'block') {
			//alert('showing...');
			disableSelects(true);
			hideSwfVideosForModal(true);
			document.getElementById('modalBkgd').style.display = 'block';
			document.getElementById(id).style.display = 'block'; 
		}
	}
}


//Contains some extra checks to hide/show flash when the modal is hidden/shown.
function HideShowWelcomeModal(id) {

	//Hide/show the welcome modal.
	HideShowModal(id);
	
   //See if the welcome modal is showing. If so, hide the leading ladies Flash content.
   var bModalShowing = checkModalVisibility();
   if (bModalShowing) { ShowHomePageFlash(false); }
   else { ShowHomePageFlash(true); }
   
}

function HideWelcomeModal() {
	var id = 'welcomeModal';
	var bModalShowing = checkModalVisibility();
	if (bModalShowing) {
		document.getElementById(id).style.display = 'none';
	}
}

// Hides or Shows a modal item by id. 
function HideShowModal(id) {

    if (document.getElementById(id).style.display != "block") {
        //alert("in if "+id);
        disableSelects(true);
        hideSwfVideosForModal(true);
        document.getElementById("modalBkgd").style.display = "block";
        disableIframes(true);
        ShowHide(id);
    }
    else {
        //alert(id+" in else");
        document.getElementById("modalBkgd").style.display = "none";
        hideSwfVideosForModal(false);
        ShowHide(id);
        disableSelects(false);
        disableIframes(false);
        
    }
}

function HideShowModalBackground(id) {
    //alert(id);
    if (document.getElementById(id).style.display != "block") {
        disableSelects(true);
        hideSwfVideosForModal(true);
        document.getElementById("modalBkgd").style.display = "block";
        //ShowHide(id);
    }
    else {
        //alert(id+" in else");
        document.getElementById("modalBkgd").style.display = "none";
        hideSwfVideosForModal(false);
        //ShowHide(id);
        disableSelects(false);
        
    }
}

// Hides a server-side modal item by id. 
function HideServerModal(id) {
    //alert(id);
    document.getElementById("modalBkgd").style.display = "none";
    document.getElementById(id).style.display="none";
    
    //if we're on the myprofile page, maintain the sign.
    //this code is located in page-profile.js.
    maintainSignImage();
}

// Hides a server-side modal item by id. 
function HideContainerDiv(id) {
    //alert(id);
    //document.getElementById("modalBkgd").style.display = "none";
    document.getElementById(id).style.display="none";
    
    //if we're on the myprofile page, maintain the sign.
    //this code is located in page-profile.js.
    //maintainSignImage();
}


////////////////////////////////////////////////////////
/* Common myFriends and myMessages grid functions */
////////////////////////////////////////////////////////

//Global css style state vars
var state01 = 'friendlist';
var state02 = 'grid_friends_alt_row';
var state03 = 'hover';
var state04 = 'open';
var state05 = 'dedicated';

//Gets whether the row is EVEN or ODD - this determines css style.
function getGridRowType(elem) {
	var H;
	var arrH = elem.getElementsByTagName('input');
	for (var i = 0; i < arrH.length; i++) {
		if (arrH[i].className == 'hidRowType') {
			H = arrH[i];
		}
	}
	return H.value;
}

//Switches the row css style on mouseover and mouseout.
function swapGridRowStyle(elem, bHighlight) {
	var clsname = elem.className;
	var strRowType = getGridRowType(elem);	//get row type
	
	if (clsname == state04) return;	//don't apply events to 'open' state.
	if (bHighlight) elem.className = state03; //if hover...apply hover
	else {	//switch back off. get the rowtype first.
		switch (strRowType) {
			case 'ODD': elem.className = state01;
				break;
			case 'EVEN': elem.className = state02;
				break;
			case 'DEDICATED': elem.className = state05;
				break;
		}
	}
}

//Applies 'on' or 'off' css style onclick.
function selectGridRow(elem) {

	var clsname = elem.className;
	var strRowType = getGridRowType(elem);	//get row type
	
	switch (clsname) {
		case state01: //we are 'off' after click. change to 'on'.
			elem.className = state04;
			break;
		case state02: //we are 'off' after click. change to 'on'.
			elem.className = state04;
			break;
		case state03: //we are 'hover'. change to 'on'.
			elem.className = state04;
			break;
		case state04: //we are 'on'. change to 'off': get row type first.
			if (strRowType == 'ODD') {
				elem.className = state01;
			}
			if (strRowType == 'EVEN') {
				elem.className = state02;
			}
			if (strRowType == 'DEDICATED') {
				elem.className = state05;
			}
			break;
	}
}


//Shows/hides a row in the friends grid onclick.
function expandGridRow(elem) {
	
	var strRevealDiv = 'RevealPanel';
	var parent = elem.parentNode.parentNode.parentNode;
	
	//Switch css style to 'on' state.
	selectGridRow(parent);
	
	if (parent.childNodes.length > 0) {
		for (var i = 0; i < parent.childNodes.length; i++) {
			if (parent.childNodes[i].className == strRevealDiv) {
				if (parent.childNodes[i].style.display == 'block') {
					parent.childNodes[i].style.display = 'none';
					return;
				}
				else {
					parent.childNodes[i].style.display = 'block';
					return;
				}
			}
		}
	}
}

//Hides / shows the "remove" link for the friends page.
function HideShowRemoveLink(rowID) {
	var removeDiv = document.getElementById('removeButtonDiv'+rowID);
	if (removeDiv) {
		if (removeDiv.style.display == 'block') {
			removeDiv.style.display = 'none';            
		}
		else {
			removeDiv.style.display = 'block';
		}
	}
}

////////////////////////////////////////////////////////
/* END Common myFriends and myMessages grid functions */
////////////////////////////////////////////////////////



/* List Helper functions */
function removeAllOptions (selectbox) {
    var count;
    for (count = 0 ; count < selectbox.options.length ; count++) {
		selectbox.remove (count);
    }
}

function addOption(select, text, value) {
	var o = document.createElement('option');
	o.text = text;
	o.value = value;
	if (select) {
		select.options.add(o);
	}
}

function removeOption(select, index) {
	select.remove(index);
}

function removeAllOptions(select) { 
	select.options.length = 0;
}

function getSelectedListValue(select) {
	return select.value;
}

function getSelectedListText(select) {
	return select.options[select.selectedIndex].text;
}

//Breast Cancer Resources - open button
function showSupport(){
    $("#support").toggle(); 
    $("a.clickme").toggleClass("on");
}
function showAgencies(){
    $("#agencies").toggle(); 
    $("a.clickmed").toggleClass("on2");
}
function showPrinted(){
    $("#printed").toggle(); 
    $("a.clickme3").toggleClass("on3");
}
function showContent(el){
    var thisEl=document.getElementById(el);
    if(thisEl.style.display=='none')thisEl.style.display='block';
    else thisEl.style.display='none';
}

function showFaq(el){
    var thisState;
    if(el.className=='hoverable'){
        el.className='';
        thisState='off';
    }
    else{
        el.className='hoverable';
        thisState='on';   
    }
    
    var dedicationFaq=document.getElementById('dedicationfaq');
    var def=dedicationFaq.getElementsByTagName('dt');
    var thisIndex=0;
    
    for (var i = 0; i < def.length; i++){
      if(def[i]==el){
        thisIndex=i;
        
      }
      
    }
    var target=dedicationFaq.getElementsByTagName('dd');
    target[thisIndex].className=thisState;
}


function PromptToSaveGarden() {
    msg = 'Be sure to save your garden before closing! Are you sure you want to close your Garden?'
    if (confirm(msg)) {
	    return true;
	}
	else {
	    return false;
	}
}


function ConfirmGardenClose(){
    return confirm("Are you sure you want to exit without saving?");
}