$(document).ready(function(){


});


function loadPage(url,div) {
	try {
		$(div).load(url);
	}catch(err) {
		alert("Error in loadPage " + err.description);
	}
}


function updateDiv(div) {
	try {
		var options = {
			url :	$(div + " form").attr('action'),
			success : function(data) {
				$(div).html(data);
			}
		};
		
		$(div + " form").ajaxSubmit(options);
	}catch(err) {
		alert("Error in updateDiv : " + err.description);
	}
}


/**
 * fills out the payment info with the information entered in the students form
 *  (on checkbox click)
 *  @return void
 * 
 */
function fillPaymentBillingInfo() {
	try {
		$("#inputCcFname").val($("#inputStudentFname").val());
		$("#inputCcLname").val($("#inputStudentLname").val());
		
		$("#inputCcAddress").val($("#inputStudentAddress").val());
		$("#inputCcCity").val($("#inputStudentCity").val());
		$("#inputCcState").val($("#inputStudentState").val());
		$("#inputCcZip").val($("#inputStudentZip").val());
	}catch(err) {
		alert("Error in fillPaymentInfo : " + err);
	}
}



/***************************************************
This file holds all the necessary functions needed
by/for our AJAX calls
@author mhalla mhalla@ipro.org
created : jun 06/16/09 09:44 am


***************************************************/
 
 /**
  * this function loads the url into specified div
  *  @param  string url 
  *  @return void
  */
 function loadPage(url, div) {
 	try {

 		// show spinning gif while updating the div
 		var html = getLoadingHtmlString();
 		$(div).html(html);

 		$(div).load(url);
 	}catch(err) {
 		alert("Error (LoadPage) : " + err.description);
 	}
 }
 
 
 
 /**
  * function to update the div with returned html from ajax call
  * it automatically sends the form enclosed in the div
  * Note : this function depends on the ajaxsubmit plugin 
  * @param string div - the dom id of the div to updated
  * @param function name place the callback function name here (without the tailing brackets)
  * @return void
  */
 function updateDiv(div, callback_function) {
 	try {
	 	
	 	
	 	var options = {
	 		url : $(div + " form").attr('action'),
	 		success : 
	 		
	 		function(data) 
	 			{
	 				try 
	 				{
		 				$(div).html(data);
		 				if(typeof(callback_function) != 'undefined') 
		 				{
		 					// run the callback function
		 					callback_function();
		 				}	
		 			}catch(err) {
		 				alert("Error : "+ err.description);
		 			}	
	 			}
	 		
	 			
	 			
	 	};//end of options
	 	
	 	
	 	
	 	$(div + " form").ajaxSubmit(options);
	 }catch(err) {
	 	alert("Error in (updateDiv) " + err.description);
	}	
 }
 
 
 
 
 
// dialog function 
// this fucntion allows you to load any element into the UI jquery dialog
// @param string div the jquery style name of the div (example " #my div" or ".theDiv")
// @param string url the url to be loaded into the specified div
// @param string callback_function optional naem of the funtion to be called when the dialog closes
	 	function showDialog(div,url,callback_function) {
	 		try {
	 			// set the default options
	 			var options = {
	 				autoOpen : false, 	// do not open by itself, it would cause to be oopened only once per page
	 				hide:'slide', 		// slide out
	 				open:'slide',		// slide in
	 				position:'middle',		// show at the top of the page
	 				modal : true, 		// make it modal - no other things while the form is up there
	 				width:420 	,		// make it 500px wide\
	 				title : "New :  Online CDL TESTS !"
	 				
	 				
	 			};
	 			// add the callback function
	 			if(typeof(callback_function) !== undefined) {
	 				options.close = callback_function;
	 			}
	 			
	 			loadPage(url,div);
	 			
	 			// set the default options
	 			$(div).dialog(options);
	 			// open the window
	 			$(div).dialog('open');
	 			
	 		}catch(err) {
	 			alert("Error occurred (showDialog Function) " + err.description);
	 		}
	 	}
	 	
// this funtion closes the dialog on provided div
// $param string div name of the div on which the dialog should be closed	 	
function closeDialog(div) {
	try {
		if($(div).dialog('isOpen')) {
			$(div).dialog('close');
		}
	}catch(err) {
		alert("Error in fillPaymentInfo : " + err);
	}
}	
 
 
/**
  * returns html code that is displays while ajax content is loaded
  * it shows ":Loading" and the  spinning gif
  * Note : make sure the image exists 
  * @return string $html the html code 
  */
 function getLoadingHtmlString() {
	 var html = "";
	 try {
		 html += '<div align="center" class="centered">';
		 html += 'Loading ....<br /><br />';
		 html +='<img src="/img/loading.gif"';
		 html += '<br /><br />';
		 html += 'please wait ...';
		 html +='</div>';
	 }catch(err) {
		alert("Error in getLoadingHtmlString function : " + err);
	}
	 return html;
 } 
 
 
 
  /**
  * jquery preload images functions
  *  taken from http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
  *  Usage : $.preloadImages("image1.gif", "/path/to/image2.png",
  *	"some/image3.jpg");
  */
 jQuery.preloadImages = function()
{
	try {
	  for(var i = 0; i<arguments.length; i++)
	  {
	    jQuery("<img>").attr("src", arguments[i]);
	  }
  
  }catch(err) {
		alert("Error in preloadImages : " + err);
	}
}
 