﻿$(document).ready(function() {
  $.ajaxSetup({ cache: false });
});

function validateCode()
{
    $('#codeStatus').slideUp('fast');
    if($.trim($('#gpID').val()).length > 0)
    {
        $('#bGetDeal').attr('disabled', true);
        $('#bGetDeal').val('Validating');

	    $.ajax( {url:'/Groupon/AJAX/ajax_functions.asp', 
				    data: "t=1&c=" + $('#gpID').val(),
				    type: "post",
				    dataType: "text",
				    
				    success: 	function(rsp) {
				                    switch(rsp)
				                    {
				                        case "OK":
				                            location.assign("product.asp");
				                            break;
				                        case "INVALID":
				                            $('#codeStatus').html('Invalid code entered, please try again.<br/>' +
				                                                    'Please make sure you enter the entire 20 character code.');
				                            break;
                                    }				                            
							    },
				    error:		function (rq, ts, et) {
				                    $('#codeStatus').html('An error occurred looking up your code, please try again.');
 							    },
				    complete: 	function (rq, ts) {
				                    $('#codeStatus').slideDown();
                                    $('#bGetDeal').attr('disabled', false);
                                    $('#bGetDeal').val('Get my deal');
							    }
		    });
	}
	else {
	    $('#codeStatus').html('Invalid code entered, please try again.');
	    $('#codeStatus').slideDown();
	}
}

function redeem()
{
    $('#bRedeem').attr('disabled', true);
    $('#bRedeem').val('Please wait');
    
    $.ajax( {url:'/Groupon/AJAX/ajax_functions.asp', 
			    data: "t=2",
			    type: "post",
			    dataType: "text",
			    
			    success: 	function(rsp) {
			                    switch(rsp)
			                    {
			                        case "OK":
			                            location.assign("checkout.asp");
			                            break;
			                        case "TIMEOUT":
			                            location.assign("/Groupon");
			                            break;
                                }				                            
						    },
			    error:		function (rq, ts, et) {
			                    location.assign("/Groupon");
						    }
			
	    });
}
function calcTax()
{
        $.ajax( {url:'/Groupon/AJAX/ajax_functions.asp', 
			    data: "t=3&z=" + $('#cZip').val(),
			    type: "post",
			    dataType: "xml",
			    
			    success: 	function(xml) {
			                    if($(xml).find('status').text() == "OK") {
                                    $('#csTax').html($(xml).find('tax').text());
                                    $('#csTot').html('<strong>' + $(xml).find('total').text() + '</strong>');
                                    $('#agreeLinks').slideDown('fast');
                                }
                                else location.assign("/Groupon");
						    },
			    error:		function (rq, ts, et) {
			                    location.assign("/Groupon");
						    }
	    });
}

function enableZip()
{
    if($('#cZip').val().length == 5)
        $('#bCalcZip').attr('disabled', false);
    else {
        $('#bCalcZip').attr('disabled', true);
        $('#agreeLinks').hide();
    }
}

function ckChange()
{
    if($('#ckTC').is(':checked') &&  $('#ckRP').is(':checked')) 
        $('#bProceed').attr('disabled', false);
    else
        $('#bProceed').attr('disabled', true);
}
function copyBilltoShip()
{
    $('#shipName').val($('#billName').val());
    $('#shipPh').val($('#billPh').val());
    $('#shipAdd1').val($('#billAdd1').val());
    $('#shipAdd2').val($('#billAdd2').val());
    $('#shipCi').val($('#billCi').val());
    $('#shipSt').val($('#billSt').val());
    $('#shipZp').val($('#billZp').val());
}

function verifyCustomer()
{
    $('#bCont').attr('disabled', true);
    if ($('#err').is(':visible'))
        $('#err').hide();
    $('label').removeClass('errLbl');
    $.ajax( {url:'/Groupon/AJAX/ajax_functions.asp', 
		    data:   {
		                t: "4",
		                bn: $('#billName').val(),
		                bp: $('#billPh').val(),
		                be: $('#billEm').val(),
		                ba1: $('#billAdd1').val(),
		                ba2: $('#billAdd2').val(),
		                bc: $('#billCi').val(),
		                bs: $('#billSt').val(),
		                bz: $('#billZp').val(),
		                sn: $('#shipName').val(),
		                sp: $('#shipPh').val(),
		                sa1: $('#shipAdd1').val(),
		                sa2: $('#shipAdd2').val(),
		                sc: $('#shipCi').val(),
		                ss: $('#shipSt').val(),
		                sz: $('#shipZp').val(),
		                em: $('#emMkt').is(':checked')
		            },
		    type: "post",
		    dataType: "script",
		    
		    success: 	function(txt) {
		                            $('#bCont').attr('disabled', false);

					    },
		    error:		function (rq, ts, et) {
		                    location.assign("/Groupon");
					    }
    });
}

function verifyOrder()
{
    $('#bCont').attr('disabled', true);
    if ($('#err').is(':visible'))
        $('#err').hide();
    $('label').removeClass('errLbl');
    $.ajax( {url:'/Groupon/AJAX/ajax_functions.asp', 
	    data:   {
	                t: "5",
	                ct: $('#cct').val(),
	                cn: $('#ccn').val(),
	                cx: $('#ccx').val(),
	                cid: $('#ccid').val()
	            },
	    type: "post",
	    dataType: "script",
	    
	    success: 	function(txt) {
	                            $('#bCont').attr('disabled', false);

				    },
	    error:		function (rq, ts, et) {
	                    location.assign("/Groupon");
				    }
    });
}

function completeOrder()
{
    $.colorbox({inline:true, href:'#pleaseWait', overlayClose:false, escKey:false, onLoad: function() {
		        $('#cboxClose').remove();

	
                $.ajax( {url:'/Groupon/AJAX/ajax_functions.asp', 
	                data:   {
	                            t: "6"
	                        },
	                type: "post",
	                dataType: "script",
            	    
	                success: 	function(txt) {
                                    $.colorbox.close();
				                },
	                error:		function (rq, ts, et) {
	                                $.colorbox.close();
	                                alert("An error occurred");
	                                //location.assign("/Groupon");
				                }
                });
    	}});  
}

function oHistory()
{
   $.ajax( {url:'/Groupon/AJAX/ajax_functions.asp', 
        data:   {
                    t: "7",
                    o: $('#oNum').val(),
                    z: $('#sZip').val()
                },
        type: "post",
        dataType: "text",
	    
        success: 	function(txt) {
                    $('#orderStat').html(txt);

	                },
        error:		function (rq, ts, et) {
                        location.assign("/Groupon");
	                }
    });
}
