// JavaScript Document

function GetXmlHttpObject()
{
var xmlHttp=null;

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	
    }
  }
return xmlHttp;
}



function getTeams(optionSelected)
{
   var strURL="findTeams.php?optionSelected="+optionSelected;
   var xmlHttp=GetXmlHttpObject();
   if (xmlHttp)
   {
     xmlHttp.onreadystatechange = function()
     {
      if (xmlHttp.readyState == 4)
      {
	 // only if "OK"
	 	 if (xmlHttp.status == 200)
         {
	    	document.getElementById("DropDownTeams").innerHTML=xmlHttp.responseText;
	 	} else {
   	   	alert("There was a problem while using XMLHTTP:\n" + xmlHttp.statusText);
	 	}
      }
     }
   xmlHttp.open("GET", strURL, true);
   xmlHttp.send(null);
   }
   if(optionSelected.length==0 || optionSelected=="individual" || optionSelected=="event")
   getRiders(optionSelected);
}


function getRiders(teamName)
{
 
  var strURL="findRiders.php?teamName="+teamName;
  var xmlHttp=GetXmlHttpObject();
  if (xmlHttp)
  {
    xmlHttp.onreadystatechange = function()
    {
      if (xmlHttp.readyState == 4) // only if "OK"
      {
        if (xmlHttp.status == 200)
        {
		  document.getElementById("DropDownRiders").innerHTML=xmlHttp.responseText;
        } else {
          alert("There was a problem while using XMLHTTP:\n" + xmlHttp.statusText);
        }
      }
    }
    xmlHttp.open("GET", strURL, true);
    xmlHttp.send(null);
  }
}





//*********************************************************************
//Validation Function for BhfDp.php 
function validateSub1() {
  
  var fName = document.PledgeForm.firstName.value;
  var lName = document.PledgeForm.lastName.value;
  var organization = document.PledgeForm.organization.value;  
  var email = document.PledgeForm.email.value;
  var contactNo = document.PledgeForm.contactNo.value;
  var currency = document.PledgeForm.currency.value;
  var amount = document.PledgeForm.amount.value;
  var mode = document.PledgeForm.mode.value;
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  if(fName.length==0 || fName=="First Name") {
   	inlineMsg('fName','You must enter your First Name.',3);
    return false;
  }
  
  if(lName.length==0 || lName=="Last Name") {
    inlineMsg('lName','You must enter the your Last Name.',3);
    return false;
  }
 
  if(organization.length==0) {
	inlineMsg('organization','You must enter your organization name.',3);
    return false;
  }
  if(email.length == 0) {
    inlineMsg('email','You must enter your E-mail.',3);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',3);
    return false;
  }
  
  if(contactNo.length == 0) {
    inlineMsg('contactNo','You must enter your mobile number.',3);
    return false;
  }
  
  /*if(contactNo != "")
  {	
	  var check=true;
	  var newKey;
	  for(var i=0; i<contactNo.length; i++)
	  {
		   newKey=contactNo.charAt(i);
		  if(((newKey < "0" ) || (newKey > "9")) && !(newKey == ""))
		  {
			  check=false;
			  break;
		  }
	  }
	  if(!check || contactNo.length != 10)
	  {
		  inlineMsg('contactNo','Please enter a valid mobile number',3);
   		  return false;
	  }
	  
  }*/
  
  if(mode.length == 0) {
    inlineMsg('mode','Select the mode of payment.',3);
    return false;
  }
  if(currency.length == 0) {
    inlineMsg('currency','Select the currency.',3);
    return false;
  }
  if(amount.length == 0 || (document.PledgeForm.amount.value == 0)) {
    inlineMsg('amount','Please pledge the amount.',3);
    return false;
  } 
   if(amount != "")
  {	
	  var check=true;
	  var newKey;
	  for(var i=0; i<amount.length; i++)
	  {
		   newKey=amount.charAt(i);
		  if(((newKey < "0" ) || (newKey > "9")) && !(newKey == ""))
		  {
			  check=false;
			  break;
		  }
	  }
	  if(!check)
	  {
		  inlineMsg('amount','Please enter only numbers',3);
   		  return false;
	  }
	  
  }
  
   
  return true;
}
//*********************************************************************************************
// Validation function for Donate/Pledge.php

