/*
J  a  v  a  c  a  t  z

Description: common.js

Tel: (852)29877951
Fax: (852)29879294
Email: office@javacatz.com
Website: www.javacatz.com

Created: 2006-11-01
*/

var whatsNewBoxStatus = false;
var pageLoadStatus = false;
var withParameter = false;

/* *** General Slide Container *** */
slideContainerItems = new Array();
slideContainerTitles = new Array();
var openingSlideContainer = false;
function openSlideContainer(id) {
	if(openingSlideContainer == false) {
		openingSlideContainer = true;
		slideContainerTitles.each( function(node) {
			$(node).className = "slideTitle";
		});
		slideContainerItems.each( function(node) {
			if($(node).style.display != "none") {
				// $(node).style.display="none";
				new Effect.BlindUp($(node), {duration: 0.75});
			}
		});
		if($("desc_"+id).style.display == "none") {
			$("title_"+id).className = "slideTitle selected";
			new Effect.BlindDown($("desc_"+id), {duration: 0.75, afterFinish:function() {openingSlideContainer = false;}});
		} else {
			openingSlideContainer = false;
		}
	}
}
/* AJAX version */
var open_this_id = "";
function showSlideContainerResponse(originalRequest) {
	$('desc_'+open_this_id).innerHTML = originalRequest.responseText;
	new Effect.BlindDown($('desc_'+open_this_id), {duration: 0.75, afterFinish:function() {openingSlideContainer = false;}});
}

var getAnchor=false;// a flag used to control the get achor action for openSlideCOntainerAjax

function openSlideContainerAjax(folder, id) {
	
	var anchor = '';
	var target = '';
	if(getAnchor==false){
		var getLinkHash = window.location.hash;

		if (getLinkHash !=anchor){
			target = getLinkHash.substring(1);// change id = anchor
			
			if($('desc_'+target)){ //if the id exists
			
				getAnchor=true;
				id=target;
				openSlideContainerAjax(folder, id);// recursive call
				
			}else{
				
				getAnchor=true;
				openSlideContainerAjax(folder, id); //pass back the initial parameter into the call
			}
		}
	}
	
	
	if(openingSlideContainer == false) {
		getAnchor=false;
		openingSlideContainer = true;
		slideContainerTitles.each( function(node) {
			$(node).className = "slideTitle";
		});
		slideContainerItems.each( function(node) {
			if($(node).style.display != "none") {
				// $(node).style.display="none";
				new Effect.BlindUp($(node), {duration: 0.75});
			}
		});
		if($("desc_"+id).style.display == "none") {
			$("title_"+id).className = "slideTitle selected";
			var pars = "noredirect=true";
			var url = folder+"/"+id+".asp";
			open_this_id = id;
			new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showSlideContainerResponse});
		} else {
			openingSlideContainer = false;
		}
	}
}
/* Notices and Announcements Version */
slideNoticeItems = new Array();
slideNoticeTitles = new Array();
var openingSlideNotice = false;
function openSlideNotice(id) {
	slideNoticeItems = $$('.slideNotice');
	slideNoticeTitles = $$('.timeline_year');
	if(openingSlideNotice == false) {
		openingSlideNotice = true;
		slideNoticeTitles.each( function(node) {
			$(node).className = "timeline_year";
		});
		slideNoticeItems.each( function(node) {
			if($(node).style.display != "none") {
				// $(node).style.display="none";
				new Effect.BlindUp($(node), {duration: 0.75});
			}
		});
		if($(id).style.display == "none") {
			$("title_"+id).className = "timeline_year selected";
			new Effect.BlindDown($(id), {duration: 0.75, afterFinish:function() {openingSlideNotice = false;}});
		} else {
			openingSlideNotice = false;
		}
	}
}

/* *** end of Slide Container *** */



/* *** General On Top Box *** */
var onTopBoxStatus = false;
function showOnTopResponse(originalRequest) {
	$('onTopBoxPadding').innerHTML = originalRequest.responseText;
	new Effect.Appear($('onTopBox'), {duration: 0.5});
}

function toggleOnTopBox(url) {		// use AJAX if toggle on
	var pars = "noredirect=true";
	if(pageLoadStatus) {
		if(!onTopBoxStatus) {
			onTopBoxStatus = true;
			new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showOnTopResponse});
		} else {
			onTopBoxStatus = false;
			$('onTopBox').style.display = "none";
		}
	}
}
function closeOnTopBox(state) {
	if(state == "on") { 
		onTopBoxStatus = true; 
	} 
	if(!onTopBoxStatus && pageLoadStatus && $('onTopBox').style.display!='none') {
		new Effect.toggle($('onTopBox'), 'appear', {duration: 0.5});
		onTopBoxStatus = true;
	}
	if(state == "off") { 
		onTopBoxStatus = false; 
	} 
}
/* *** end of On Top Box *** */



