//-----------------------------------------------------------------------------
// global javascript
//-----------------------------------------------------------------------------
// Use of this code is governed in accordance with the terms and conditions of
// the agreement executed between the Client and Tag New Media. It is intended
// for use on the Kripalu.org site only.
//
// © Copyright Tag New Media 2008. All Rights Reserved. 
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// print this page
//-----------------------------------------------------------------------------
function PrintPage() {
	window.print();  
	return false;
}

//-----------------------------------------------------------------------------
// send page url to email address
//-----------------------------------------------------------------------------
function SendToFriend() {
	alert('send to a friend');
	return false;
}

//--------------------------------------------------------------------------------
//																blurFriendName
//--------------------------------------------------------------------------------
function blurFriendName(el) {
	if (el == $('register_friend') && $('register_friend').value != "") {
		$('radio_friend').checked = true;
		$('register_friend2').value = '';
	}
	else if (el == $('register_friend2') && $('register_friend2').value != "") {
		$('radio_both').checked = true;
		$('register_friend').value = '';
	}
	if ($('register_friend').value == "" && $('register_friend2').value == "") {
		$('radio_me').checked = true;
	}
}


//-----------------------------------------------------------------------------
// check or uncheck all checkboxes in a set (defined by field name prefix)
//-----------------------------------------------------------------------------
function CheckAllSubthemes(name_prefix) {
	var inputs = document.getElementsByTagName('input');  //all form inputs
	var checkboxes = new Array();  //set of checkboxes we want to modify
	var unchecked = false;  //is a checkbox field in this set checked?

	//build array of correct checkboxes
	for (i = 0; i < inputs.length; i++) {
    if (inputs[i].type == 'checkbox' && (inputs[i].name.substring(0, name_prefix.length) == name_prefix)) {
      checkboxes[checkboxes.length] = inputs[i];
			if (!inputs[i].checked) {
				//record if any checkbox is unchecked
				unchecked = true;
			}
	  }
	}
	
	//perform an action on the set of checkbox fields
	for (i = 0; i < checkboxes.length; i++) {
		if (unchecked) {
			//if any are unchecked, check all
  		checkboxes[i].checked = true;
		} else {
			//else uncheck all
  		checkboxes[i].checked = false;
		}
	}
	return false;
}


//--------------------------------------------------------------------------------
//																ClearAddress
// clear the companion address fields on checkout step1 page
//--------------------------------------------------------------------------------
function ClearAddress(n) {
	$("companion_address_" + n).value = "";
	$("companion_city_" + n).value = "";
	$("companion_state_" + n).selectedIndex = 0;
	$("companion_zipcode_" + n).value = "";
	$("companion_country_" + n).value = "";
}


//--------------------------------------------------------------------------------
//																CopyAddress
//
// copy the address fields to companion address fields on checkout step1 page
//--------------------------------------------------------------------------------
function CopyAddress(n) {
	$("companion_address_" + n).value = $("address").value;
	$("companion_city_" + n).value = $("city").value;
	var index = $('state').selectedIndex;
	$("companion_state_" + n).value = $('state').options[index].value;
	$("companion_zipcode_" + n).value = $("zipcode").value;
	$("companion_country_" + n).value = $("country").value;
}



//-----------------------------------------------------------------------------
// discoverVideo popup
//-----------------------------------------------------------------------------
function discoverVideo() {
	var newwindow;
	newwindow = window.open("/play_video/discover_kripalu","DiscoverKripalu",'width=520,height=330,left=500,top=375,resizable=yes,scrollbars=no');
	if (window.focus) {
		newwindow.focus()
	}
}


//--------------------------------------------------------------------------------
//																keypress
//--------------------------------------------------------------------------------
function keypress() {
	if (window.event.keyCode==13) {
		submit_form();
		window.event.returnValue = false;
		window.event.cancel = true;
	} 
}

//--------------------------------------------------------------------------------
//																hasClass
//--------------------------------------------------------------------------------
function hasClass(el, class_to_match) {
    var c;
    if (el && el.className && typeof class_to_match === "string") {
        c = el.getAttribute("class");
        c = " "+ c + " ";
        return c.indexOf(" " + class_to_match + " ") > -1;
    } else {
        return false;
    }
}

