jQuery(document).ready( function($) {

$(function() {
	var newerrorstring="";
	var done=0;
  $('.error').hide();

  $('input.text-input').css({backgroundColor:"#FFFFFF"});
/*
  $('.modify').focus(function(){
		if($(this).checked=='checked'){
			var thiskey=$(this).attr('id');
			var modifyclientid=$(this).val();
			$.post("../wp-content/plugins/booktracker/functions.booktracker.php",{
				'f':'modify',
				'infotype':thiskey,
				'modifyclientid':modifyclientid,
		   		'value':$('#' + thiskey).val()
				}
			);

		}

  });
*/
$('.webmailcall').live('click',function(){
	var page=document.location.href;
	page=page.replace("#","");
	var title=$(this).attr('title');
	if(title!='')$('#dialog').dialog('option', 'title', title);
	var requesttype=$(this).attr('id');
   	$('#dialog').load(page,{
			'ajaxrequest':requesttype
			},
			function(data){
				$('#dialog').html(data);// add the data to the div
		  		$('#dialog').dialog('open');
				$('#subject').focus();
				$('#subject').css({backgroundColor:"#FFDDAA",border:'2px inset black'});

				$('input.text-input').focus(function(){
					$(this).css({backgroundColor:"#FFDDAA",border:'2px inset black'});
					$(".swd-validation-errors").show();
				});

				$('input.text-input').blur(function(){
					var itemid=$(this).attr("id");// assign the id of the input element to the variable itemid
					var itemclass=$(this).attr("class");// get the class of the element and assign it to a variable
					$(this).css({backgroundColor:"#FFFFFF"});
					$('#' + itemid + '-modify').checked='';
				    $.post(page,{
					'ajaxrequest':'validate',
					'f':itemid,
					'value' : $('input#' + itemid).val(),
					'class' : itemclass,
					'id' : itemid},
					function(data){
				        var dataarray=data.split("&&");// split the returned message into an array
						// the array that is returned the keys will be in this format:
						// validated=0, valid-result=1,valid-value=2
						var validated=dataarray[0].substring(10);// 0 (validation failed) or 1 (validation occured)
						var validresult=dataarray[1].substring(13);// 0 (invalid) or 1 (valid)
						var validvalue=dataarray[2].substring(12);// value of the data
						var validerror=dataarray[3].substring(12);// the error message from validation
						var errors=$("#swd-errors").val();// get the existing errors
						var found=errors.indexOf(itemid);// check the existing errors for a match with the itemid
/*						if(found<0){// create a replacement value for the string if we need it later - this means the value wasn't found
							var newerrorstring="&&" + itemid;// create a string to be added to the listing of errors
						}
						else{// the value already exists, so don't duplicate it
  //							var matchstring='';
							newerrorstring='';//
						}
  */						var divid="div" + itemid;

						if(validated==1 && validresult==1){// this was successfully validated
							$('input#' + itemid).val(validvalue);// assign the data to the input element if it is valid
			  		   		if(found>=0){// if the error already exists, we now need to remove it since the error has been corrected
								if(errors.length>0){
									errorarray=errors.split('&&' + itemid);// split the original errors with the itemid
									var newerrorcount=errorarray.length;
								}
								else{
									var errorarray='';
									var newerrorcount=0;
								}
								if(errorarray.length==2){
									if(errorarray[0].length>0){
										newerrorstring=errorarray[0];
									}
									else{
										newerrorstring='';
									}

									if(errorarray[1].length>0){
										newerrorstring=newerrorstring+errorarray[1];
									}

								}
						 	}
/*                           var diagnostics='there are no errors on 98 and  diagnostics are found=' + found + ' for the itemid of ' + itemid + ' the new errorcount is ' + newerrorcount + ' the newerrorstring is ' + newerrorstring + ' and the errors are ' + errors + ' for the parts ' + errorarray[0] + ' and ' + errorarray[1];
$('.swd-debug').html(diagnostics);
*/
						}
						else{
							// the data was not validated or validation failed
							if(validated==1 && validresult==0){
								// data was validated, but is not valid
								$('input#' + itemid).val('');// return a blank to the form field
								$('input#' + itemid).css({'border':'2px solid red'});// highlight the field in red
								// if there is an error for this itemid, do nothing, otherwise, add it to the error string
					   			if(found<0){// add the error to the end of the string
									newerrorstring=errors + "&&" + itemid;

						 		}
							}
							else{// the only remaining option is that the validation itself failed
								$('#swd-validation-errors').append("<div id='" + divid + "' style='background-color:blue;color:red'>Fatal Error - Validation Failed</div>");
							}


						}
						if(data=='invalid'){
							newerrorstring=errors + '&&Fatal Validation Error';
						}

						if(newerrorstring!=''){
					   //		$(".swd-validation-errors").show();
					   //		$(".swd-validation-errors").html('errorcount is ' + newerrorcount + 'found is ' + found + ' for the errors ' + errors + ' and the errorstring is ' + newerrorstring);
							$("#swd-errors").val(newerrorstring);// replace the error string in the form
						}
						else {// if there are no errors, clear the field and hide it
							$(".swd-validation-errors").hide();
							$('.swd-validation-errors').html(newerrorstring);
							$('#swd-errors').val('');
						}// end newerrorstring if statement

					});// end post and data function call

				});// end input.text function

		$(".swd-validates-as-selected").live('click',function() {
			// validate and process form if there are no errors.  First, check for errors, then proceed if there are none
	//		$(this).select().focus();
			var checkrequired='';
			$('.swd-validates-as-required').each(function(){// check to see if all required field are completed.  if not, set a value for the missing fields
				if($(this).is(".text-input")){
					if($(this).val()==''){
						$(this).focus();
						$(this).css({'border':'2px solid red'});// highlight the field in red
						checkrequired=checkrequired + '&&' + $(this).attr('id');
					}
				}

			});
   		 	if(checkrequired!=''){$('#swd-errors').val(checkrequired);}

			if($('#swd-errors').val()==''){// if there are no validation errors allow the sending of the form
//			$(".swd-validation-errors").empty();
				// first hide any error messages
				var page=document.location.href;
				page=page.replace("#","");

				var datalist='';
				var processingpage=$('.swd-processing-page').html();
      			if(done==0){
					done=1;
	  	 			$('.text-input').each(function(){
				   	datalist=datalist + '%%' + $(this).attr('name') + '=' + $(this).val();
					}); // end each statement

 					$.post(page,{
						ajaxrequest:processingpage,
						datalist:datalist
					},
					function(data){
 						if(data=='close'){
 							$('#dialog').empty();
 							// this is for a dialog box, so close the box
 							$('#dialog').dialog('close');
							$('#swd-errors').empty();
							done=0;
 						}
						else{
							var darray=data.split(':');
							if(darray[0]=='replace'){
								$('#dialog').html(darray[1]);
							}

						}
					});// end function and post
				}// end if statement (done==0) for processing
  //  return false;
 			}
			else{// this is the option to show validation errors
			$(".swd-validation-errors").html("<p style='color:red'>There are validation errors.  Please fix the fields outlined in red</p>");
			}

			});// end swd-validates-as-selected loop

  });// end data function call and load page statement
  });// end webmail loop
/*
  $("#rememberme").click(function(){
  	$("#rememberme").val('1');
  });

*/
$(".ajax-loader").ajaxStart(function(){// show the animated gif when loading ajax
   $(this).show();
 });

$(".ajax-loader").ajaxStop(function(){// hide the animated gif
   $(this).hide();
 });


});// end main function call


});// end main jquery call

