var LINK = "https://secure.shareit.com/shareit/checkout.html?";
var MSG_SUPPORT_UNMATCHED = "The license and support do not match. Change the support according to the license?";
var MSG_SUPPORT_NOT_SELECTED = "Are you sure you want to proceed without Premium Support?";
var MSG_LICENSE = "Premium Support has to accompany a license, please select one.";

var supSqlyogUnchecked = false;
var supMonyogUnchecked = false;
var supComboUnchecked = false;

function attachHandlers() {
    
    if ($("#discontValue").val() == 'monty') {
        $("#discountBanner").show();  
    } 
    
    $("#buyMonyog").click(function() {
       shopIt('panel_monyog_buy');
    });

    $("#buySqlyog").click(function() {
       shopIt('panel_sqlyog_buy');
    });

    $("#buyCombo").click(function() {
       shopIt('panel_monyogsqlyog_buy');
    });

    $("#supportSQLyogChkBox").click(function () {
        if ($(this).is(":checked") == false) {
            supSqlyogUnchecked = true;
        }
    });

    $("#supportMONyogChkBox").click(function () {
        if ($(this).is(":checked") == false) {
            supMonyogUnchecked = true;
        }
    });

    $("#supportMONyogSQLyogChkBox").click(function () {
        if ($(this).is(":checked") == false) {
            supComboUnchecked = true;
        }
    });

    $("#panel_sqlyog_buy input[name='chkBoxProduct']").click(function () {
        var supportChkBox = $("#supportSQLyogChkBox");
        if (supSqlyogUnchecked == false) {
            supportChkBox.attr('checked', true);
        }

        selectSupport("SQLyog");
    });

    $("#panel_monyog_buy input[name='chkBoxProduct']").click(function () {

        var supportChkBox = $("#supportMONyogChkBox");
        if (supMonyogUnchecked == false) {
            supportChkBox.attr('checked', true);
        }

        selectSupport("MONyog");
    });

    $("#panel_monyogsqlyog_buy input[name='chkBoxProduct']").click(function () {
        var supportChkBox = $("#supportMONyogSQLyogChkBox");

        if (supComboUnchecked == false) {
            supportChkBox.attr('checked', true);
        }
    });

}

function selectSupport(productType) {
    var productName = productType.toLowerCase();
    var products = getProducts(productName);
    var support = $("#support"+productType);
    for (var i = products.length-1; i >= 0; i--) {
        var product = $("#" + productName + products[i]);
        if (product.is(":checked")) {
            support.val(product.attr('class'));
            break;
        }
    }    
}

function getProducts(productName) {
    var products = new Array();
    products[0] = "SUP";
    if (productName == "monyog") {
        products[products.length] = "2UP";
        products[products.length] = "10UP";
    }

    products[products.length] = "5UP";
    products[products.length] = "UUP";
    return products;
}

function errorDialog() {
   $('#dialogerror').show();
   $('#dialogerror').dialog('open');
   $("#dialogerror").dialog({
			bgiframe: true,
			resizable: false,
			modal: true,
			width: 300,
			height: 160,
			buttons: {
				OK: function() {
					$(this).dialog('close');
				}
			}
		});
}
function dialogProductSupportMismatch(divId, productType) {
   $('#dialogproductsupport').show();
   $('#dialogproductsupport').dialog('open');
   $("#dialogproductsupport").dialog({
			bgiframe: true,
			resizable: false,
			modal: true,
			width: 450,
      height: 180,			
			buttons: {
				OK: function() {
					$(this).dialog('close');
					selectSupport(productType);           
          redirectTo(divId);
				},
				Cancel: function() {
					$(this).dialog('close');
					return false;
				}
			}
		});
}

