/*
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-20
*/

var memberLoginBoxStatus = false;
var pageLoadStatus = false;
var sideNavSub = new Array();
var subSectionDetails = new Array();
var openingSlide = false;
var subSectionTimer = 0;
var fontsizePx = 11;	// same as font-size in body, can be customized in customization.js
var small_fontsize = 11;	// can be customized in customization.js
var big_fontsize = 14;	// can be customized in customization.js
/* An array to store the toggle status of each main section element */
var mainsection_togglestatus = new Array() ;

var pageTitle;
var mainsection_names;

var organizationtypeSub;
var organizationtypeSub2;

/* *** Adjust Font Size *** */
function adjustFontsize(sizechange) {
	// sizechange can be:
	// - null or "" -- no change (reset to current fontsizePx)
	// - "smaller" / "larger" / "small" / "large" -- set to relative / absolute fontsize preset
	// - integers -- set fontsizePx to sizechange.
	if( sizechange != null && sizechange != "") {
		/* smaller and larger is disabled as requested by client
		if( sizechange == 'smaller' && fontsizePx > small_fontsize ) fontsizePx = fontsizePx - 1;
		else if( sizechange == 'larger' && fontsizePx < big_fontsize ) fontsizePx = fontsizePx + 1;
		*/
		if( sizechange == 'smaller' && fontsizePx > small_fontsize ) fontsizePx = small_fontsize;
		else if( sizechange == 'larger' && fontsizePx < big_fontsize ) fontsizePx = big_fontsize;
		else if( sizechange == 'small' ) fontsizePx = small_fontsize;
		else if( sizechange == 'large') fontsizePx = big_fontsize;
		else if( !(isNaN(sizechange)) == sizechange ) fontsizePx = sizechange;
	}
	document.body.style.fontSize = fontsizePx + "px";
}
/* *** end of Adjust Font Size *** */

/* *** General / Left Side Navigation Slider *** */
function toggleSlide(index,id) {
	if(pageLoadStatus && !openingSlide) {
		if($('sub_'+id)) {
			openingSlide = true;
			new Effect.toggle($('sub_'+id), 'blind', { duration: 0.5, afterFinish: function() { openingSlide = false; } });
			$$('#main_'+id+' a').each( function(node) { if($(node).className != 'open') $(node).className = 'open'; else $(node).className = ''; } );
		}
	}
}
/* *** end of General Left Side Navigation Slider *** */

/* *** Left Side Navigation Subsection Details *** */
function subDetails(pid,action) {
	var indexFirstUnderScore = pid.indexOf('_');
	var indexLastUnderScore  = pid.lastIndexOf('_');
	var parentID;
	if(pid == '') {
		return;	
	}
	if(pid.indexOf('_0') != -1 ) { // special check 
		parentID =pid.substring(indexFirstUnderScore+1, (indexLastUnderScore-indexFirstUnderScore+3));
	} else {
		parentID = pid;	
	}
	if(action=='show') {
		// mainSelectedIDs = $$('.mainSectionSelected');
		// mainSelectedIDs.each( function(node) { if ($(node).id !=  pid) $(node).className='mainSection' ; } );
		if(pid && $(pid) && !openingSlide) {	
			subSectionDetails.each( function(node) {$(node).style.display = 'none'; } );	// follow
			$(pid).style.display = 'block';	// follow
			$(pid).style.visibility = 'visible';
			// new Effect.Appear($(id), {duration: 0.5});	// above
			clearTimeout(subSectionTimer);
			subSectionTimer = setTimeout("if($('"+pid+"')) $('"+pid+"').style.display = 'none';", 50000);	// follow
		} else {
			if($('main_'+parentID).className != null && $('main_'+parentID).className != "") {
				$('main_'+parentID).className = 'mainSectionSelected ' + $('main_'+parentID).className;
			} else {
				$('main_'+parentID).className = 'mainSectionSelected';
			}
		}
	} else if(action=='hide') {
		if(pid && $(pid)) {
			subSectionDetails.each( function(node) {$(node).style.display = 'none'; } );	// follow
			$(pid).style.display = 'none';	// follow
			$(pid).style.visibility = 'hidden';
			clearTimeout(subSectionTimer);
			subSectionTimer = setTimeout("if($('"+pid+"')) $('"+pid+"').style.display = 'none';", 1000);    // follow
		} else {
			$('main_'+parentID).className = $('main_'+parentID).className.replace('mainSectionSelected', '');
			if($('main_'+parentID).className == " ") {
				$('main_'+parentID).className = "";
			}
		}
	}
}
/* *** end of Left Side Navigation Subsection Details *** */

/* *** showOrganizationType *** */
function showOrganizationType(id) {
	organizationtypeSub2.each( function(node) { new Effect.Fade($(node), {duration:0}); } );
	organizationtypeSub.each( function(node) { new Effect.Fade($(node), {duration:0}); } );
	setTimeout( 'if($("organizationtypeSub_'+id+'")) new Effect.Appear($("organizationtypeSub_'+id+'"), {duration:0.75});', 100 );
}
function showOrganizationTypeSub(id) {
	organizationtypeSub2.each( function(node) { new Effect.Fade($(node), {duration:0}); } );
	setTimeout( 'if($("organizationtypeSub_'+id+'")) new Effect.Appear($("organizationtypeSub_'+id+'"), {duration:0.75});', 100 );
}
/* *** end of showOrganizationType *** */

