/*
 Filename: knights.js
 Original author: B. Menoza
 Creation date: 7/11/2003
 Version: 4
 Modified by: bmenoza on 7/15/2003

Shah Khatri added the KofC_getLanguageLink function on 5/31/06.  Modified it again on 3/4/07 and
  3/8/07, as follows: Added a "goToCurrentSiteHomePage" (boolean) parameter.  If it is true, the new
  link refers to the current site's home page in the new language.  Otherwise, the new link refers to
  kofc.org.
Shah Khatri copied David Gianetti's language-cookie related functions on 6/12/06
Shah Khatri updated the image path in KofC_rotateHomePageImage() on 2/24/07
Shah Khatri removed call to KofC_ChooseCSS() from within this file on 2/25/07
Shah Khatri updated KofC_ChooseCSS() on 2/25/07
Shah Khatri updated KofC_getLanguageLink() on 4/15/08 to accomodate McGivney

 Copyright (c) 2003 Molecular, Inc.
 343 Arsenal St.
 Watertown, MA 02472
 (617) 218-6500
 http://molecular.com
*/

function positionEverything(totalItems, offsetDiff) {
	var divPosLeft = document.getElementById('myContainer').offsetLeft;
	var divPosTop = document.getElementById('myContainer').offsetLeft;
	var divPosWidth = document.getElementById('myContainer').offsetWidth
	
	var timelinePos = divPosLeft + divPosWidth - offsetDiff
	var refinePos = divPosLeft + 200


 for (i = 1; i < totalItems; i++) {
 var highlightItem
 highlightItem =("item" + i);
	document.getElementById(highlightItem).style.left = timelinePos;
}
	
}

	function setVisible(obj, bool){

// see if it's a document id, if it's not, then it must be an obj reference
	if(typeof obj == "string")
		obj = document.getElementById(obj);

// if user chose false, then lets see if the obj needs to be hidden,
//and if it does, then hide it.
	
	if(bool == false){
		if(obj.style.visibility != 'hidden');
			obj.style.visibility = 'hidden';
		}

// the user chose true, or omitted the second parameter.
//see if the object is visible. If it isn't, the show it.

	else { 
		if(obj.style.visibility != 'visible');
		obj.style.visibility = 'visible';
	}
}


function turnOff(thisItem, totalItems) {

 for (i = 1; i < totalItems; i++) {
 if (thisItem == ("item" + i)) {
 //alert("item" + i);
 setVisible(("item" + i),"false")
}
else {
//alert("item" + i)
 setVisible(("item" + i),"")
}
//setVisible(thisItem,"false");
 }

}

function positionLine() {

var URL = location.href;

if (URL.indexOf("timeline") != -1) {
    positionEverything(19, 545);
}
else if (URL.indexOf("process") != -1) {
    positionEverything(17, 533);
}

}

function FMG_OpenNewWindow(url,type,name,w,h) {
    var type;
    var new_win;
    var features;
    var toolbar;
    var location;
    var statusbar;
    var menubar;
    var resizable;
    var scrollbars;
    type = type.toLowerCase();
    if (type == 'html') {
        h=h-50;
        toolbar = 1;
        location = 1;
        statusbar = 1;
        menubar = 1;
        resizable = 'yes';
        scrollbars = 1;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/6 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }
    else if (type == 'media') {
        toolbar = 0;
        location = 0;
        statusbar = 1;
        menubar = 0;
        resizable = 'no';
        scrollbars = 1;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/2 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }
    else if (type == 'flash') {
        toolbar = 0;
        location = 0;
        statusbar = 1;
        menubar = 0;
        resizable = 'no';
        scrollbars = 0;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/2 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }
    else {
        toolbar = 0;
        location = 0;
        statusbar = 0;
        menubar = 0;
        resizable = 'no';
        scrollbars = 0;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/2 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }

    new_win = window.open(url,name,features) ;
    new_win.focus();
}


var initialFolder = "";
var currentWebSite = "other";
var KOFC_COOKIE = "CFCLIENT_KOFC";
var KOFC_COOKIE_PATH = "/";
var KOFC_COOKIE_DURATION_IN_DAYS = 365.25 * 2;
var LANGUAGE_KEY = "languagecode";

