$(document).ready(function() {
    var options1 = {
        target: '',
        beforeSubmit: showRequest,  // pre-submit callback 
        success: showResponse  // post-submit callback 
    };

    var optionsReport = {
        target: '',
        beforeSubmit: showRequestReport,  // pre-submit callback 
        success: showResponseReport  // post-submit callback 
    };

    // attach handler to form's submit event 
    $('form').submit(function() {
        //alert("attach handler to form's submit event addtocart.js file");
        if (this.Category) {
            if ((this.Category.value == "REMEDYSP") || (this.Category.value == "REMEDYEN") || (this.Category.value == "DKRP") || (this.Category.value == "REMEDYBS") || (this.Item.value == "GOODORBADPLANETS") || (this.Item.value == "200912-JTASTREPHOM") || (this.Item.value == "200912-JTASTREPJUPTRA32")) {
                document.commonForm.tosubmit.value = this.name;
                $("#dlggcmmfrm").dialog("open");
            }
            else if ((this.Category.value == "REPRT") || (this.Category.value == "AREPRT")) {
                document.commonForm.tosubmit.value = this.name;
                $("#dlggcmmfrm").dialog("open");
                return false;
            }
            else {
                $(this).ajaxSubmit(options1);
            }
        }
        else { $(this).ajaxSubmit(options1); }
        return false;
    });

    $("#dlggcmmfrm").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 210,
        width: 420,
        modal: true,
        title: "Input your details",
        buttons: {
            Ok: function() {
                frm = eval("document." + $("#tosubmit").val());
                var arrElement = $('#commonForm :input');
                for (i = 0; i < arrElement.length; i++) {
                    if (arrElement[i].name.toLowerCase() == "name") {
                        frm.Name.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "nakno") {
                        frm.NakNo.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "userdetail") {
                        frm.UserDetail.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "partner") {
                        frm.Partner.value = arrElement[i].value;
                    }

                    if (arrElement[i].name.toLowerCase() == "monthlypay") {
                        if (arrElement[i].checked) {
                            frm.monthlypay.value = arrElement[i].value;
                        }
                    }
                    if (arrElement[i].name.toLowerCase() == "deposit") {
                        if (arrElement[i].checked) {
                            frm.deposit.value = arrElement[i].value;
                        }
                    }

                    //for Ask 3 Questions services
                    if (arrElement[i].name.toLowerCase() == "txtques1") {
                        frm.txtQues1.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "txtques2") {
                        frm.txtQues2.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "txtques3") {
                        frm.txtQues3.value = arrElement[i].value;

                    }
                    if (arrElement[i].name.toLowerCase() == "txtques1") {
                        if (frm.txtQues1.value == "" && frm.txtQues2.value == "" && frm.txtQues3.value == "") {
                            $("#errorMsg").html("Please enter atleast one question.");
                            $("#errorMsg").show();
                            return false;
                        }
                    }
                    if (arrElement[i].name.toLowerCase() == "txtdate") {
                        frm.txtDate.value = arrElement[i].value;
                        if (frm.txtDate.value == "") {
                            $("#errorMsg").html("Please enter date.");
                            $("#errorMsg").show();
                            return false;
                        }
                    }
                    if (arrElement[i].name.toLowerCase() == "txthour") {
                        frm.txtHour.value = arrElement[i].value;
                        if (frm.txtHour.value == "" || frm.txtHour.value > 12) {
                            $("#errorMsg").html("Please enter valid hour.");
                            $("#errorMsg").show();
                            return false;
                        }
                    }
                    if (arrElement[i].name.toLowerCase() == "txtminute") {
                        frm.txtMinute.value = arrElement[i].value;
                        if (frm.txtMinute.value == "" || frm.txtMinute.value > 60) {
                            $("#errorMsg").html("Please enter valid minute.");
                            $("#errorMsg").show();
                            return false;
                        }
                    }
                    if (arrElement[i].name.toLowerCase() == "txtsecond") {
                        frm.txtSecond.value = arrElement[i].value;
                        if (frm.txtSecond.value == "" || frm.txtSecond.value > 60) {
                            $("#errorMsg").html("Please enter valid second.");
                            $("#errorMsg").show();
                            return false;
                        }
                    }
                    if (arrElement[i].name.toLowerCase() == "ampm") {
                        frm.AMPM.value = arrElement[i].value;
                    }

                    //for Astrologer Prescription Remedies
                    if (arrElement[i].name.toLowerCase() == "cborem") {
                        frm.cboRem.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "txarem") {
                        frm.txaRem.value = arrElement[i].value;
                    }

                    //for Dog Astrology
                    if (arrElement[i].name.toLowerCase() == "DogName") {
                        frm.DogName.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "BDogdob") {
                        frm.BDogdob.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "BDogtob") {
                        frm.BDogtob.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "BDogcob") {
                        frm.BDogcob.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "BDogdod") {
                        frm.BDogdod.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "BDogtod") {
                        frm.BDogtod.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "BDogcod") {
                        frm.BDogcod.value = arrElement[i].value;
                    }

                    //for Astrologer NameChange Service
                    if (arrElement[i].name.toLowerCase() == "fulllegalname") {
                        frm.FullLegalName.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "selectionname") {
                        frm.SelectionName.value = arrElement[i].value
                    }
                    if (arrElement[i].name.toLowerCase() == "comments") {
                        frm.Comments.value = arrElement[i].value;
                    }

                    //for Tarpanam product
                    if (arrElement[i].name.toLowerCase() == "maternalnames") {
                        frm.MaternalNames.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "paternalnames") {
                        frm.PaternalNames.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "aname") {
                        frm.AName.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "coun") {
                        frm.Coun.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "city") {
                        frm.City.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "dateandtime") {
                        frm.DateandTime.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "relationships") {
                        frm.Relationships.value = arrElement[i].value;
                    }

                    //for Fertility products
                    if (arrElement[i].name.toLowerCase() == "daughtername") {
                        frm.DaughterName.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "parentsname") {
                        frm.ParentsName.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "parentsname2") {
                        frm.ParentsName2.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "txtaddress") {
                        frm.txtAddress.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "txtmessage") {
                        frm.txtMessage.value = arrElement[i].value;
                    }
                    if (arrElement[i].name.toLowerCase() == "rdmessage") {
                        frm.rdMessage.value = $("[name='rdMessage']:checked").val()
                    }

                }

                if ((frm.Name.value == "") && (frm.UserDetail.value == "")) {
                    $("#" + frm.elmtarget.value).html('<div class="failure">Result: Please enter your name and birthstar.</div>');
                }
                else {
                    //null for sepecil input
                    $("#otinput").html("");
                    if (frm.Category.value == "REPRT" || frm.Category.value == "AREPRT")
                        $("[name='" + $("#tosubmit").val() + "']").ajaxSubmit(optionsReport);
                    else
                        $("[name='" + $("#tosubmit").val() + "']").ajaxSubmit(options1);
                }

                $(this).dialog('close');
            },
            Cancel: function() {
                $(this).dialog('close');
                $("#otinput").html("");
            }
        },
        close: function(event) {
            $("#otinput").html("");
            $('#dlggcmmfrm').dialog('option', 'height', 210);
        }
    });

    //report dialog
    $("#reportdialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 540,
        width: 770,
        modal: true,
        title: "Reports",
        buttons: {
            Close: function() {
                window.location.reload();
                $(this).dialog('close');
            }
        }
    });
    $("#reportdialog").bind('dialogclose', function(event, ui) {
        window.location.reload();
    });
});