function validateSub() {
  
  var fName = document.PledgeForm.firstName.value;
  var lName = document.PledgeForm.lastName.value;
  var organization = document.PledgeForm.organization.value;  
  var email = document.PledgeForm.email.value;
  var contactNo = document.PledgeForm.contactNo.value; 
  var pledge_for = document.PledgeForm.pledge_for.value;
  var currency = document.PledgeForm.currency.value;
  var amount = document.PledgeForm.amount.value;
  var mode = document.PledgeForm.mode.value;
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  if(fName.length==0 || fName=="First Name") {
   	inlineMsg('fName','You must enter your First Name.',3);
    return false;
  }
  
  if(lName.length==0 || lName=="Last Name") {
    inlineMsg('lName','You must enter the your Last Name.',3);
    return false;
  }
 
  if(organization.length==0) {
	inlineMsg('organization','You must enter your organization name.',3);
    return false;
  }
  if(email.length == 0) {
    inlineMsg('email','You must enter your E-mail.',3);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',3);
    return false;
  }
  
  if(contactNo.length == 0) {
    inlineMsg('contactNo','You must enter your mobile number.',3);
    return false;
  }
  
 /* if(contactNo != "")
  {	
	  var check=true;
	  var newKey;
	  for(var i=0; i<contactNo.length; i++)
	  {
		   newKey=contactNo.charAt(i);
		  if(((newKey < "0" ) || (newKey > "9")) && !(newKey == ""))
		  {
			  check=false;
			  break;
		  }
	  }
	  if(!check || contactNo.length != 10)
	  {
		  inlineMsg('contactNo','Please enter a valid mobile number',3);
   		  return false;
	  }
	  
  }*/
  
  if(pledge_for.length == 0) {
    inlineMsg('pledge_for','Select one of the option.',3);
    return false;
   }
  if(mode.length == 0) {
    inlineMsg('mode','Select the mode of payment.',3);
    return false;
  }
  if(currency.length == 0) {
    inlineMsg('currency','Select the currency.',3);
    return false;
  }
  if(amount.length == 0 || (document.PledgeForm.amount.value == 0)) {
    inlineMsg('amount','Please pledge the amount.',3);
    return false;
  } 
   if(amount != "")
  {	
	  var check=true;
	  var newKey;
	  for(var i=0; i<amount.length; i++)
	  {
		   newKey=amount.charAt(i);
		  if(((newKey < "0" ) || (newKey > "9")) && !(newKey == ""))
		  {
			  check=false;
			  break;
		  }
	  }
	  if(!check)
	  {
		  inlineMsg('amount','Please enter only numbers',3);
   		  return false;
	  }
	  
  }
  return true;
}
//**************************************************************************
// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,100); 
  arrow.src = "images/msg_arrow.gif"; 
}

//*********************************************************************************************
function resetForm()
{
	document.PledgeForm.reset();
}
function ClearForm(field)
{
	if(field.value=="First Name" || field.value=="Last Name")
	field.value="";
}

//*********************************************************************************************

function activateFields(option)
{	
	
	if(option=="Cash" || option=="Check " || option=="DD" || option=="Account Transfer" || option=="Credit Card")
	{
		setTable('table1',option);		
		return false;
	}	
	else if(option=="Payroll Deduction")
	{
		setTable('table1',option);		
		return false;		
	} 
	else if(option=="one_time" || option=="recurring")
	{		
		 var amount=document.PledgeForm.amount.value;
		 if(amount.length==0) 
		 { 
			 alert(" Enter a valid Pledge Amount"); 
			 if(document.PledgeForm.type_of[0].checked)
			 document.PledgeForm.type_of[0].checked=false;
			 if(document.PledgeForm.type_of[1].checked)
			 document.PledgeForm.type_of[1].checked=false;
			 return false; 
		 }
		 if(option=="recurring")
		 {
			 document.PledgeForm.months.disabled=false;
		 }else
		 {
			 document.PledgeForm.months.disabled=true;
			var amount=document.PledgeForm.amount.value; 
			//document.PledgeForm.o_amount.value=amount;
			document.PledgeForm.t_amount.value=amount;
		 }
	}else
	{
		var amount=document.PledgeForm.amount.value;
		var months=document.PledgeForm.months.value;
		var total=(amount*months);		
		//document.PledgeForm.o_amount.value=amount;
		document.PledgeForm.t_amount.value=total;
	}
}

function setTable(what,option)
{
	if(option=="Cash" || option=="Check " || option=="DD" || option=="Account Transfer" || option=="Credit Card")
	{
		if(document.getElementById(what).style.display=="block")
		{
			document.getElementById(what).style.display="none";
		}
	}
	else if(option=="Payroll Deduction")
	{
		if(document.getElementById(what).style.display=="none")
		{
			document.getElementById(what).style.display="block";
		}
	}
}