/* *** turnPage *** */
function turnPage(current_id, new_id) {
	new Effect.BlindUp($(current_id), {duration: 1});
	setTimeout('new Effect.SlideDown($("'+new_id+'"), {duration: 1});', 1000);
}
/* *** end of turnPage *** */

/* *** numberList *** */
function numberList(id, target) {
	var number = escape($F(id));
	if( number < 1 ) number = 1;
	var index;
	for( index=1; index <= number; index++ ) {
		$(escape(target)+escape(index)).style.display = "block";
	}
	while($(escape(target)+escape(index)) && $(escape(target)+escape(index)).style.display == "block") {
		$(escape(target)+escape(index)).style.display = "none";
		index++;
	}
}
/* *** end of numberList *** */

/* *** changeview *** */
function changeview(hide_id, show_id) {
	if($(hide_id) && $(show_id)) {
		if($(hide_id).style.display=="block" || $(show_id).style.display=="none") {
			$(hide_id).style.display = "none";
			$(show_id).style.display = "block";
			// BAD sliding effect on Safari and MSIE5.5 // new Effect.BlindUp($(hide_id), {duration: 1, afterFinish: function() {new Effect.BlindDown($(show_id), {duration: 1});}});
		}
	}
}
/* *** end of changeview *** */

/* *** change_membership_coorganizer_code_text *** */
function change_membership_coorganizer_code_text() {
	var bec_member = escape($F('bec_member'));
	if( bec_member == "No" ) {
		$('membership_coorganizer_area').style.display = "none";
	} else {
		$('membership_coorganizer_area').style.display = "block";
		if( bec_member == "Yes - enter Membership Code" ) $('membership_coorganizer_code_text').innerHTML = "Membership Number <span class=\"redout\">*</span> : ";
		else if( bec_member == "Co-Organizer" ) $('membership_coorganizer_code_text').innerHTML = "Co-Organizer Number <span class=\"redout\">*</span> : ";
		else $('membership_coorganizer_code_text').innerHTML = "Membership Number <span class=\"redout\">*</span> : ";
	}
}
/* *** end of change_membership_coorganizer_code_text *** */

/* *** change_membership_coorganizer_code_text *** */
function showotherbox(id) {
	//alert(id);
	var select_id = id +"select";
	var id_selectedValue = escape($F(select_id));
	
	var other_id = id+"_otherbox"
	if( id_selectedValue == "Other" ){
				
			$(other_id).style.display = "block";
	}else{
			$(other_id).style.display = "none";
	}
}
/* *** end of change_membership_coorganizer_code_text *** */

/* *** 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 *** */



function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function upper(field_name){
	
	var d = $(field_name);
	d.value=d.value.toUpperCase();
	
}

function changeState(index,id,action){
	if(action=='in'){	
		if(mainsection_togglestatus[index]=='open'){
			mainsection_togglestatus[index]='open_select';
		}else if(mainsection_togglestatus[index]=='closed'){
			mainsection_togglestatus[index]='closed_select';
		}
	} else if(action=='click'){	
		if(mainsection_togglestatus[index]=='open_select'){
			mainsection_togglestatus[index]='closed_select';
		}else if(mainsection_togglestatus[index]=='closed_select'){
			mainsection_togglestatus[index]='open_select';
		}
	} else if(action=='out'){	
		if(mainsection_togglestatus[index]=='open_select'){
			mainsection_togglestatus[index]='open';
		}else if(mainsection_togglestatus[index]=='closed_select'){
			mainsection_togglestatus[index]='closed';
		}
	}
	changeImage(index, id);
}

function changeImage(index, id){
	if($('main_'+id+"_label")){
		if(mainsection_togglestatus[index]==null)
			mainsection_togglestatus[index]='open';
		if(mainsection_togglestatus[index]=='open'){
			$('main_'+id+'_label').className="label_open";
		}else if(mainsection_togglestatus[index]=='open_select'){
			$('main_'+id+'_label').className="label_open_select";
		}else if(mainsection_togglestatus[index]=='closed'){
			$('main_'+id+'_label').className="label_close";
		}else if(mainsection_togglestatus[index]=='closed_select'){
			$('main_'+id+'_label').className="label_close_select";
		}
	}
}


/* *** form_event_signup.aspx total payment calculation *** */
function calculate_event_signup_payment() {
	/* pending trevor's backend soapclient webservice ***
	var lbl_price = $('lbl_price').innerHTML;
	var lbl_discount_member = $('lbl_discount_member').innerHTML;
	var lbl_discount_group = $('lbl_discount_group').innerHTML;
	var lbl_discount_earlybird = $('lbl_discount_earlybird').innerHTML;
	var bool_bec_member = false;
	if($F('bec_member')=="BEC Member") bool_bec_member = true;
	var n_attendees = $F('n_attendees');
	
	var total_payment_base = lbl_price * n_attendees;	// base price
	var total_payment = total_payment_base;
	if(bool_bec_member) total_payment -= total_payment_base * lbl_discount_member / 100;	// BEC Member Discount
	
	$('total_payment');
	*/
}