// pre-submit callback 
function showRequest(formData, jqForm, options) {
    $(".failure, .success").hide();
    temp = "";
    if (jqForm[0].elmtarget)
        temp = "#" + jqForm[0].elmtarget.value;
    else
        temp = "#" + jqForm[0].Item.value;
    options["target"] = temp;
    $(temp).html("<div class='loading'>Loading...&nbsp;&nbsp;<img src='/images/loading.gif'></div>");
    return true;
}

// post-submit callback 
function showResponse(responseText, statusText) {
    $(".shopbag").click(function() {
        $("#viewcart").load("/cart/cartcomp.asp");
        $("#viewcart").dialog("open");
        return false;
    });
}

// pre-submit callback 
function showRequestReport(formData, jqForm, options) {
    $(".failure, .success").hide();
    $("#reportdialog").dialog('option', 'title', $('#rtitle').val());
    $("#reportdialog").dialog('open');
    $("#reportdialog").html("<div class='loading'>Loading...&nbsp;&nbsp;<img src='/images/loading.gif'></div>");
    options["target"] = "#reportdialog";
    return true;
}
// post-submit callback 
function showResponseReport(responseText, statusText) {
    $("#PWFORM2").html(responseText);
    $("#PWFORM2").dialog("open");
}

//for mantra product
function submitMantraForm(formName, szItem) {
    var countrycode = formName.tmpccode.value;
    var strItem = new String();
    var ship = "1";
    var monthlypay = "0";
    strItem = szItem;

    formName.ship.value = ship;
    var countrycode = formName.tmpccode.value;

    if (formName.tmpccode.value == "USD") {
        if ((strItem.indexOf("3", 1) >= 0) || (strItem.indexOf("4", 1) >= 0)) {
            if (strItem.indexOf("2", 1) >= 0)
                monthlypay = "1";
            else
                monthlypay = "3";
            $('#dlggcmmfrm').dialog('option', 'height', 350);
            $("#otinput").html("<div><label>Do you want to ship the written mantras?</label><input type=radio value=1 name=ship checked>Yes<input type=radio value=0 name=ship>No</div><div><label>Do you want to pay monthly?</label><input type=radio value=1 name=monthlypay onClick='mntpayenable(\"" + szItem + "\",\"" + countrycode + "\")'>Yes<input type=radio value=0 name=monthlypay checked onClick='mntpaydisable()'>No</div><div id='paymentmt'></div>");

        }
    }
    else {
        if ((strItem.indexOf("2", 1) >= 0) || (strItem.indexOf("3", 1) >= 0) || (strItem.indexOf("4", 1) >= 0)) {
            if (strItem.indexOf("2", 1) >= 0)
                monthlypay = "1";
            else
                monthlypay = "3";
            $('#dlggcmmfrm').dialog('option', 'height', 260, 'width', 460);
            $("#otinput").html("<div><input type=hidden name=ship id=ship value=0 /> </div><div><label>Do you want to pay monthly?</label><input type=radio value=3 name=monthlypay onClick='mntpayenable(\"" + szItem + "\",\"" + countrycode + "\")'>Yes<input type=radio value=0 name=monthlypay checked onClick='mntpaydisable()'>No</div><div id='paymentmt'></div>");
        }
    }
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    $("#dlggcmmfrm").dialog("open");
}

