﻿/* GENERAL FUNCTIONS */
function expandcontent(cid) {
    document.getElementById(cid).style.display = (document.getElementById(cid).style.display != "") ? "" : "none";
    return false;
}

function CloseModal(controlID) {
    if ($find(controlID) != null) {
        $find(controlID).hide();
        return false;
    }
}
function OpenModal(controlID) {
    if ($find(controlID) != null) {
        $find(controlID).show();
        return false;
    }
}
function openRadWindow(URL, title, width, height) {
    if (URL != "") {
        var oWnd = radopen(URL, "RadWindow1");
        var w = (screen.width * .70);
        if (width != null && width != "") {
            w = width;
        }
        var h = (screen.height * .70);
        if (height != null && height != "") {
            h = height;
        }
        oWnd.setSize(w, h);
        if (title == "") {
            title = "Expo Tracker Lead Retrieval Order";
        }
        oWnd.set_title(title);
        oWnd.Center();
    }
}
function closeRadWindow() {
    var oManager = GetRadWindowManager();
    var oActive = oManager.getActiveWindow();
    oActive.closeActiveWindow();
}

function showPopup(url, title, width, height) {
    var w = width;
    var h = height;
    if (w == 0) {
        w = (screen.width * .80);
    }
    if (h == 0) {
        h = (screen.height * .80);
    }
    var baseURL = document.location.href;
    var pathURL = document.location.pathname;
    var index1 = baseURL.indexOf(pathURL);
    var newURL = "";
    /*if (baseURL.toLowerCase().indexOf("/admin") != -1) {
        newURL = baseURL.substr(0, index1) + "/admin/";
    } */   
    if (url != null) {
        winl = (screen.width) ? (screen.width - w) / 2 : 100;
        wint = (screen.height) ? (screen.height - h) / 2 : 100;
        var winProps = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
        if (newURL != "") {
            url = newURL + url;
        }
        var newwin = window.open(url, title, winProps);
        newwin.focus();
        return false;
    }
}
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}
function formatNumber(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}
//check for numeric value in the input box
function checknum(field) {

    var FieldValue = $get(field).value;
	var valid = "0123456789.,"
	var ok = "yes";
	var temp;
	for (var i = 0; i < FieldValue.length; i++) {
	    temp = "" + FieldValue.substring(i, i + 1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Invalid entry!  Please enter numeric values only!");
		FieldValue = "";
		$get(field).focus();
		return false;		
   }
   else{
		return true;	
   }

}
function setDate(sender, args) {
    if (sender._selectedDate == "" || sender._selectedDate == null) {
        sender._selectedDate = new Date();
    }
}

function validateDate(thisField) {
    var ObjRegEx = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
    if (thisField.value != "") {
        if (ObjRegEx.test(thisField.value) == false) {
            alert("Invalid date entered, please enter mm/dd/yyyy");
            thisField.value = "";
            thisField.focus();
        }
    }
}
function showError() {
    $("#divForm").css("display", "none");
    $("#divError").css("display", "block");
    return false;
}
function hideError() {
    $("#divForm").css("display", "block");
    $("#divError").css("display", "none");
    return false;
}
function startUp() {
    if ($("[id$='hiddenID']").val() == "0") {
        document.getElementById("divEdit").style.display = "";
        document.getElementById("divView").style.display = "none";
        if ($("[id$='hiddenHasProducts']").length) {
            if ($("[id$='hiddenHasProducts']").val() == "0") {
                document.getElementById("divEditPricing").style.display = "none";
                document.getElementById("divViewPricing").style.display = "none";
            }
        }
    }
    else {
        document.getElementById("divEdit").style.display = "none";
        document.getElementById("divView").style.display = "";
        if ($("[id$='hiddenHasProducts']").length) {
            if ($("[id$='hiddenHasProducts']").val() == "0") {
                document.getElementById("divEditPricing").style.display = "";
                document.getElementById("divViewPricing").style.display = "none";
            }
            else {
                document.getElementById("divEditPricing").style.display = "none";
                document.getElementById("divViewPricing").style.display = "";
            }
        }
        if ($("[id$='hiddenHasFieldList']").val() == "1") {
            document.getElementById("divFieldList").style.display = "";
            document.getElementById("divEmails").style.display = "";
        }
    }
}
function showEdit(showDIV, hideDIV) {
    document.getElementById(showDIV).style.display = "";
    document.getElementById(hideDIV).style.display = "none";    
    return false;
}
/* END GENERAL FUNCTIONS */

/* EVENT EDIT FUNCTIONS*/
function validProduct(ddlID) {
    //make sure the product selected has not already been setup for this event
    var ProductID = $("[id='" + ddlID + "']").val();
    var FoundProduct = false;
    $("[id*='ddlProduct']").each(function() {
        if ($(this).attr("id") != ddlID) {
            //alert($(this).attr("id"));
            if ($(this).val() != undefined && $(this).val() != "") {
                if ($(this).val() == ProductID) {
                    alert("This product has already been setup for this event");
                    $(this).val("");                    
                }
            }
        }
    });
    /*
    $("[id='" + ddlID + "']").val("");
    $("[id='Skinned" + ddlID + "']").val("");
    $("[id='" + ddlID + "']").attr("selectedIndex", -1);
    $("[id='Skinned" + ddlID + "']").attr("selectedIndex", -1);
    */
}
/* END EVENT EDIT FUNCTIONS*/
/* Event Lead Setup */
function ShowFieldList() {
    //if ddlFields has values then show labels else show ddl
    if ($("[id$='hiddenhasData']").val() == "1") {
        $("[id*='ddlField']").each(function() {
            $(this).css("display", "none");
        });
        $("[id*='lblField']").each(function() {
            $(this).css("display", "");
        });
    }
    else {
        $("[id*='ddlField']").each(function() {
            $(this).css("display", "");
        });
        $("[id*='lblField']").each(function() {
            $(this).css("display", "none");
        });
    }
}

function showLeads() {
    var EventCode = $("[id$='ddlEvent']").val();
}
/* Event Lead Setup */

/* Event Leads Email page - see if the emails have already been sent or if there are any*/
function findLeads() {
    var EventCode = $("[name$='ddlEvent']").val();
    if (EventCode != null && EventCode != undefined) {
        LROS.DataAccess.ReturnEventLeads(EventCode, SucceededGetLeads);
        return false;
    }
}
function SucceededGetLeads(result, eventArgs) {
    if (result.length == 2) {
        if (result[1] == "true") {
            $("[id$='lblNumberAttendees']").html("The attendee emails for this event have already been sent.<br />Are you sure you want to send them again?");
        }
        else {
            $("[id$='lblNumberAttendees']").html("There are " + result[0] + " attendee emails to send for this event.");
        }
    }
}

/* CONTACT PAGE FUNCTIONS */
function findZip() {
    var zipCode = $("input[id$='txtZip']").val();
    if (zipCode != null) {
        LROS.DataAccess.ReturnCityState(zipCode, SucceededGetCityState);
        return false;
    }
}
function SucceededGetCityState(result, eventArgs) {
    if (result.length == 2) {
        $("input[id$='txtCity']").val(result[0]);
        $("[id$='ddlState']").val(result[1]);
        $("input[id$='txtPhone']").focus();
    }
}
function CheckEmail() {
    var Email = $("input[id$='txtEmail']").val();
    var CustomerID = $("input[id$='hiddenCustomerID']").val();
    if (Email != undefined && CustomerID != undefined) {
        LROS.DataAccess.ReturnValidEmail(Email, CustomerID, SucceededCheckEmail);
        return false;
    }
}
function SucceededCheckEmail(result, eventArgs) {
    if (result.length == 1) {
        if (result[0] != "OK") {
            alert("There is an existing account with the email address entered\nA new account cannot be created.  Please login to your existing account.");
            $("input[id$='txtEmail']").val("");
            $("#trEmailError").css("display", "");
        }
        else
            $("#trEmailError").css("display", "none");
    }
}
function ValidateState(src, args) {
    var Country = $("[name$='ddlCountry']").val();
    if (Country.toUpperCase() == "UNITED STATES") {
        args.IsValid = ($("[name$='ddlState']").val() != "" && $("[name$='ddlState']").val() != undefined);
    }
    else {
        args.IsValid = true;
    }
}
function ValidateProvince(src, args) {
    var Country = $("[name$='ddlCountry']").val();
    if (Country.toUpperCase() == "CANADA") {
        args.IsValid = ($("[name$='ddlProvince']").val() != "" && $("[name$='ddlProvince']").val() != undefined);
    }
    else {
        args.IsValid = true;
    }
}
function switchCountry() {
    var country = $("[name$='ddlCountry']").val();
    if (country.toUpperCase() == "UNITED STATES") {
        $("[id*='tr_u']").css("display", "");
        $("[id*='tr_i']").css("display", "none");
    }
    else {
        $("[id*='tr_u']").css("display", "none");
        $("[id*='tr_i']").css("display", "");
        if (country.toUpperCase() == "CANADA") {
            $("[id*='trProvince']").css("display", "");
            $("[id*='trIntlState']").css("display", "none");
        }
        else {
            $("[id*='trProvince']").css("display", "none");
            $("[id*='trIntlState']").css("display", "");
        }
    }
}
function findExhibitor() {
    var EID = $("[id$='ddlExhibitors']").val();
    if (EID != null) {
        LROS.DataAccess.ReturnExhibitor(EID, SucceededGetExhibitor);
        return false;
    }
}
function getExhibitor(EID) {
    if (EID != null) {
        LROS.DataAccess.ReturnExhibitor(EID, SucceededGetExhibitor);
        return false;
    }
}
function SucceededGetExhibitor(result, eventArgs) {    
    if (result.length > 2) {        
        $("input[id$='txtAddress']").val(result[0]);
        $("input[id$='txtAddress2']").val(result[1]);
        $("input[id$='txtCity']").val(result[2]);
        $("[id$='ddlState']").val(result[3]);
        $("input[id$='txtZip']").val(result[4]);
        $("[id$='ddlCountry']").val(result[5]);
        $("input[id$='txtPhone']").val(result[6]);
        $("input[id$='txtFax']").val(result[7]);
        if ($("input[id$='txtEmail']").val() == "") {
            $("input[id$='txtEmail']").val(result[8]);
        }
        if (location.href.toLowerCase().indexOf("contact.aspx", 0) != -1) {
            $("input[id$='txtCompanyBadge']").val(result[9]);
            //set Group Registration
            if ($("input[id$='rbGroupReg']") != null) {
                $('[id$=rbGroupReg]').find("input[value='1']").attr("checked", true);
            }
            //international fields
            $("input[id$='txtCity2']").val(result[2]);
            $("[id$='ddlProvince']").val(result[3]);
            $("input[id$='txtZip2']").val(result[4]);
            $("input[id$='txtPhone2']").val(result[6]);
            $("input[id$='txtFax2']").val(result[7]);

            switchCountry();

        }
        $("input[id$='txtCompany']").val(result[9]);
        $("input[id$='txtFirstName']").val(result[10]);
        $("input[id$='txtLastName']").val(result[11]);        
    }
}
/* END CUSTOMER PAGE FUNCTIONS */

/* ORDER FORM PAGE FUNCTIONS */
function ValidateOrder(src, args) {
    var iCount = 0;
    $("input[id*='txtQty']").each(function() {
        if ($(this).val() != undefined) {
            if (isNaN($(this).val()) == false && $(this).val() != "0" && $(this).val() != "") {
                iCount += 1;                
            }
        }
    });
    args.IsValid = (iCount > 0);
}

function CalcExtAmt(rowID) {
    if (rowID != null && rowID != "") {
        //onBlur of txtQty pass the PriceID (rowID) which is the part of every control for that row
        var Qty = 0;
        var Rate = 0;
        if (isNaN($("[id$='txtQty" + rowID + "']").val()) == false) {
            Qty = Number($("[id$='txtQty" + rowID + "']").val());
        }
        if (isNaN($("[id$='hiddenRate" + rowID + "']").val()) == false) {
            Rate = Number($("[id$='hiddenRate" + rowID + "']").val());
        }
        //set the lblAmt and hiddenAmt fields
        var ExtAmt = (Qty * Rate);
        $("[id$='hiddenAmt" + rowID + "']").val(formatNumber(ExtAmt));
        $("[id$='lblAmt" + rowID + "']").html(formatCurrency(ExtAmt));
        
        //retotal the order form
        setOrderTotal();
    }
}
function setOrderTotal() {
    var oTotal = 0;
    $("input[id*='hiddenAmt']").each(function() {
        if ($(this).val() != undefined) {
            if (isNaN($(this).val()) == false && $(this).val() != "0" && $(this).val() != "") {
                oTotal += Number($(this).val());
            }
        }
    });
    $("[id$='lblOrderTotal']").html(formatCurrency(oTotal));
}
//admin fees page
function ValidateDiscountNumber(src, args) {
    if ($("[id$='txtDiscountNumber']").val() != "") {
        if (isNaN($("[id$='txtDiscountNumber']").val())) {
            src.errormessage = "The value entered in the Discount Amount box is not a number";
            $("[id$='txtDiscountNumber']").val("");
            $("[id$='txtDiscountNumber']").focus();
            args.IsValid = false;
        }
        else {
            var num = Number($("[id$='txtDiscountNumber']").val());
            if (num < 0) {
                src.errormessage = "Please enter a positive numeric value in the Discount Amount box";
                $("[id$='txtDiscountNumber']").focus();
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }
    }
    else {
        args.IsValid = true;
    }
}
function ValidateComments(src, args) {
    var needsComments = false;
    if ($('input[id$=cBoxCompBadge]').is(':checked') || $('input[id$=cBoxCompReg]').is(':checked') || $("[id$='txtDiscountNumber']").val() != "") {
        needsComments = true;
    }
    if ($("[id$='hiddenCommentRequired']").val() == "false") {
        needsComments = false;
    }
    if (needsComments == true && $("[id$='txtComments']").val() == "") {
        src.errormessage = "You must enter comments when discounting a registration";
        $("[id$='txtComments']").focus();
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
}
/* END ORDER FORM PAGE FUNCTIONS */

/* Payment Page Functions */
function setPaymentDetails(cBox, BalanceAmt) {
    var currentTotal = 0;
    var iCount = 0;
    $("input[id*='cBoxPayment']").each(function() {
        if ($(this).attr("checked") == true) {
            $("input[id$='txtPaymentAmount']").attr("readonly", "readonly");
            iCount += 1;
        }
    });

    if (iCount == 0) {
        $("input[id$='hiddenPaymentAmount']").val($("input[id$='hiddenOriginalAmount']").val());
        $("input[id$='txtPaymentAmount']").val(formatCurrency($("input[id$='hiddenOriginalAmount']").val()));
        document.getElementById("spanAmount").style.display = "none";
    }
    if (iCount == 1) {
        if ($get(cBox).checked == true) {
            if (Number(BalanceAmt) != NaN) {
                $("input[id$='txtPaymentAmount']").val(formatCurrency(Number(BalanceAmt)));
                $("input[id$='hiddenPaymentAmount']").val(Number(BalanceAmt));
            }
        }
        else {
            if (Number($("input[id$='hiddenPaymentAmount']").val()) != NaN) {
                currentTotal = Number($("input[id$='hiddenPaymentAmount']").val());
            }
            currentTotal -= Number(BalanceAmt);
            $("input[id$='txtPaymentAmount']").val(formatCurrency(currentTotal));
            $("input[id$='hiddenPaymentAmount']").val(currentTotal);
        }
        document.getElementById("spanAmount").style.display = "inline";
    }
    if (iCount > 1) {
        if (Number($("input[id$='hiddenPaymentAmount']").val()) != NaN) {
            currentTotal = Number($("input[id$='hiddenPaymentAmount']").val());
        }
        if ($get(cBox).checked == true) {
            if (Number(BalanceAmt) != NaN) {
                currentTotal += Number(BalanceAmt);
                $("input[id$='txtPaymentAmount']").val(formatCurrency(currentTotal));
                $("input[id$='hiddenPaymentAmount']").val(currentTotal);
            }
        }
        else {
            currentTotal -= Number(BalanceAmt);
            $("input[id$='txtPaymentAmount']").val(formatCurrency(currentTotal));
            $("input[id$='hiddenPaymentAmount']").val(currentTotal);
        }
        document.getElementById("spanAmount").style.display = "inline";
    }
    if (Number($("input[id$='hiddenPaymentAmount']").val()) != NaN) {
        if (Number($("input[id$='hiddenPaymentAmount']").val()) < 0) {
            $("input[id$='hiddenPaymentAmount']").val($("input[id$='hiddenOriginalAmount']").val());
            $("input[id$='txtPaymentAmount']").val(formatCurrency($("input[id$='hiddenOriginalAmount']").val()));
        }
    }
}
function selectedAttendees() {
    var iCount = 0;
    $("input[id*='cBoxPayment']").each(function() {
        if ($(this).attr("checked") == true) {
            $("input[id$='txtPaymentAmount']").attr("readonly", "readonly");
            iCount += 1;
        }
    });
    return iCount;
}
function showInputs() {
    $("[id$='cardport']").val("0");
    showhidePayment();
}
function showhidePaymentPublic() {
    var PaymentType = $("[id$='rbType']").val();
    if (PaymentType == "Purchase Order") {
        document.getElementById("checkdiv").style.display = "block";
        document.getElementById("creditcarddiv").style.display = "none";
        document.getElementById("onsitediv").style.display = "none";
        document.getElementById("showcheckbutton").style.display = "inline";
        document.getElementById("showcreditcardbutton").style.display = "none";
        $("[id$='ValidationSummary1']").css("display", "none");        
        return;
    }
    else if (PaymentType == "Check") {
        document.getElementById("checkdiv").style.display = "block";
        document.getElementById("creditcarddiv").style.display = "none";
        document.getElementById("onsitediv").style.display = "none";
        document.getElementById("showcheckbutton").style.display = "inline";
        document.getElementById("showcreditcardbutton").style.display = "none";
        $("[id$='ValidationSummary1']").css("display", "none");  
        return;
    }
    //credit cards
    else {
        document.getElementById("creditcarddiv").style.display = "block";
        document.getElementById("checkdiv").style.display = "none";
        document.getElementById("onsitediv").style.display = "none";
        document.getElementById("showcheckbutton").style.display = "none";
        document.getElementById("showcreditcardbutton").style.display = "inline";
        return;
    }
    if ($("[id$='hiddenPaymentMethod']").val() == "1") {
        document.getElementById("trPaymentMethod").style.display = "";
    }
}
function showhidePayment() {
    var PaymentType = $("[name$='cc_type']").val();
    //alert(PaymentType);
    //alert($("[name$='cc_type']").attr("id"));
    if (PaymentType == "Check" || PaymentType == "Cash") {
        document.getElementById("checkdiv").style.display = "block";
        document.getElementById("showcheckbutton").style.display = "inline";
        document.getElementById("creditcarddiv").style.display = "none";
        document.getElementById("showcreditcardbutton").style.display = "none";        
        if ($("[id$='hiddenAdmin']").val() == "0") {
            document.getElementById("trCheckNumber").style.display = "none";
            document.getElementById("trCheckInfo").style.display = "";
            $("#showcheckbutton").css("padding-left", "60px");
        }
        else {
            document.getElementById("trCheckNumber").style.display = "";
            document.getElementById("trCheckInfo").style.display = "none";
            $("#showcheckbutton").css("padding-left", "0px");
        }
        if ($("[id$='PanelAdmin']").length) {
            document.getElementById("trDepositDate").style.display = "";
            document.getElementById("reqDate").style.display = "none";
        }
        return;
    }    
    else if (PaymentType == "Unapplied Transfer") {   
        document.getElementById("checkdiv").style.display = "none";
        document.getElementById("creditcarddiv").style.display = "none";
        document.getElementById("showcheckbutton").style.display = "none";
        document.getElementById("showcreditcardbutton").style.display = "none";
        if ($("[id$='PanelAdmin']").length) {
            document.getElementById("trDepositDate").style.display = "none";
            document.getElementById("reqDate").style.display = "none";
            showUnapplied();
        }
        return;
    }
    //credit cards
    else {
        document.getElementById("creditcarddiv").style.display = "block";
        document.getElementById("showcreditcardbutton").style.display = "inline";
        document.getElementById("checkdiv").style.display = "none";
        document.getElementById("showcheckbutton").style.display = "none";
        if ($("[id$='PanelAdmin']").length) {
            document.getElementById("trDepositDate").style.display = "none";
            document.getElementById("reqDate").style.display = "none";
            document.getElementById("managediv").style.display = "block";
            document.getElementById("rowInstructions").style.display = "block";
        }
        return;        
    }
}
function retrievePayment() {
    if ($("[id$='cBoxPayment']").attr("checked") == true) {
        $("[id$='cc_type']").val($("[id$='hiddenPaymentType']").val());
        $("[id$='txtcardnum']").val($("[id$='hiddenCC']").val());
        $("[id$='ddlexpmonth']").val($("[id$='hiddenExpMonth']").val());
        $("[id$='ddlexpyear']").val($("[id$='hiddenExpYear']").val());

        $("[id$='txtFirstName']").val($("[id$='xFirstName']").val());
        $("[id$='txtLastName']").val($("[id$='xLastName']").val());
        $("[id$='txtAddress']").val($("[id$='xAddress']").val());
        $("[id$='txtZip']").val($("[id$='xZip']").val());
    }
}
/*End Payment Page Functions */
