
var $j = jQuery.noConflict();
var reg_ErrorTemplate = $j.template('<div class="error" id="error_${Id}">' +
                                            '<li>${Text}</li>' +
                                          '</div>');
$j(document).ready(function() {

	 $j('#frmReg').submit(function() { 
       var path = '/api/Registration.svc/UsrFormReg?apikey=0566e181-5b76-44e7-a41d-e556635d5dee'; 
       if ($j.query.toString().length > 0) { path = path + $j.query.toString().replace('?','&') + "&urlCode=-1000"; } 
	   else{path = path + "&urlCode=-1000"}
       $j(this).ajaxSubmit({ url: path, dataType: "json", success: reg_showResponse }); 
       return false; 
     }); 


    $j('.error').hide();
    $j('#errorWrapper').hide();
    $j('#dialog').jqm();

});

  
function reg_showResponse(responseText, statusText) {
    if (responseText.PageRedirect != null && responseText.Success == 1)
        window.location.href = responseText.PageRedirect;

    $j('#errorWrapper').hide();
    $j('.error').hide();    
    $j.each(responseText.Messages, function(i, msg) {
        $j('#errorWrapper').show();
        //See if error has already been added
        var err = $j('#error_' + msg.Id);
        if (err.length == 0) { $j('#errorItems').append(reg_ErrorTemplate, msg); }
        else err.show();
    });
}
    
function gotoBuyBook() {
    var url = $j('#BuyBook').val();
    if (url != null)
        window.open(url);
}