function mntpayenable(strItem, countrycode) {
    if (strItem.indexOf("3", 1) >= 0)
        deposit = "500";
    else
        deposit = "1000";
    if (countrycode == "USD") {
        $("#paymentmt").html('<div><label>Please select the payment method:</label><select name=method id="selmet"><option value=1>12 payments</option><option value=2>24 payments</option></select></div><div><label>Do you like to pay the deposit?(' + deposit + ')</label><input type=radio value=1 name=deposit>Yes<input type=radio value=0 name=deposit>No</div>')
    }

}

function mntpaydisable() {
    $("#paymentmt").html('');
}

//for DKRP Services
function submitDKRPForm(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 270);
    $("#otinput").html("<div><p>Payment Type:</p><label>Full Payment</label><input type=radio value=FULLPAY name=Payment onClick='document.frmFULLPAY.Item.value=\"FULLPAY\"' checked><br/><label>12 Payments</label><input type=radio value=MONTHLYPAY1 name=Payment onClick='document.frmFULLPAY.Item.value=\"MONTHLYPAY1\"'><br/><label>4 Payments</label><input type=radio value=QUARTERPAY1 name=Payment onClick='document.frmFULLPAY.Item.value=\"QUARTERPAY1\"'></div>");
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    $("#dlggcmmfrm").dialog("open");
}
function dkrppayment(strItem) {
    alert(strItem);
}

//for Tarpanam product
function submitTarpanamForm(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 270);
    $("#otinput").html('<div><b>Note: Please enter the name if you known, else leave the boxes empty</b></div><div><label>Maternal Name</label><input type="text" name="MaternalNames" maxlength="150" /></div><div><label>Paternal Name</label><input type="text" name="PaternalNames" maxlength="150" /></div>');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    //open name & birthstar dailog
    $("#dlggcmmfrm").dialog("open");
}