function KofC_ChooseCSS(pc_css,mac_css) {
    var stylesheet;
    if (navigator.userAgent.toLowerCase().indexOf('mac_ppc') != -1) { // AOL 5, MAC OS 9
      stylesheet = mac_css;
      if (! pc_css) { // No parameters passed
        stylesheet = "/eb/common/styles/knights-mac.css";
      }
      else if (pc_css == "930") { // The Anniversary site passes only one parameter: 930
        stylesheet = "/eb/common/styles/knights-mac_930.css";
      }
    }
    else {
      stylesheet = pc_css;
      if (! pc_css) { // No parameters passed
        stylesheet = "/eb/common/styles/knights.css";
      }
      else if (pc_css == "930") { // The Anniversary site passes only one parameter: 930
        stylesheet = "/eb/common/styles/knights_930.css";
      }
    }
    document.write('<link rel="STYLESHEET" type="text/css" href="' + stylesheet + '">');
} // End of function KofC_ChooseCSS(pc_css) {

function KofC_ChangeState(id,state) {
    if (document.getElementById) { // NETSCAPE 6, IE 5.X+
        document.getElementById(id).className = state;
    } else if (document.all) {
        document.all[id].className = state;
    }
}

function KofC_OpenNewWindow(url,type,name,w,h) {
    var type;
    var new_win;
    var features;
    var toolbar;
    var location;
    var statusbar;
    var menubar;
    var resizable;
    var scrollbars;
    type = type.toLowerCase();
    if (type == 'html') {
        h=h-50;
        toolbar = 1;
        location = 1;
        statusbar = 1;
        menubar = 1;
        resizable = 'yes';
        scrollbars = 1;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/6 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }
    else if (type == 'media') {
        toolbar = 0;
        location = 0;
        statusbar = 1;
        menubar = 0;
        resizable = 'no';
        scrollbars = 1;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/2 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }
    else if (type == 'warning') {
        toolbar = 0;
        location = 0;
        statusbar = 1;
        menubar = 0;
        resizable = 'no';
        scrollbars = 1;
        features =
        'height=400' + ',' +
        'width=600' + ',' +
        'top=' + (screen.height - h)/2 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }
    else if (type == 'flash') {
        toolbar = 0;
        location = 0;
        statusbar = 1;
        menubar = 0;
        resizable = 'no';
        scrollbars = 0;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/2 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }
    else {
        toolbar = 0;
        location = 1;
        statusbar = 0;
        menubar = 0;
        resizable = 'yes';
        scrollbars = 1;
        features =
        'height=' + h + ',' +
        'width=' + w + ',' +
        'top=' + (screen.height - h)/4 + ',' +
        'left=' + (screen.width - w)/2 + ',' +
        'toolbar=' + toolbar + ',' +
        'location=' + location + ',' +
        'statusbar=' + statusbar + ',' +
        'menubar=' + menubar + ',' +
        'resizable=' + resizable + ',' +
        'scrollbars=' + scrollbars;
    }

    new_win = window.open(url,name,features) ;
    new_win.focus();
}

function KofC_determineOS() {
    var isOSX;
    if (navigator.userAgent.toLowerCase().indexOf('os x') != -1) { // MAC OS X
        isOSX = true;
    }
    else {
        isOSX = false;
    }
}

function KofC_rotateHomePageImage() {
    var randomNumber = Math.floor(3*Math.random()+1);
    var randomImage = '/eb/common/images/level0_photo'+randomNumber+'.jpg';
    document.writeln('<img src="'+randomImage+'" alt="Three men" title="Three men" width="353" height="130" vspace="4" border="0">');
}

function KofC_rotateImage(imageDirectory,imageArray,alt,width,height) {
    var randomNumber = Math.floor(imageArray.length * Math.random());   
    var randomImage = imageDirectory + "/" + imageArray[randomNumber];    
    
    document.writeln('<img src="' + randomImage + '" alt="' + alt + '" title="' + alt +
                     '" width="' + width + '"height="' + height + '" vspace="4" border="0">');
} // End of function KofC_rotateImage(imageDirectory,imageArray,alt,width,height) {