/* *** What's New Box *** */
function showWhatsNewResponse(originalRequest) {
	$('whatsNewBox').innerHTML = originalRequest.responseText;
	$('whatsNewBoxLoading').style.display = "none";
	new Effect.toggle($('whatsNewBox'), 'appear', {duration: 0.5, afterFinish: function() { whatsNewBoxStatus = false; } });
}

function toggleWhatsNewBox() {		// use AJAX if toggle on
	var url = "whatsnew.asp";
	var pars = "noredirect=true";
	if(pageLoadStatus) {
		if(whatsNewBoxStatus) {
			$('whatsNewBoxLoading').style.display = "block";
			new Effect.Appear($('whatsNewBoxLoading'), {duration: 0});
			new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showWhatsNewResponse});
			$('whatsNewBoxLoading').style.display = "none";
		} else {
			new Effect.toggle($('whatsNewBox'), 'appear', {duration: 0.5, afterFinish: function() { whatsNewBoxStatus = true; } });
			$('whatsNewBoxLoading').style.display = "none";
		}
	}
}
function closeWhatsNewBox(state) {
	if(state == "on") { 
		whatsNewBoxStatus = true; 
	} 
	if(!whatsNewBoxStatus && pageLoadStatus && $('whatsNewBox').style.display!='none') {
		new Effect.toggle($('whatsNewBox'), 'appear', {duration: 0.5, afterFinish: function() { whatsNewBoxStatus = true; } });
		$('whatsNewBoxLoading').style.display = "none";
		// whatsNewBoxStatus = true;
	}
	if(state == "off" && $('whatsNewBox').style.display!='none') { 
		whatsNewBoxStatus = false; 
		$('whatsNewBoxLoading').style.display = "none";
	} 
}
/* *** end of What's New Box *** */

/* *** Time Line Box *** */
var timelineBoxStatus = false;
historyLinks = new Array();
timelineYear = new Array();
var openingLinks = false;
function showTimelineResponse(originalRequest) {
	$('timelineBoxPadding').innerHTML = originalRequest.responseText;
	// $('timelineLoading').style.display = "none";
	new Effect.Fade($('timelineLoading'), {duration: 0});
	// $('timelineBox').style.display = "block";
	new Effect.Appear($('timelineBox'), {duration: 0.5});
}

function toggleTimelineBox(url) {		// use AJAX if toggle on
	var pars = "noredirect=true";
	if(pageLoadStatus) {
		if(!timelineBoxStatus) {
			timelineBoxStatus = true;
			// $('timelineLoading').style.display = "block";
			new Effect.Appear($('timelineLoading'), {duration: 0});
			new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showTimelineResponse});
			// $('timelineLoading').style.display = "none";
			new Effect.Fade($('timelineLoading'), {duration: 0});
		} else {
			timelineBoxStatus = false;
			// new Effect.toggle($('timelineBox'), 'appear', {duration: 0.5});
			$('timelineBox').style.display = "none";
			$('timelineLoading').style.display = "none";
		}
	}
}
function closeTimelineBox(state) {

	if(state == "on") { 
		timelineBoxStatus = true; 
	} 
	if(!timelineBoxStatus && pageLoadStatus && $('timelineBox').style.display!='none') {
		new Effect.toggle($('timelineBox'), 'appear', {duration: 0.5});
		// $('timelineBox').style.display = "none";
		$('timelineLoading').style.display = "none";
		timelineBoxStatus = true;
	}
	if(state == "off") { 
		timelineBoxStatus = false; 
		$('timelineLoading').style.display = "none";
	} 
}
function openTimeline(id) {
	if(openingLinks == false) {
		openingLinks = true;
		timelineYear.each( function(node) {
			$(node).className = "timeline_year";
		});
		historyLinks.each( function(node) {
			if($(node).style.display != "none") {
				// $(node).style.display="none";
				new Effect.BlindUp($(node), {duration: 0.5});
			}
		});
		if($(id).style.display == "none") {
			$("title_"+id).className = "timeline_year selected";
			new Effect.BlindDown($(id), {duration: 0.5, afterFinish:function() {openingLinks = false;}});
		} else {
			openingLinks = false;
		}
	}
}
/* *** end of Time Line Box *** */



/* *** AJAX Glossary *** */
function showGlossaryResponse(originalRequest) {
	$('glossaryContents').innerHTML = originalRequest.responseText;
}
function ajaxGlossary(filename) {
	$('trahk_terms').className = "";
	$('general_terms').className = "";
	$(filename+"_terms").className = "glossary_header selected";
	$('glossaryContents').innerHTML = "<p></p>Please wait while loading...";
	var pars = "noredirect=true";
	var url = "glossary/"+filename+".asp";
	new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showGlossaryResponse});
}
/* *** end of AJAX Glossary *** */


