function showResult(o) {
	$('getPrice').disabled = false;
	$("resultPane").innerHTML = o.responseText;
	UNI.price.loadingDialog.hide();
	UNI.price.quoteDialog.show();
};

var callback = 
{
	success: showResult,
	failure: showResult
}

function sendRequest() {
	if( validateForm() ) {
        $('quoteFrm').submit();
	}
}

function validateForm() {
	    
    var errors = '';
    // Filter for checking valid email address
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    var radio = document.getElementsByName('isCust');
    var val = '';
    for (var r = 0; r < radio.length; r++) {
        if (radio[r].checked) {
            val = radio[r].value;
        }
    }
    if (val == '') {
        alert('You must select whether or not you are a Unishippers customer');
        return false;
    } else {
    
    if( $("fromZip").value == null || $("fromZip").value.length < 1 ) {
        errors += '- You must enter a value for \'Sender Zip\'\n';
    }
    else if(  $("fromZip").value.length < 5 || $("fromZip").value.length > 5 ) {
        errors += '- \'Sender Zip\' needs to be a 5-digit zip code\n';      
    }
    if( $("toZip").value == null || $("toZip").value.length < 1 ) {
        errors += '- You must enter a value for \'Delivery Zip\'\n';
    }
    else if(  $("toZip").value.length < 5 || $("toZip").value.length > 5 ) {
        errors += '- \'Delivery Zip\' needs to be a 5-digit zip code\n';        
    }
    if( $("weight").value == null || $("weight").value.length < 1 ) {
        errors += '- You must enter a value for \'Weight\'\n';
    }
    if( $("shipmentRecurrence").value == null || $("shipmentRecurrence").value == 0 ) {
        errors += '- You must enter a value for \'How often do you ship\'\n';
    }
    if( $("contactName").value == null || $("contactName").value.length < 1 ) {
        errors += '- You must enter a value for \'Contact Name\'\n';
    }
    if( $("companyName").value == null || $("companyName").value.length < 1 ) {
        errors += '- You must enter a value for \'Company Name\'\n';
    }
    if( $("contactPhone").value == null || $("contactPhone").value.length < 1 ) {
        errors += '- You must enter a value for \'Phone\'\n';
    }
    if( $("contactEmail").value == null || $("contactEmail").value.length < 1 ) {
        errors += '- You must enter a value for \'Email\'\n';
    }
    else if( ! filter.test($("contactEmail").value) ) {
        errors += '- You must enter a valid email address\n';       
    }
    
    if( errors.length > 0 ) {
        alert('These errors must be corrected:\n\n'+errors);
        return false;
    }
	}
	return true;
}

UNI.price = {};

UNI.onQuoteResultReady = function() {
	var handleCancel = function() {
		this.cancel();
	};
	
	UNI.price.quoteDialog = new YAHOO.widget.Dialog("quoteResult", 
			{ width : "260px",
			  fixedcenter : true,
			  visible : false, 
			  constraintoviewport : true,
			  modal : true,
			  zIndex : 102,
			  postmethod: "form",
			  buttons : [ { text:"OK", handler:handleCancel } ]
			} 
	);
	
	UNI.price.quoteDialog.render();
	
	$("quoteResult").style.display = "block";
	
	UNI.price.loadingDialog = new YAHOO.widget.Dialog("uni-loading-dialog", { 
		width: "300px",
		fixedcenter : true,
		visible : false, 
		constraintoviewport : true,
		modal: true,
		zIndex: 10000,
		buttons : [ { text:"Cancel", handler:handleCancel } ]
 	});
	 
	UNI.price.loadingDialog.render();
	
	$("uni-loading-dialog").style.display = "block";
};

function isCustomer() {
    UNI.navigation.nav(document.location.href);
}

function isNotCustomer() {
    
}

YAHOO.util.Event.addListener("getPrice", "click", sendRequest);
YAHOO.util.Event.addListener("isCust1", "click", isCustomer);
//YAHOO.util.Event.addListener("isCust2", "click", isNotCustomer);
YAHOO.util.Event.onContentReady("quoteResult", UNI.onQuoteResultReady);