function getQueryStringValue(paramName){
    // GET THE &param=value pairs to the right of the ?
    var qString = window.location.href.split("?")[1];
    var paramArray = qString.split("&")

    for(var i=0;i<paramArray.length;i++){
        var temp = paramArray[i].split("=")
        if(temp[0] == paramName) return (temp[1]);
    }

    return "";
}

function KofC_limitTextarea(limitField, limitNum, limitDisclaimer) {
    var oDisclaimer;
    if (document.getElementById) {
        oDisclaimer = document.getElementById(limitDisclaimer);
    }
    else if (document.all) {
        oDisclaimer = document.all[limitDisclaimer];
    }
    if (limitField.value.length > limitNum) {
        //limitField.value = limitField.value.substring(0, limitNum);
        oDisclaimer.style.display = "block";
    }
    else {
        oDisclaimer.style.display = "none";
    }
}

/* function CalculateTotal(frm, elementStartNo, elementEndNo, sectionName) {
    var order_total = 0

    for (var i=elementStartNo; i < elementEndNo; i++) {
        form_field = frm.elements[i]
        item_quantity = parseInt(form_field.value)

            if (item_quantity >= 0) {
                order_total += item_quantity
            }
        }

    //frm.sectionName.value = order_total
	eval("frm."+sectionName+".value = order_total");
} */

function KofC_getLanguageLink(currentLanguageCode, newLanguageCode, goToCurrentSiteHomePage,
                              coordinates, altText) {

  var newLanguageName = "English";
  if (newLanguageCode == "es") {
    newLanguageName = "Espa&ntilde;ol";
  }
  else if (newLanguageCode == "fr") {
    newLanguageName = "Fran&ccedil;ais";
  }
  else if (newLanguageCode == "pl") {
    newLanguageName = "Polski";
  }

// Example current page: /eb/pl/leadership_institute/surge_with_service/youth/index.html
  var regExp;
  var newHRef;
  if (goToCurrentSiteHomePage) {
// Example new page: /eb/en/leadership_institute/index.html
    regExp = new RegExp("/" + currentLanguageCode + "/([^/]+)/.*");
    newHRef = location.href.replace(regExp,"/" + newLanguageCode + "/$1/index.html");
  }
  else {
//newHRef = "http://www.kofc.org/"; 
// Example new page: /eb/en/leadership_institute/surge_with_service/youth/index.html
    regExp = new RegExp("/" + currentLanguageCode + "/","g");
    newHRef = location.href.replace(regExp,"/" + newLanguageCode + "/");
  }
  
  var commonString = '" href="' + newHRef + '" onClick="KofC_setCookie(\'' +
      KOFC_COOKIE + '\',\'' + LANGUAGE_KEY + '=' + newLanguageCode + '#\',\'' +
      KofC_cookieExpiration(KOFC_COOKIE_DURATION_IN_DAYS) + '\');">'
      
  if (currentWebSite == 'Museum') {
    document.write('<area shape="rect" coords="' + coordinates + '" alt="' + altText +
                   commonString);
  }
  else if (currentWebSite == 'McGivney' ||
           currentWebSite == 'Fatherhood') {
    document.write('<A ' + commonString + newLanguageName + '</A>');
  }  
  else {
    document.write('<A class="UtilityLink"' + commonString + newLanguageName + '</A>');
  }

} // End of function KofC_getLanguageLink(currentLanguageCode, newLanguageCode, ...) {

function KofC_getCookie(cookieName,key) {
    var docCookie = document.cookie;
    
    if (! docCookie.length) {
       return;
    }
    
    var offset = docCookie.indexOf(cookieName + "=");
    if (offset < 0) {
      return;
    }    
    offset += cookieName.length + 1;
    var end = docCookie.indexOf(";", offset);
    if (end < 0) {
      end = docCookie.length;
    }    

    var myCookieValue = unescape(docCookie.substring(offset, end));
    
    offset = myCookieValue.indexOf(key + "=");
    if (offset < 0) {
      return;
    }
    offset += key.length + 1;
    end = myCookieValue.indexOf("#", offset);
    if (end < 0) {
      end = myCookieValue.length;
    }
    
    return myCookieValue.substring(offset, end);
} // End of function KofC_getCookie(cookie_name,key) {