/* *** AJAX FAQ *** */
function showFaqResponse(originalRequest) {
	$('faqContents').innerHTML = originalRequest.responseText;
	slideContainerItems = $$('.slideContents')
	slideContainerTitles = $$('.slideTitle');
	slideContainerItems.each( function(node) {
		new Effect.BlindUp($(node), {duration: 0, afterFinish:function() {
			// open up first question... add more for new faq section(s)
			if($('desc_gq1')) openSlideContainerAjax('faq/gq','gq1');
			else if($('desc_ci1')) openSlideContainerAjax('faq/ci','ci1');
			else if($('desc_mt1')) openSlideContainerAjax('faq/mt','mt1');
			else if($('desc_tr1')) openSlideContainerAjax('faq/tr','tr1');
			else if($('desc_lb1')) openSlideContainerAjax('faq/lb','lb1');
			else if($('desc_rd1')) openSlideContainerAjax('faq/rd','rd1');
		}});
	});
}
function ajaxFaq(filename) {
	$('general_questions').className = "";
	$('comparable_investments').className = "";
	$('management_trahk').className = "";
	$('trading').className = "";
	$('loyalty_bonus').className = "";
	$('redemption').className = "";
	$(filename).className = "faq_header selected";
	$('faqContents').innerHTML = "<p></p>Please wait while loading...";
	var pars = "noredirect=true";
	var url = "faq/"+filename+".asp";
	new Ajax.Request(url, {method:'post', parameters:pars, onComplete:showFaqResponse});
}
/* *** end of AJAX FAQ *** */


function checkform() {
	if( document.downloadform.firstname.value == "" ) {
		alert("Please enter your first name.");
		document.downloadform.firstname.focus();
		return false;
	}
	if( document.downloadform.lastname.value == "" ) {
		alert("Please enter your family name.");
		document.downloadform.lastname.focus();
		return false;
	}
	if( document.downloadform.address1.value == "" ) {
		alert("Please enter your address.");
		document.downloadform.address1.focus();
		return false;
	}
	if( document.downloadform.city.value == "" ) {
		alert("Please enter your city.");
		document.downloadform.city.focus();
		return false;
	}
	if( document.downloadform.country.value == "" ) {
		alert("Please enter your country.");
		document.downloadform.country.focus();
		return false;
	}
	/** Email **/
	if( document.downloadform.email.value == "" ) {
		alert("Please enter your email.");
		document.downloadform.email.focus();
		return false;
	}else{
		var email = document.downloadform.email.value;
		var atIndex = email.indexOf('@');
		var dotIndex = email.lastIndexOf('.');
		
		if(atIndex == -1 || dotIndex == -1){
				alert("Please enter your email.");
				return false;
		}
		
		if(atIndex > dotIndex){
				alert("Please enter your email.");
				return false;
		}
	
		
	}
	return true;
}



/* *** Smooth Anchor Scrolling *** */
var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
  
    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL =
setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 40;

ss.addEvent(window,"load",ss.fixAllLinks);
/* *** end of Smooth Anchor Scrolling *** */



/* *** getParameter *** */
function getParameter(name) {
   var url = window.location.href;
   var paramsStart = url.indexOf("?");

   if(paramsStart != -1){

      var paramString = url.substr(paramsStart + 1);
      var tokenStart = paramString.indexOf(name);

      if(tokenStart != -1){

         paramToEnd = paramString.substr(tokenStart + name.length + 1);
         var delimiterPos = paramToEnd.indexOf("&");

         if(delimiterPos == -1){
            return paramToEnd;
         }
         else {
            return paramToEnd.substr(0, delimiterPos);
         }
      }
   } else return "";
}
/* *** end of getParameter *** */

/* *** goPage *** */
function goPage() {
	var page = escape(getParameter("page"));
	var folder = escape(getParameter("folder"));
	if( page!="" && folder!="" ) {
		withParameter = true;
		openSlideContainerAjax(folder,page);
	}
}
/* *** end of goPage *** */

/* *** goGlossary *** */
function goGlossary() {
	var page = escape(getParameter("page"));
	var folder = escape(getParameter("folder"));
	if( page!="" && folder=="glossary" ) {
		withParameter = true;
		ajaxGlossary(page)
	}
}
/* *** end of goGlossary *** */

/* *** goFaq *** */
function goFaq() {
	var page = escape(getParameter("page"));
	if( page!="") {
		withParameter = true;
		ajaxFaq(page)
	}
}
/* *** end of goFaq *** */


function changeSideNavPic(id){
	var img_path = "img/sidenav/";
	var img_name = "sidenav_"+id+".jpg";
	var img = img_path + img_name ;
	
	var str ="<img src=\""+img+"\"/>";
	
	$('sideNavPic').innerHTML= str;
}