function withoutSupportDialog(divId, productType) {
   $('#dialogwithoutpremium').show();
   $('#dialogwithoutpremium').dialog('open');
   $("#dialogwithoutpremium").dialog({
			bgiframe: true,
			resizable: false,
			modal: true,
			width: 500,
			open: function(event, ui) {
                $("button:contains('Include Premium Support')").focus();
            },
			buttons: {				
				"Include Premium Support": function() {
					$(this).dialog('close');
					if(productType == 'SQLyog'){
					  if ($("#panel_sqlyog_buy input[name='chkBoxProduct']").is(":checked") == true) { 
					       var supportChkBox = $("#supportSQLyogChkBox");  					       
                 supportChkBox.attr('checked', true);                 
            }					   
          }
          else if(productType == 'MONyog') {
            if ($("#panel_monyog_buy input[name='chkBoxProduct']").is(":checked") == true) { 
  					       var supportChkBox = $("#supportMONyogChkBox");  					       
                   supportChkBox.attr('checked', true);                 
              }					   
          } else {
              if ($("#panel_monyogsqlyog_buy input[name='chkBoxProduct']").is(":checked") == true) { 
  					       var supportChkBox = $("#supportMONyogSQLyogChkBox");  					       
                   supportChkBox.attr('checked', true);                 
              }
          }
					selectSupport(productType);                    
          redirectTo(divId);
				},
				"No Thanks": function() {
					$(this).dialog('close');
					selectSupport(productType);           
          redirectTo(divId);
				}
			}
		});
} 


function supportErrorDialog() {
   $('#dialogsupport').show();
   $('#dialogsupport').dialog('open');
   $("#dialogsupport").dialog({
			bgiframe: true,
			resizable: false,
			modal: true,
			width: 400,
			height: 180,
			buttons: {
				OK: function() {
					$(this).dialog('close');
				}
			}
		});
}


function checkSupport(divId, productType) {
    var ctrl = false;
    var supportSelected = false;
    var productName = productType.toLowerCase();
    var products = getProducts(productName);
    var supportChkBox = $("#support" + productType + "ChkBox");
    if (supportChkBox.is(":checked")) {
        supportSelected = true;
    }

    for (var i = 0; i < products.length; i++) {
        var product = $("#" + productName + products[i]);
        if (product.is(":checked")) {
            ctrl = true;
            if (supportSelected) {
                supportValue = product.attr('class');
            }
        }
    }
    
    if (ctrl == false) {
        if (supportSelected == false) {
          errorDialog();         
        } else {
            supportErrorDialog(); 
        }
        
        $("#"+ productName + "UUP").focus();
        return false;
    }  

    if (supportSelected) {
        var supportProduct = $("#support" + productType);
        if (supportValue != supportProduct.val()) {
            supportProduct.focus();            
                ctrl = false;
            dialogProductSupportMismatch(divId, productType);          
                      
        } else {
            ctrl = true;
        }
    } else {
        ctrl = false;
        withoutSupportDialog(divId, productType);        
    }
    return ctrl;
}

function validate(divId) {

    if (divId == "panel_sqlyog_buy") {
        return checkSupport(divId, "SQLyog");
    } else if (divId == "panel_monyog_buy") { 
        return checkSupport(divId, "MONyog");
    } else if (divId == "panel_monyogsqlyog_buy") {
        var productUUP = $("#monyogsqlyogUUP");
        var supportChkBox = $("#supportMONyogSQLyogChkBox");
        var supportSelected = supportChkBox.is(":checked");
        if (productUUP.is(":checked") == false) {
            if (supportSelected == false) {
                 errorDialog();
            } else {
                supportErrorDialog();
            }
            productUUP.focus();
            return false;
        }

        if (supportSelected == false) {
            withoutSupportDialog(divId, "MONyogSQLyog"); 
            return false;
        }
    }

    return true;
}

function shopIt(divId) {
    if (validate(divId) == false) {
        return ;
    }
    redirectTo(divId);    
}

function redirectTo(divId) {
    var params = "";
    $("#"+divId+" input[name='chkBoxProduct']").each(function () {
        if ($(this).is(":checked")) {
            var selectId = $(this).val();
            params += "PRODUCT["+$("#"+selectId).val()+"]=1&";
        }
    });

    if ($("#discontValue").val() == 'monty') {    
        var discount = 'COUPON1=mariadb2411';
        params +=discount;
    } else {
        var discount = '';
        params = params.substring(0, params.length -1);
    }
    
    document.location = LINK + params;
}
