﻿var isGift = false;

function showCountryDialog() {
    //document.getElementById("country_select_fog").style.display = "inline";
    //document.getElementById("country_select_dialog_container").style.display = "inline";
    //document.getElementById("wait").innerHTML = "";
}
    
function hideCountryDialog() {
    document.getElementById("country_select_fog").style.display = "none";
    document.getElementById("country_select_dialog_container").style.display = "none";
}

function processLocale(locale) {
    document.getElementById("wait").innerHTML = "Please wait...";
    if (currentLocale != locale) {            
        window.location = "setlocale.aspx?localesetting=" + locale + "&returnurl=" + scrimReturnURL;
    }
    else {
        disableCountryPrompt()
        hideCountryDialog();
    }
}

function disableCountryPrompt() {
    var xmlHttp;
    try {  // Firefox, Opera 8.0+, Safari 
        xmlHttp = new XMLHttpRequest(); 
    }
    catch (e) {  // Internet Explorer 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;   
            }
        } 
    }
    
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) { 

        }
    }
 
    xmlHttp.open("get", "disablecountryprompt.aspx", true);       
    xmlHttp.send(null); 
}

var donationIsCOForIC = false;

function validateDonationAmount(donationForPremium) {
    if (!donationForPremium) {
        if (!donationAmountIsValid()) {
            alert("The donation amount entered is not a valid number. Please check your entry and try again.");
            return;
        }
        else if (document.getElementById("donation_amount").value == "0") {
            alert("Your donation amount cannot be 0. Please check your entry and try again.");
            return;
        }
        else {
            document.form1.submit();
        }
    }        

    var userNeedsToConfirmAutoPay = false;
    
    if (donationIsCOForIC) {
        if (!document.getElementById("cofic_confirm").checked)
            userNeedsToConfirmAutoPay = true;
    }
    
    if (userNeedsToConfirmAutoPay) {
        document.getElementById("cofic_confirm_autopay_area").style.border = "2px solid red";
        alert("Please check the box confirming that you are entering into a monthly Auto-Pay program.");
    }
    else {
        if (!donationAmountIsValid())
            alert("The donation amount entered is not a valid number. Please check your entry and try again.");
        else if (document.getElementById("donation_amount").value == "0") {
            alert("Your donation amount cannot be 0. Please check your entry and try again.");
        }            
        else if (premium == "cof" && document.getElementById("donation_amount").value < 25.00) {
            alert("A minimum of $25.00 is required to join circle of friends.");
        }
        else if (premium == "ic" && document.getElementById("donation_amount").value < 50.00) {
            alert("A minimum of $50.00 is required to join inner circle.");
        }
        else if (premium == "pc" && document.getElementById("donation_amount").value < 125.00) {
            alert("A minimum of $125.00 is required to join president's circle.");
        }
        else if ((document.getElementById("donation_amount").value < minimumDonationAmount && document.getElementById("yes_send_resource").checked && document.getElementById("paymentplan").value == "0") || (document.getElementById("paymentplan").value == "1" && document.getElementById("donation_amount").value < minimumDonationAmountPaymentPlan && document.getElementById("yes_send_resource").checked)) {
            alert("Your donation amount is less than the minimum amount required for this resource item.");
	    }   									   
        else {
            if (!document.getElementById("yes_send_resource").checked && !document.getElementById("no_send_resource").checked) {
                document.getElementById("resource_selector").style.border = "2px solid red";
                alert("Please indicate whether you would like to receive this resource with your donation.");
                return;
            }	 
	        document.form1.submit();
	    }
	}
}

var matrixDonationAmount = "";
var matrixCount = 0;
var isMatrix = false;
var isCOF = false;
var donationAmount = 0;

function donationAmountIsValid() {

    
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;
    var sText;

    if (matrixDonationAmount != "") {
        sText = matrixDonationAmount;
    }
    else {
        sText = document.getElementById("donation_amount").value;
    }

    sText = sText.replace("$", "");

    for (i = 0; i < sText.length && IsNumber == true; i++) { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
} 

function setMatrixDonationAmount(val) {
    matrixDonationAmount = val;
    $("#donation_amount").val("Other");
    $("#matrix_amount").val(val);
}

function donationTextBoxClicked() {
    if (matrixCount != 0) {
        for (var i = 0; i < matrixCount; i++) {
            $("#matrix" + i).attr("checked", "")
        }
    }
    matrixDonationAmount = "";
    $("#matrix_amount").val("");  
}

function checkNumberOfItems() {
    if (donationAmount > 0) {
        document.getElementById("num_of_items").innerHTML = (document.getElementById("num_of_items").innerHTML*1) - 1;
        if ((document.getElementById("num_of_items").innerHTML*1) < 0)
            document.getElementById("num_of_items").innerHTML = "0";
    }
}
function disableEnterForDonationAmount(e)
{
     var key;

     if(window.event)
        key = window.event.keyCode;
     else
        key = e.which;

     if(key == 13) {
        checkDonationAmount();
        return false;
     }
     else
        return true;
}
