/*****************************************************************Script to move into generalize group and will be included in every form ***********************************************************************/
function updateLook() {
    $(".insideTitle").each(function () {
        $(this).next("input").addClass("insideDefault");
        if ($(this).next().attr("set") != "1") {
            $(this).next("input").val($(this).find("span").html().replace(":", ""));
        }
        else {
            $(this).next().addClass("insideActive");
            $(this).next().removeClass("insideDefault");
        }
        //$(this).find("input").css("font-weight", "bold");
        if ($(this).next("input").attr("type") != "radio") {
            $(this).find("span").css("display", "none");
            $(this).next("input").focus(function () {
                if (!$(this).hasClass("insideActive")) {
                    $(this).removeClass("insideDefault");
                    $(this).val("");
                    $(this).addClass("insideActive");
                }
            });

            $(this).next("input").blur(function () {
                if ($(this).val() == "") {
                    $(this).removeClass("insideActive");
                    $(this).addClass("insideDefault");
                    $(this).val($(this).prev().find("span").html().replace(":", ""));
                    //$(this).val("Test");
                }
            });
        }
        if ($(this).next("input").attr("set") == "1") {            
            $(this).next().blur();  
        }
    });

    $("[formtype='CreditCardNumber']").keypress(function () {
        $("[type='CCImage']").each(function () {
            $(this).fadeTo(0, .4);
            $(this).css("padding-left", "5px");
        });        
        if ($(this).val().length == 14 && $(this).val().indexOf("3") == 0) {
            //AMEX
            $("[cardType='amex']").fadeTo(0, 1);
        }
        if ($(this).val().length == 15 && $(this).val().indexOf("4") == 0) {
            //Visa
            $("[cardType='visa']").fadeTo(0, 1);
        }
        if ($(this).val().length == 15 && $(this).val().indexOf("5") == 0) {
            //MasterCard
            $("[cardType='mastercard']").fadeTo(0, 1);
        }
        if ($(this).val().length == 15 && $(this).val().indexOf("6") == 0) {
            //Discover
            $("[cardType='discover']").fadeTo(0, 1);
        }
    });

    //Make all the CC Images opacity to .4
    $("[type='CCImage']").each(function () {
        $(this).fadeTo(0, .4);
        $(this).css("padding-left", "5px");
    });

    formatPage();
}

//Format The form page
function formatPage() {    
    var isBackend = false;
   try{
       if (parent.isBackend == undefined) {
           isBackend = false;
       } else {
           isBackend = parent.isBackend;
       }
   } catch (ex) {
        isBackend = false;
   }

    //Get the max width for each field set
   $(document).ready(function () {
       //alert($(this).parent("iframe#right"));              
       if (!isBackend) {
           $(".fieldsContainer").each(function () {
               var maxWidth = 1;
               $(this).find(".leftSideTitle").each(function () {
                   //get the width of each one
                   //alert($(this).width());
                   if ($(this).width() > maxWidth) {
                       maxWidth = $(this).width();
                   }
               });
               //Set the leftside title to all be the same
               //alert(maxWidth);        
               //alert(maxWidth);
               if (maxWidth > 10) {
                   $(this).find(".leftSideTitle").css("width", maxWidth);
                   var rightSideInputWidth = $(this).find(".leftSideTitle").parent().width() - maxWidth - 76
                   $(this).find(".leftSideTitle + input").css("width", rightSideInputWidth);
                   $(this).find(".leftSideTitle + textarea").css("width", rightSideInputWidth);
                   //alert(rightSideInputWidth);
                   $(this).find(".leftSideTitle + select").each(function () {
                       if ($(this).width() > rightSideInputWidth) {
                           $(this).css("width", rightSideInputWidth);
                       }
                   });
               }
           });
       }
   });

    setTimeout(function () {        
        var maxWidth = 0;
        $(".productListItemLabel").each(function () {
            //alert("test");            
            if ($(this).width() > maxWidth) {
                maxWidth = $(this).width();
            }
            //Set the leftside title to all be the same            
        });
        if(maxWidth > 0)
            $(".productListItemLabel").css("width", maxWidth);
    }, 50);

}

function addElementAppend(htmlString, divId) {  
    //alert(htmlString.replace(/\|/g, '"'));
    var appendEle = $(htmlString.replace(/\|/g, '"'));
    $("#" + divId).append(appendEle);
}

function removeParentElement(ele) {
    $(ele).parent().parent().remove();
}

function loadEvents() {
    sameCheck();
    navButtonPressed();
}

function navButtonPressed() {
    $(".FormNavButton").click(function () {
        //Clear out all the ones that are not submitted
        $(".insideTitle").each(function () {
            if ($(this).next("input").hasClass("insideDefault")) {
                $(this).next("input").val("");
            }
        });
    });
}

function sameCheck() {    
    $(".sameAsCheck").children("input").unbind("click");
    $(".sameAsCheck").children("input").bind('click', function () {
        if ($(this).attr("state") == "" || $(this).attr("state") == "off" || $(this).attr("state") == undefined) {
            //alert("going to check" + $(this).parent().attr("fieldMap"));
            var fieldArray = $(this).parent().attr("fieldMap").split(";");            
            for (var i = 0; i < fieldArray.length; i += 2) {
                //Take what is in field 1 and add it to field 2
                if (!$("[clientid='field_" + fieldArray[i] + "']").find("input").hasClass("insideDefault")) {                    
                    $("[clientid='field_" + fieldArray[i + 1] + "']").find("input").val($("[clientid='field_" + fieldArray[i] + "']").find("input").val());
                    $("[clientid='field_" + fieldArray[i + 1] + "']").find("input").addClass("insideActive");
                    $("[clientid='field_" + fieldArray[i + 1] + "']").find("input").removeClass("insideDefault");
                }
                $("[clientid='field_" + fieldArray[i + 1] + "']").find("select").val($("[clientid='field_" + fieldArray[i] + "']").find("select").val());
            }
            $(this).attr("state", "on");
        }
        else {
            //alert("going to uncheck" + $(this).parent().attr("fieldMap"));            
            var fieldArray = $(this).parent().attr("fieldMap").split(";");
            for (var i = 0; i < fieldArray.length; i += 2) {
                //Take what is in field 1 and add it to field 2
                $("[clientid='field_" + fieldArray[i + 1] + "']").find("input").val("");
                $("[clientid='field_" + fieldArray[i + 1] + "']").find("select").val("");
                $("[clientid='field_" + fieldArray[i + 1] + "']").find("input").blur();
            }
            $(this).attr("state", "off");
        }
    });
}
/***************************************************************End Script*************************************************************************************************************************************/
