
function processXml(responseXML) {
    // 'responseXML' is the XML document returned by the server; we use 
    // jQuery to extract the content of the message node from the XML doc 
    var message = $('message', responseXML).text();
	var status = $('status', responseXML).text();

	if(status == 'success' && message != ''){
		window.location = message;	
	}	
	else if(status == 'error'){
		$('#msg').removeClass("msg");
		$('#msg').addClass("msg_error");
		$('#msg').attr({innerHTML:message});
		$('#msg_container').attr({style:"width: 90%; background-color: #ffffff; margin: 10px auto;"});
		$('#msg').corner("round 8px");
	}
}

function get_screen_size(){
	var agent = navigator.userAgent.toLowerCase();
   var is_ie = (agent.indexOf('msie') != -1);
   var is_mac = (agent.indexOf('macintosh') != -1);
   var pageWidth = is_ie ? window.document.body.clientWidth : window.innerWidth;
   var pageHeight = is_ie ? window.document.body.clientHeight : window.innerHeight;

   // fix 1-way scrollbars in Firefox
   if (!is_ie) {
       var scrollbarSize = is_mac ? 15 : 19;
       //if (pageWidth < minWidth) pageHeight -= scrollbarSize;
       //if (pageHeight < minHeight) pageWidth -= scrollbarSize;
   }
	return {h:pageHeight, w:pageWidth};
}

function showRequest(formData, jqForm, options){
	if(Spry){
		r = Spry.Widget.Form.validate(jqForm[0]);
		if(!r) return r;
	}
	
	var dim = get_screen_size();
	w = dim.w+'px';
    h = dim.h+'px';

	$('#msg_container').remove();
	jqForm.createPrepend(
	    'div', {className:"msg", id:"msg", innerHTML:"Please Wait..."}
	);

	$('#msg').insertBefore(jqForm);
	$('#msg').wrap('<div id="msg_container" style="width: 90%; background-color: #c0c2d6; margin: 5px auto;" align="center"></div>');
	$('#msg').corner("round 8px");

	jqForm.createPrepend(
	    'div', {className:"top_div", id:"block_div", style:"height: "+h+"; width: "+w}
	);
	$('#block_div').insertBefore("#wrap");
}

function AF(frmID){
	// prepare the form when the DOM is ready
	$(document).ready(function() { 
		// bind form using ajaxForm 
		$(frmID).ajaxForm({ 
			// dataType identifies the expected content type of the server response 
			beforeSubmit:  showRequest, 			
			dataType:  'xml', 
			async: true, 
			cache: false, 
		//	timeout: 30000,
			error: function(req, stat, err){
							$('#msg_container').attr({style:"width: 90%; background-color: #ffffff; margin: 10px auto;"});
							$('#msg').removeClass("msg")
									 .addClass("msg_error")
							         .attr({innerHTML:"Request Failed...  ("+err+")"})
									 .corner("round 8px");

					},
			complete: function(){ 
							$('#block_div').remove();
						},
			// success identifies the function to invoke when the server response 
			// has been received 
			success:   processXml 
		}); 
	});
}