document.observe('dom:loaded' , function() {
	SubmitRequest.init();
	if( $('accordion')){
		$$('div.sdetail').invoke('hide');
	}
});
var display_session_details = function (c){
	var s=$(c).next('div').identify();	
	var sd = $(s).style.display;
	var h = ($(s).getHeight())+500;
	var ah = $('accordion').getHeight();	
	//to start hide all the sdetails
	
	if (sd=='none'){
		$$('div.sdetail').invoke('hide');	
		new Effect.BlindDown($(s), {
           duration:.5
        });
		if (h > ah){
		$('accordion').style.height=h+'px';
 		}
		if ((ah-h)>200){
		$('accordion').style.height = h+'px';
		}
	}else{//end of if dispaly == none	
		new Effect.BlindUp($(s), {
           duration:.5
        });
	}
}

var display_all_sdetail= function(){
	$$('div.sdetail').invoke('show');
}
 
function showhideheader (){// used to turn off the header for one page view
		$('header').toggle();
		}
		
//ajax email javascript functions.

function toggleFields(id,d){// id means the id of the element you are referring to. d means the display value, hide, block, inline.
	$('maildiv'+id).style.display = d;
	$('name'+id).focus();
}
function blank_mail(mid){
	
	$$('.mail').invoke('hide');
	//show('maildiv'+mid,"block");
	$('maildiv'+mid).show();

}

function countLineBreaks (string) {
  var re = /\r\n|\r|\n/g;
  var n = 0;
  while (re.exec(string))
    n++;
  return n;
}
function adjustRows (textarea) {
  if (document.all) {
    while (textarea.scrollHeight > textarea.clientHeight)
      textarea.rows++;
    textarea.scrollTop = 0;
  }
  else if (textarea.rows) {
    var lineBreaks = countLineBreaks(textarea.value);
    var rows = parseInt(textarea.rows);
    var wrap = textarea.getAttribute('wrap');
    if (lineBreaks > rows)
      textarea.rows = ++rows;
    else if (wrap.toLowerCase() == 'soft' || wrap.toLowerCase() =='hard') {
      while (textarea.rows * textarea.cols <= textarea.value.length) {
        textarea.rows = ++rows;
      }
    }
  }
}

function show_focus(mid){
	adjustRows(mid);
	document.getElementById(mid).style.background='#ffe59d';
}
function cancel(cid){
	var sid='subject'+cid;
	var mid='mailbox'+cid;
	document.getElementById(sid).value='';
	document.getElementById(mid).value='';
	toggleFields(cid,"none");
}
function call_send_mail(cid) {
	var message = $F('mailbox'+cid);
	var from = $F('from'+cid);
	var name = $F('name'+cid);
	var subject = $F('subject'+cid);
	var firstname = $F('firstname'+cid);
	var lastname = $F('lastname'+cid);
	var page = $F('page'+cid);
		
	var pars = 'id='+cid+'&firstname='+firstname+'&lastname='+lastname+'&from='+from+'&name='+name+'&subject='+subject+'&message='+message+'&page='+page+'&func=send_message';
	
	new Ajax.Request('http://www.phantomranch.org/ajax/ajax_email.php', 
		{parameters : pars, 
		onFailure : function(resp) {alert("Oops, there's been an error.");},
		onSuccess: function(transport) {
			var n = transport.responseText.evalJSON();
			alert (n.note);
			toggleFields(cid,'none');	
		}		
	});	
  }

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
		
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
var SubmitRequest = {
note_success: 'Request was submitted',
note_error: 'Due to technical difficulties, request was not submitted',
note_notvalid:'Your input data was not valid',
init: function(){
	$$('.srform').each(function(f){
		Event.observe(f.id,'submit', SubmitRequest.submithandler);
	});
},//end init
submithandler: function(e){
	e.stop();
	var id = Event.element(e).id;
	SubmitRequest.validate(id);
},
validate: function(id){
	$('vnote').update('');
	var errnum = 0;
	var inputs = $(id).getElements();
	inputs.each(function(input){
		var v=input.value;
		var className=input.className;
		var classes = className.split(" ");
		classes.each(function(class){
			
			var rule = SubmitRequest.rules[class];
				if (rule){
					if (!rule.test(v)){
						input.focus();
						$('vnote').insert('<div>'+SubmitRequest.errors[class]+'</div>');
						errnum++;
					}//end if test
				}//end if rule			
		});//end each classes
	});//end each inputs
	if (errnum==0){
	SubmitRequest.process(id);
	}
},//end validate
process: function(id){
	var pars = $(id).serialize();
	var url = $(id).action;
	new Ajax.Request(url, 
		{parameters : pars+'&ajax=ajax', 
		onFailure : function(resp) {alert("Oops, there's been an error.");},
		onSuccess: function(transport) {
			var t = transport.responseText.evalJSON();
			if (t.result=='success'){
			$('vnote').update(SubmitRequest.note_success);
			}else if(t.result=='no data'){
			$('vnote').update(SubmitRequest.note_notvalid);
			}else{
			$('vnote').update(SubmitRequest.note_error);
			}
		}//end onSuccess		
	});	//end new Ajax
	
},//end process
rules:{
    required: /\w/,
	fullname: /[a-zA-Z][ ][a-zA-Z]/,
	formselect: /\S/
  },//end rules
errors:{
	required: 'You must complete required fields.',
	fullname: 'Please provide the first and last names seperated by a space.',
	formselect: 'Please make a selection.'
	}//end errors
}//end SubmitRequest