function convertToUSD() {
	var a = $("#amt").val();
	if(a == "") a = 0;
	else a = parseFloat($("#amt").val());
	a = a * 3.3;
	a = a.toFixed(2);
	$("#amt_usd").val(a);
	$("#paypalTotalAmount").html(a.toString());
}
function showWarning(id) {
	if(id == "amt") {
		$("#"+id).attr("class","amountInput amountInputHL");
		setTimeout(function() { $("#"+id).attr("class","amountInput"); },3000);
		$("html, body").animate({ scrollTop: $("#amountContainer").offset().top }, 1000);
		setTimeout(function() { $("#"+id).focus(); },1000);
	}
}
function formDataReady() {
	var a = $("#amt").val();	
	if(a.length < 1) { showWarning("amt"); return false; }
	else {
		convertToUSD();
		return true;
	}				
}
function cleanAmount() {
	var a = $("#amt").val();
	
	if(a.length < 10) {
	
	} else {
		a = a.slice(0,-1);
		$("#amt").val(parseFloat(a));
	}	
	a = a.replace(".","");
	a = parseInt(a);
	a = a.toString();
	if(a == "0") $("#amt").val("");
	else {
		
		var a2 = "";
		for (var i=0; i<a.length; i++) {
			if(a.charAt(i) == "0" || a.charAt(i) == "1" || a.charAt(i) == "2" || a.charAt(i) == "3" || a.charAt(i) == "4" || a.charAt(i) == "5" || a.charAt(i) == "6" || a.charAt(i) == "7" || a.charAt(i) == "8" || a.charAt(i) == "9") {
				a2 += a.charAt(i);
			} 
		}
		if(a2.length == 0) { 
			$("#amt").val("");
		} else { 
			if(a2.length == 1) a2 = "0.00" + a2;
			else if(a2.length == 2) a2 = "0.0" + a2;
			else if(a2.length == 3) a2 = "0." + a2;
			else if(a2.length > 3) {
				a2 = a2.substring(0,(a2.length-3)) + "." + a2.substring((a2.length-3));
			}
			$("#amt").val(parseFloat(a2).toFixed(3));
		}
	}
	convertToUSD();
}
function hideAlert() {
	$("#alertContainer").hide();
	$("#backgroundBlocker").fadeOut('fast');
	$("#alertMessage").html("");
}
function showAlert(m) {
	$("#alertMessage").html(m);
	$("#backgroundBlocker").fadeIn('fast');
	$("#alertContainer").fadeIn('fast');
}
function initiateProcessing() {
	$("#backgroundBlocker").fadeIn('fast');
}
function showProcessing() {
	$("#processingMainElement").hide();
	$("#processingMainElement").fadeIn('fast');
}
function Pay(pm) {
	$("#payButton_"+pm).attr("onclick","return false;");
	var pform = $('#paymentForm');
	var disabled = pform.find(':input:disabled').removeAttr('disabled');
	var data = pform.serialize();
	disabled.attr('disabled','disabled');
	initiateProcessing();
    $.ajax({
        type: "POST",
        cache: false,
        url: "includes/processors/pay.php?f=pay&g="+pm+"&r="+Math.random(),
        data: data,
        success: function(json,status,xhr){
	        if(xhr.status == 200) {
		        if(json["status"] == "success") {
			        showProcessing();
			        window.location = json["paymenturi"];
			    } else {
				    showAlert(json["description"])					
					$("#payButton_"+pm).attr("onclick","Pay('"+pm+"');");
				}
		        
		        
	        }
        }
    });
}