function KofC_forwardUser(fURI) {
	var result = KofC_getCookie(KOFC_COOKIE,LANGUAGE_KEY);
	
	if (result) {
		KofC_redirect(initialFolder + "/" + result + fURI);
	} else {
		KofC_redirect(initialFolder + "/en" + fURI);
	}	
} // End of function KofC_forwardUser() {

function KofC_redirect(fURI) {
  window.location = fURI;
} // End of function KofC_redirect() {

function KofC_cookieExpiration(numDays) {
    var now = new Date();
    var expires = new Date();
    expires.setTime(now.getTime() + numDays * 1000 * 60 * 60 * 24);
    return expires.toGMTString();
} // End of function KofC_cookieExpiration(numDays) {

function KofC_setCookie(name, value, expire) {
    var url = location.href;
    var domainSpec = "";
    var re = /https?:\/\/([^\.\/]+\.)*([^\.\/]+\.[^\.\/]+)\//i;
    var myArray = url.match(re);
    if (myArray) {
      domainSpec = "; domain=" + myArray[2];
    }

    document.cookie = name + "=" + escape(value) +
      ((expire == null) ? "" : ("; expires=" + expire)) +
        "; path=" + KOFC_COOKIE_PATH +
        domainSpec;
} // End of function KofC_setCookie(name, value, expire) {

function KofC_setLanguageCookieAndRedirect() {
// Example:
//http://stage.fathermcgivney.org/mcg/shared/setLanguage.html?lang=fr&furl=/favors/postFavorsAndIntentions.do%3fview%3dtrue%26favors%3dtrue
  var newLanguageCode = getQueryStringValue("lang");
  var newURI = getQueryStringValue("furl");
  if (newURI) {
    var re = /%3f/i;
    newURI = newURI.replace(re,"?");
    re = /%26/g;
    newURI = newURI.replace(re,"&");
    re = /%3d/g;
    newURI = newURI.replace(re,"=");
  }
  else {
    newURI = "/";
  }
  
  if (newLanguageCode) {
    KofC_setCookie(KOFC_COOKIE, LANGUAGE_KEY + '=' + newLanguageCode + '#',
      KofC_cookieExpiration(KOFC_COOKIE_DURATION_IN_DAYS));
  }

  KofC_redirect(newURI);
} // End of function KofC_setLanguageCookieAndRedirect() {

function KofC_setLanguageCookieIfNecessaryAndPossible() {
  var cookieLanguageCode = KofC_getCookie(KOFC_COOKIE,LANGUAGE_KEY);
  var URLLanguageCode = "";
  var siteJunction = "";
  if (currentWebSite == "McGivney") {
    siteJunction = "(/mcg)?";
  }

  var URL = location.href;
// Example: http://stage.fathermcgivney.org/en/guild/index.html
// Example: http://www.fathermcgivney.org/mcg/en/guild/news/index.html
  var regExp = new RegExp("^https?://[^/]+" + siteJunction + initialFolder + "/(en|es|fr)/.+","i");
  
  var myArray = URL.match(regExp);
  if (myArray) {
    if (siteJunction) {
      if (myArray[2]) {
        URLLanguageCode = myArray[2];
      }
    }
    else {
      if (myArray[1]) {
        URLLanguageCode = myArray[1];
      }
    }
  }
  
  if (URLLanguageCode && (! cookieLanguageCode || (cookieLanguageCode != URLLanguageCode))) {
    KofC_setCookie(KOFC_COOKIE, LANGUAGE_KEY + '=' + URLLanguageCode + '#',
      KofC_cookieExpiration(KOFC_COOKIE_DURATION_IN_DAYS));
  }
} // End of function KofC_setLanguageCookieIfNecessaryAndPossible() {

KofC_determineOS();