//-----------------------------------------------------------------------------
// Show/Hide building preference options on Add/Edit registration pages
//-----------------------------------------------------------------------------
function buildingPreference(theForm) {
//*** all room choices have class accom-choice
	var radios = $$(".accom-choice");
	var radiosendto = $$("#send_to");
	var privateChecked = false;
  var re = /PB/i; // Private Bath
  var replus = /\+/; // test for myself+friend  
  var sendto_val = "";  
  if (theForm.send_to.type=="hidden") //r and r
  {  sendto_val = theForm.send_to.value; }
  else
  {
    for (var i=0; i < theForm.send_to.length; i++)
    {
      if (theForm.send_to[i].checked)
      { sendto_val = theForm.send_to[i].value; }
    }
  }
  var twosome = replus.test(sendto_val); // true if myself+friend

 // alert("sendto_val: "+sendto_val+", twosome: "+twosome);
  for (var i = 0; i < radios.length; i++) {
		//if (radios[i].checked && (radios[i].value == 'p' || radios[i].value == 'p1')) {
		if ( radios[i].checked && re.test(radios[i].value) ) {
			privateChecked = true;
			$('accom' + i).insert({'after': $('building-list')});
			$('building-list').show();
 			new Effect.Highlight('building-list', {duration: 1});
		}
  // bed preference toggle
    if (radios[i].checked)
    {
      if ( hasClass(radios[i],'bedpref') )  // everything but dorm
      {
  // alert("sendto_val: "+sendto_val+", twosome: "+twosome);
       if (twosome) // always give bed choice
        { $('bedpref-select').show(); }  
        else if (radios[i].value.substr(2,1) == "S")
        { $('bedpref-select').hide(); 	$(radios[i]).removeClassName('error'); } 
        else
        { $('bedpref-select').show(); }
      }
      else
      { $('bedpref-select').hide(); } 
    }
  }
    if (!privateChecked) {
      $('building-list').hide();
      radios = $$('#building-list input');
        for (var i = 0; i < radios.length; i++)
        radios[i].checked = false;
    }

}	

//-----------------------------------------------------------------------------
// Show/Hide toggle using Scriptaculous
//-----------------------------------------------------------------------------
function showHide(link, textShow, textHide) {
	// find the div that contains the text we want to hide/show
	var theDiv = link.parentNode.getElementsByTagName("div")[0];
	if (link.innerHTML == textShow) {
		// we need to show the div
		Effect.BlindDown(theDiv);
		link.innerHTML = textHide;
	} else {
		// we need to hide the div
		Effect.BlindUp(theDiv);
		link.innerHTML = textShow;
	}
}


//-----------------------------------------------------------------------------
// Show/Hide toggle using Scriptaculous
//-----------------------------------------------------------------------------
function ShowHide(element_id) {

	if ($(element_id).style.display == 'none') {
		Effect.BlindDown(element_id);
	} else {
		Effect.BlindUp(element_id);
	}
	
	return false;
}

//--------------------------------------------------------------------------------
//																submit_form
//--------------------------------------------------------------------------------
function submit_form () {

	if (validate())
		$('radio_me').form.submit();

}


//--------------------------------------------------------------------------------
//																submit_form
//--------------------------------------------------------------------------------
function submit_rr_form () {

	if (validate_rr())
		$('start-date').form.submit();

}


//--------------------------------------------------------------------------------
//															validate
// validate the Add/Edit registration form
//--------------------------------------------------------------------------------
function validate () {

	$('accommodations-text').removeClassName('error');
	$('friendsname-text').removeClassName('error');
	$('building-text').removeClassName('error');
	$('bedpref-text').removeClassName('error');
	//$('specialneeds-text').removeClassName('error');
	var accomchoices = $$(".accom-choice");

	var radio_me = $('radio_me').checked;
	var radio_friend = $('radio_friend').checked;
	var radio_both = $('radio_both').checked;
	if (!(radio_me || radio_friend || radio_both)) {
		alert("Please specify who you are registering.");
		return false;
	}

	if (radio_friend) {
		if ($('register_friend').value == '') {
			$('friendsname-text').addClassName('error');
			alert("Please enter your friend's first name.");
			$('register_friend').select();
			return false;
		}
	}

// added for ancillary web rates changes with new last name field 8/24/10 vgw  
	if (radio_friend) {
		if ($('register_friendlast').value == '') {
			$('friendsname-text').addClassName('error');
			alert("Please enter your friend's last name.");
			$('register_friendlast').select();
			return false;
		}
	}

  // got rid of hardcoded number of accommodations choices for new web rates 8/24/10 vgw
	var accommodations = '';
  var re = /PB/i; // Private Bath 
  var checkBPP = 0; //private
  var checkBPS = 0; //shared
	for (var i=0; i<=accomchoices.length-1; ++i) {
//alert("i: "+i);
		if ($('radio' + i).checked)
		{  // validate bedpref for these
      accommodations = $('radio' + i).value;
      if (hasClass($('radio' + i),'bedpref') && accommodations.substr(2,1) == "P") //private room
        checkBPP = 1;
      if (hasClass($('radio' + i),'bedpref') && (radio_both && accommodations.substr(2,1) == "S" )) //shared room for 2 
        checkBPS = 1;
    }
	}
	if (accommodations == '') {
		$('accommodations-text').addClassName('error');
		alert("Please choose accommodations.");
		return false;
	}
	//else if (accommodations == 'p' || accommodations == 'p1') {
  // new room rate structure vgw 9/2/10
	else if ( re.test(accommodations) ) {
		var preference = "";
		radios = $$("#building-list input");
    	for (var i = 0; i < radios.length; i++)
      {
        if (radios[i].checked)
          preference = radios[i].value;
      }
        if (preference == "") {
          $('building-text').addClassName('error');
          alert("Please choose a building preference");
          return false;
        }
      //}	
	}
/*
  var qrank = document.getElementById("queen_rank");
  var trank = document.getElementById("twin_rank");
//alert("qrank: "+qrank.options[qrank.selectedIndex].value+", trank: "+trank.options[qrank.selectedIndex].value+", checkbp: "+checkBP);
  if (checkBPP || checkBPS) // bedpref and (private room or shared room for 2)
  {
    if ((qrank.options[qrank.selectedIndex].value == '' || trank.options[trank.selectedIndex].value == '') && radio_both) {
      $('bedpref-text').addClassName('error');
      alert("Please specify a rank for your bed preferences."); 
      return false;
    }
    if (qrank.options[qrank.selectedIndex].value == trank.options[trank.selectedIndex].value) {
      $('bedpref-text').addClassName('error');
      alert("Please specify a different rank for each bed preference.");
      return false;
    }
  }
  else
  {  // dorm/single shared needs values supplied
    qrank.value = 2;
    trank.value = 1;
  } */
	return true;
}		


