// wait for the DOM to be loaded 
$(document).ready(function() { 
	// bind 'myForm' and provide a simple callback function 
	
	var box = $("#statusBox");
	box.fadeOut("fast");
	
	// prepare Options Object 
	var options = { 
    	//target:     '#statusBox', 
		dataType: 'json',    	
		//url:        'reg2.php', 
    	//beforeSubmit:  showRequest,
    	success: processJson
	}; 
	
	$('#myForm').ajaxForm(options);
	
	$.fn.clearForm = function() {
		return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
 		if (tag == 'form')
   			return $(':input',this).clearForm();
 		if (type == 'text' || type == 'password' || tag == 'textarea')
   			this.value = '';
 		else if (type == 'checkbox' || type == 'radio')
   			this.checked = false;
 		else if (tag == 'select')
   			this.selectedIndex = -1;
  });
	};
}); 

function processJson(data) { 
    // 'data' is the json object returned from the server 

var box = $("#statusBox");
	box.fadeOut("fast");
	if(data.status == "error"){
		box.addClass("error");
		box.html(data.message);
	}else if(data.status == "success"){
		box.addClass("success");
		box.html(data.message);
		$('#myForm').clearForm();
	}
	box.fadeIn("slow");
//alert(data.message); 
//	 alert(data.status); 
}

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
var box = $("#statusBox");   
box.fadeOut("fast");
	box.addClass("notice");
	box.html("Παρακαλώ περιμένετε...");
	box.fadeIn("fast");
 	
    // jqForm is a jQuery object encapsulating the form element. To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
	
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 	var box = $("#statusBox");
 	box.fadeIn("slow");
	//box.addClass("error");
	//box.addClass("success");
	
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
} 

function windowOpen(url, windowName, width, height){
	var w = width;
	var h = height;
	window.open(url, windowName, 'location = no, width='+w+', height='+h+', status=no, scrollbars=no resizable=no links=on top=10 left=10')
}