
function is_valid_email (str)
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1 || 
				(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) ||
				(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) ||
				(str.indexOf(at,(lat+1))!=-1) ||
				(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) ||
				(str.indexOf(dot,(lat+2))==-1) ||
				(str.indexOf(" ")!=-1))
		    return false;
		
 		return true;					
	//return /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/.test(email);
}

function validateFields() {
  var af_card_colour = document.af_form.af_card_colour;
  var af_name = document.af_form.af_name;
  var af_patient_name = document.af_form.af_patient_name;
//  var af_patient_room = document.af_form.af_patient_room;
//  var af_patient_ward = document.af_form.af_patient_ward;
  var af_message = document.af_form.af_message;
  var af_email = document.af_form.af_email;
  var af_code = document.af_form.af_code;
  var whiteSpace = /^[\s]+$/;

  var colour_selected = false;
  var i;
  for ( i = 0 ; i < af_card_colour.length ; i++ )
    {
      if ( af_card_colour[i].checked )
        {
          colour_selected = true;
        }
    }

  if ( ! colour_selected ) {
    displayAFFeedback( 'Select a card colour' );
  }
  else if ( af_name.value == '' || whiteSpace.test( af_name.value ) ) {
    displayAFFeedback( 'Enter your name to put on the greeting card' );
  }
  else if(!is_valid_email(af_email.value)){
  		displayAFFeedback( 'Please enter a valid email address' );
  }
  else if ( af_patient_name.value == '' || whiteSpace.test( af_patient_name.value ) ) {
    displayAFFeedback( 'Enter the name of the patient to send the greeting card to' );
  }
//  else if ( af_patient_room.selectedIndex == 0 ) {
//    displayAFFeedback( 'Select the patient room no.' );
//  }
//  else if ( af_patient_ward.selectedIndex == 0 ) {
//    displayAFFeedback( 'Select the patient ward no.' );
//  }
  else if ( af_message.value == '' || whiteSpace.test( af_message.value ) ) {
    displayAFFeedback( 'Enter a message to put on the greeting card' );
  }
  else if ( af_message.value.length > 200 ) {
    displayAFFeedback( 'Your message is too long. Shorten your message' );
  }
  else if ( af_code.value.length !== 6 ) {
    displayAFFeedback( 'The security code is incorrect. Please re-enter it' );
  }
  else {
    sendPosEmail();
  }
}
function sendPosEmail() {
  change_button();  
   var af_form = document.af_form;
  var af_feedback = document.getElementById( 'af_feedback' );
  var af_card_colour = document.af_form.af_card_colour;
  var af_name = document.af_form.af_name;
  var af_email = document.af_form.af_email;
  var af_patient_name = document.af_form.af_patient_name;
//  var af_patient_room = document.af_form.af_patient_room;
//  var af_patient_ward = document.af_form.af_patient_ward;
  var af_message = document.af_form.af_message;
  var af_code = document.af_form.af_code;

  var url = "/greeting_card_form_process_ajax.php?contact=true&xml=true";
  
  showContactTimer(); // quickly begin the load bar
  af_feedback.className = 'hide'; // hide the af_feedback bar (incase this is a multi-email

  var colour_selected = '';
  var i;
  for ( i = 0 ; i < af_card_colour.length ; i++ )
    {
      if ( af_card_colour[i].checked )
        {
          colour_selected = af_card_colour[i].value;
          break;
        }
    }

  // convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.
  var af_card_colour_enc = encodeValue( colour_selected );
  var af_name_enc = encodeValue( af_name.value );
  var af_email_enc = encodeValue( af_email.value );
  var af_patient_name_enc = encodeValue( af_patient_name.value );
//  var af_patient_room_enc = encodeValue( af_patient_room.options[af_patient_room.selectedIndex].value );
//  var af_patient_ward_enc = encodeValue( af_patient_ward.options[af_patient_ward.selectedIndex].value );
  var af_message_enc = encodeValue( af_message.value );
  var af_code_enc = encodeValue( af_code.value );
  
//  var stuff = "af_card_colour="+af_card_colour_enc+"&af_name="+af_name_enc+"&af_email="+af_email_enc
//    +"&af_patient_name="+af_patient_name_enc+"&af_patient_room="+af_patient_room_enc
//    +"&af_patient_ward="+af_patient_ward_enc+"&af_message="+af_message_enc;
  var stuff = "af_card_colour="+af_card_colour_enc+"&af_name="+af_name_enc+"&af_email="+af_email_enc
    +"&af_patient_name="+af_patient_name_enc+"&af_message="+af_message_enc+"&af_code="+af_code_enc;

  // notice the use of a proxy to circumvent the Same Origin Policy.

	new Ajax.Request(url, {
 	 method: 'post',
 	 postBody:stuff,
 	 onComplete: function(transport) {
			if(transport.responseXML.documentElement.getElementsByTagName('status')[0].childNodes[0].nodeValue == 'OK'){
				$('af_feedback_good').innerHTML = '<p>'+transport.responseXML.documentElement.getElementsByTagName('confirmation')[0].childNodes[0].nodeValue+'</p>';
				//$('sent_button').setAttribute('src','_r/img/btn_snd_msg_close.png');
				
				$('sent_button').addClass('hide');
				$('close_button').removeClass('hide');

				your_name = $('your_name');
				$('af_feedback_good').removeClass('hide');
				close = $('close_button')
				addEvent( close, 'click', slide_toogle, false );
				//addEvent( your_name, 'focus', function(){alert('focus')}, false );
				af_form.reset();
			}else{
				displayAFFeedback(transport.responseXML.documentElement.getElementsByTagName('confirmation')[0].childNodes[0].nodeValue)
				change_button();
			}
	}
	})
}
function encodeValue( str ) {
  str = str.replace( /&/g,"**am**" );
  str = str.replace( /=/g,"**eq**" );
  str = str.replace( /\+/g,"**pl**" );
  return str;
}
function displayAFFeedback( message ) {
//var loader = document.getElementById( 'loadBar' );
 var af_feedback = document.getElementById( 'af_feedback' );
 
  af_feedback.className = '';
  af_feedback.innerHTML = '<p>'+message+'</p>';
}
function showContactTimer() {
  //var loader = document.getElementById( 'loadBar' );
 // loader.className = '';
  //sentTimer = setTimeout( "hideContactTimer()",6000 );
}
function hideContactTimer() {
  var loader = document.getElementById( 'loadBar' );
  var af_feedback = document.getElementById( 'af_feedback' );
  var af_form = document.af_form;

  //displayAFFeedback( grabPosXML( 'confirmation' ) );

  af_form.reset();
}

function slide_toogle(){
	mySlide.toggle();
	$('af_feedback_good').innerHTML = '';
	ajaxContact();
}

function change_button(is_submitted){
	if($('sent_button').getAttribute('src') == '_r/img/btn_snd_msg.gif'){
		$('sent_button').setAttribute('src','_r/img/btn_sending_msg.png');
	}else{
		$('sent_button').setAttribute('src','_r/img/btn_snd_msg.gif');
	}
}

function ajaxContact() {
  $('close_button').addClass('hide');
  $('sent_button').removeClass('hide');
  var af_form = document.af_form;
  var af_feedback = document.getElementById( 'af_feedback' );
   af_feedback.className = 'hide';
  var af_feedback_good = document.getElementById( 'af_feedback_good' );
   af_feedback_good.className = 'hide';
  change_button();
  addEvent( af_form, 'submit', validateFields, false );
  
  af_form.onsubmit = function() { return false; }
}
addEvent( window, 'load',ajaxContact, false );