//--------------------------------------------------------------------------------
//															validate_rr
// validate the Add/Edit R&R registration form
//--------------------------------------------------------------------------------
function validate_rr () {

	$('accommodations-text').removeClassName('error');
	$('building-text').removeClassName('error');
	$('bedpref-text').removeClassName('error');
	var accomchoices = $$(".accom-choice");
//|\d{4}
	var datePattern = /^\d{1,2}\/\d{1,2}\/(\d{2}|\d{4})$/;
	var startDate = $('start-date').value;
	var	endDate = $('end-date').value;

  //alert("pattern: "+datePattern+", startdate: "+startDate+", enddate: "+endDate);
	if (!datePattern.match(startDate)) {
		alert("Please enter a valid Arrival Date");
		$('start-date').select();
		return false;
	}
	if (!datePattern.match(endDate)) {
		alert("Please enter a valid Departure Date");
		$('end-date').select();
		return false;
	}

	var accommodations = '';
  var re = /PB/i; // Private Bath 
  var checkBPP = 0;
  var checkBPS = 0; //shared
	for (var i=0; i<=accomchoices.length-1; ++i) {
//alert("i: "+i);
		if ($('radio' + i).checked)
		{
      accommodations = $('radio' + i).value;
      if (hasClass($('radio' + i),'bedpref') && accommodations.substr(2,1) == "P") //private room
        checkBPP = 1;
      //if (hasClass($('radio' + i),'bedpref') && (radio_both && accommodations.substr(2,1) == "S" )) //shared room for 2 
      //  checkBPS = 1;  this doesn't work for r&r; there is no myself+friend
    }
	}
	if (accommodations == '') {
		$('accommodations-text').addClassName('error');
		alert("Please choose accommodations.");
		return false;
	}
	//else if (accommodations == 'p' || accommodations == 'p1') {
  // new room rate structure vgw 9/2/10
	else if ( re.test(accommodations) ) {
		var preference = "";
		radios = $$("#building-list input");
    	for (var i = 0; i < radios.length; i++)
      {
        if (radios[i].checked)
          preference = radios[i].value;
      }
      if (preference == "") 
      {
        $('building-text').addClassName('error');
        alert("Please choose a building preference");
        return false;
      }
      //}	
	}

/*
  var qrank = document.getElementById("queen_rank");
  var trank = document.getElementById("twin_rank");
    //var strUser = e.options[e.selectedIndex].value;
  if (checkBPP || checkBPS) // bedpref and (private room or shared room for 2)
  {
    if (qrank.options[qrank.selectedIndex].value == '' || trank.options[trank.selectedIndex].value == '') {
      $('bedpref-text').addClassName('error');
      alert("Please specify a rank for your bed preferences.");
      return false;
    }
    if (qrank.options[qrank.selectedIndex].value == trank.options[trank.selectedIndex].value) {
      $('bedpref-text').addClassName('error');
      alert("Please specify a different rank for each bed preference.");
      return false;
    }
  }
  else
  {  // dormitory needs values supplied
    qrank.value = 2;
    trank.value = 1;
  }
*/
	return true;
}


//-----------------------------------------------------------------------------
// YogaBreak popup
//-----------------------------------------------------------------------------

//added 25 pixels to height to reveal caption w/link below shockwave frame 2.12.09 PHILW
function yogaBreak() {
	var newwindow;
	newwindow = window.open("play_video/yoga_break","YogaBreak",'width=511,height=490,left=500,top=375,resizable=yes,scrollbars=no');
	if (window.focus) {
		newwindow.focus()
	}
}