//for Ask3Questions
function submitAsk3Questions(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 365);
    $("#otinput").load('/include/ask3ques.asp');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    $("#dlggcmmfrm").dialog("open");
}

//for Dog Astrlogy
function submitdogastrology(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 450);
    $("#otinput").load('/include/dogastrology.asp');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    $("#dlggcmmfrm").dialog("open");
}


//for Astrologer PrescriptionRemedies
function submitPrescRemedies(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 330);
    $("#otinput").html('<div><table><tr><td align="right" width="39%"><b>Remedies:</b></td><td width="232"><select name="cboRem" id="cboRem"><option value="sel" selected="selected">--- Select ---</option><option value="Father">Father Relationship</option><option value="Mother">Mother Relationship</option><option value="Finance">Money</option><option value="Romance">Romance</option><option value="Career">Career</option><option value="Health">Health</option><option value="Saturn Remedy">Saturn Remedy</option><option value="AOT">Any Other Area of Life</option></select></td></tr><tr><td align="right"><b>Add Your Comments:</b></td><td><textarea name="txaRem" rows="6" cols="28" id="txaRem"></textarea></td></tr></table></div>');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    $("#dlggcmmfrm").dialog("open");
}

//for Astrologer Namechange
function submitNamechange(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 330);
    $("#otinput").html('<div><table><tr><td width="53%">Full Legal Name: </td><td><input type="text" id="FullLegalName" name="FullLegalName" size="27" /></td></tr><tr><td> Name Change Type : </td><td><select name="SelectionName" id="SelectionName"><option value="H">Spiritual Name</option><option value="O">Other Name</option></select></td></tr><tr><td>Requested Name or God/Goddess Name</td><td><label><input type="text" id="Comments" name="Comments" size="27" /></label></td></tr></table></div>');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    $("#dlggcmmfrm").dialog("open");
}

//for Astrologer BeautySecrets
function submitBeautySecrets(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 200);
    $("#otinput").html('<div><table><tr><td width="35%">Add your Comments:</td><td width="33%"><input type="text" id="txaRem" name="txaRem" /></td></tr>');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    $("#dlggcmmfrm").dialog("open");
}


function submitDeathDyingForm(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 380);
    $('#dlggcmmfrm').dialog('option', 'width', 530);
    $("#otinput").load('/include/formdeathdying.asp');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    //open name & birthstar dailog
    $("#dlggcmmfrm").dialog("open");
}

function submitBirthStarForm(formName, szItem) {
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    //open name & birthstar dailog
    $("#dlggcmmfrm").dialog("open");
}


//fill item and open name & birthstar dailog form
function FillNameBirthStarItem(formName, szItem) {
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    //open name & birthstar dailog
    $("#dlggcmmfrm").dialog("open");
}

//for Fertility products
function submitFertilityForm(formName, szItem) {
    $('#dlggcmmfrm').dialog('option', 'height', 260);
    $('#dlggcmmfrm').dialog('option', 'width', 530);

    $("#otinput").html('<div><label>Enter Your Daughter Name</label><input type="text" name="DaughterName" value="" size="30" maxlength="200" ID="Text4"></div><div><label>Enter First Parents Name and BirthStar</label><input type="text" name="ParentsName" value="" size="30" maxlength="300" ID="Text4"><Font color="red">(eg. Jane Doe, Rohini) </font></div><div><label>Enter Second Parents Name and BirthStar (Optional)</label><input type="text" name="ParentsName2" value="" size="30" maxlength="300" ID="Text4"><Font color="red">(eg.John Doe, Revati)</font></div>');
    formName.Item.value = szItem;
    document.commonForm.tosubmit.value = formName.name;
    //open name & birthstar dailog
    $("#dlggcmmfrm").dialog("open");
}

//validate charity form
function submitCharityForm() {
    amtType = $("[name='AmountRadio']:checked").val();
    if (amtType == "fix")
        $("#Amount").val($("#FixAmount").val());
    else
        $("#Amount").val($("#VariableAmount").val());
    if ($("#Amount").val() == "") {
        $("#TRIPURA").html("<div class=failure>Please enter amount!</div>");
    }
    else
        $("#CharityForm").ajaxSubmit({ target: '#TRIPURA' });
}