﻿//alert('t');
//alert(document.documentElement.clientHeight);
//$('#footerMain').css("top", document.documentElement.clientHeight);
////document.getElementById('footerMain').style.top=window.scrollTop
//$(window).scroll(function() {
//    //            if ($(window).scrollTop() > 385) {
//    $('#footerMain').css("position", "absolute");
//    //$('.newmember').css("top", $(window).scrollTop());
//    $('#footerMain').stop();
//    //$('.newmember').animate({"top":$(window).scrollTop()}, 200);
//    $('#footerMain').css("top", document.documentElement.clientHeight);
//    //$('.footerMain').css("left", document.documentElement.clientHeight);
//    //$('.footerMain').css('border-bottom', 'solid 3px #FF0000')
//    //            }
//    //            else {
//    //$('#footerMain').css("position", "");
//    //$('#footerMain').css('border-bottom', 'solid 1px #b6cde1')
//    //}
//});

$(document).ready(function() {
    //    if (getCookie("UID").length <= 0)
    //    { window.location.href = 'index.html'; }
    setPersonProjects();
    function getdata(n, d) {
        if (n == "Hours") {
            if (d == "Add")
                return "{StartTime:'" + getCookie('StartTime') + "',EndTime:'" + getCookie('EndTime') + "',PersonID:'" + getCookie('PersonID') + "',TotalHour:'" + $('#TotalHour').val() + "',Project:'" + $('#drdProjects').val() + "',LastModifiedBy:'" + getCookie('UserID') + "'}";
            else if (d == "Update")
                return "{HourID:'" + $('#HourID').val() + "',StartTime:'" + $('#StartTime').val() + "',EndTime:'" + $('#EndTime').val() + "',PersonID:'" + $('#PersonID').val() + "',TotalHour:'" + $('#TotalHour').val() + "',Project:'" + $('#Project').val() + "',TrueHour:'" + $('#TrueHour').val() + "',LastModifiedBy:'" + $('#LastModifiedBy').val() + "'}";
        }
        else if (n == "Company") {
            if (d == "Add") {
                return "{CompanyName:'" + $('#CompanyName').val() + "',TaxID:'" + $('#TaxID').val() + "',Type:'" + $('#Type').val() + "',Website:'" + $('#Website').val() + "',Address1:'" + $('#Address1').val() + "',Address2:'" + $('#Address2').val() + "',City:'" + $('#City').val() + "',Region:'" + $('#Region').val() + "',PostalCode:'" + $('#PostalCode').val() + "',Phone:'" + $('#Phone').val() + "'}";
            }
            else if (d == "Update") {
                return "{CompanyName:'" + $('#CompanyName').val() + "',TaxID:'" + $('#TaxID').val() + "',Type:'" + $('#Type').val() + "',Website:'" + $('#Website').val() + "',Address1:'" + $('#Address1').val() + "',Address2:'" + $('#Address2').val() + "',City:'" + $('#City').val() + "',Region:'" + $('#Region').val() + "',PostalCode:'" + $('#PostalCode').val() + "',Phone:'" + $('#Phone').val() + "'}";
            }
        }
        else if (n == "Material") {
            if (d == "Add") {
                return "{ProductName:'" + $('#ProductName').val() + "',Currency:'" + $('#Currency').val() + "',TradeName:'" + $('#TradeName').val() + "',Specification:'" + $('#Specification').val() + "',ExtractionSolvents:'" + $('#ExtractionSolvents').val() + "',CountryOfOrigin:'" + $('#Country').val() + "',PricePerUnit:'" + $('#PricePerUnit').val() + "',Unit:'" + $('#Unit').val() + "',VendorID:'" + $('#VendorID').val() + "',VendorProductID:'" + $('#VendorProductID').val() + "',notes:'" + $('#txtNotes').val() + "',UID:'" + getCookie("UID") + "',Percentage:'" + $('#spercentage').val() + "',Master:'" + $('#Master').val() + "'}";
            }
            else if (d == "Update") {
                return "{ProductName:'" + $('#ProductName').val() + "',Currency:'" + $('#Currency').val() + "',ID:'" + $('#hid').val() + "',TradeName:'" + $('#TradeName').val() + "',Specification:'" + $('#Specification').val() + "',ExtractionSolvents:'" + $('#ExtractionSolvents').val() + "',CountryOfOrigin:'" + $('#Country').val() + "',PricePerUnit:'" + $('#PricePerUnit').val() + "',Unit:'" + $('#Unit').val() + "',VendorID:'" + $('#VendorID').val() + "',notes:'" + $('#txtNotes').val() + "',VendorProductID:'" + $('#VendorProductID').val() + "',UID:'" + getCookie("UID") + "',Percentage:'" + $('#spercentage').val() + "',Master:'" + $('#Master').val() + "'}";
            }
        }
        else if (n == "Inventory")
            return "{RawName:'" + $('#RawName').val() + "',VendorsProductID:'" + $('#VendorsProductID').val() + "',Price:'" + $('#Price').val() + "',Vendor:'" + $('#Vendor').val() + "',MHLotNo:'" + $('#MHLotNo').val() + "',BatchID:'" + $('#BatchID').val() + "',Quantity:'" + $('#Quantity').val() + "',Unit:'" + $('#Unit').val() + "',Completed:'" + $('#Completed').val() + "',Total:'" + $('#Total').val() + "',Discontinued:'" + $('#Discontinued').val() + "',RawSpecification:'" + $('#RawSpecification').val() + "',InternalIngredientId:'" + $('#InternalIngredientId').val() + "',InternalProductID:'" + $('#InternalProductID').val() + "',CreatedBy:'" + getCookie("UID") + "'}";
        else if (n == "VendorContact") {
            if (d == "Add") {
                return "{VendorID:'" + $('#VendorID').val() + "',Salutation:'" + $('#Salutation').val() + "',FirstName:'" + $('#FirstName').val() + "',LastName:'" + $('#LastName').val() + "',Phone:'" + $('#Phone').val() + "',Email:'" + $('#Email').val() + "',CellPhone:'" + $('#CellPhone').val() + "',EnteredBy:'" + getCookie("UID") + "',Default:'" + $('#DefaultContact').attr('checked') + "'}";
            }
            else if (d == "Update") {
                return "{VendorID:'" + $('#VendorID').val() + "',Salutation:'" + $('#Salutation').val() + "',FirstName:'" + $('#FirstName').val() + "',LastName:'" + $('#LastName').val() + "',Phone:'" + $('#Phone').val() + "',Email:'" + $('#Email').val() + "',CellPhone:'" + $('#CellPhone').val() + "',ID:'" + $('#hid').val() + "',LastModifiedBy:'" + getCookie("UID") + "',Default:'" + $('#DefaultContact').attr('checked') + "'}";
            }
        }
        else if (n == "InventoryOrder") {
            if (d == "Add") {
                return "{InventoryID:'" + $('#InventoryID').val() + "',Quantity:'" + $('#Quantity').val() + "',Unit:'" + $('#Unit').val() + "',UnitPrice:'" + $('#UnitPrice').val() + "',ShippingCost:'" + $('#ShippingCost').val() + "',Status:'" + $('#Status').val() + "',ExpireDate:'" + $('#ExpireDate').val() + "',CreatedBy:'" + getCookie("UID") + "'}";
            }
            if (d == "Update") {
                return "{InventoryID:'" + $('#InventoryID').val() + "',Quantity:'" + $('#Quantity').val() + "',Unit:'" + $('#Unit').val() + "',UnitPrice:'" + $('#UnitPrice').val() + "',ShippingCost:'" + $('#ShippingCost').val() + "',Status:'" + $('#Status').val() + "',ExpireDate:'" + $('#ExpireDate').val() + "',ID:'" + $('#hid').val() + "',LastModifiedBy:'" + getCookie("UID") + "'}";
            }
        }
        else if (n == "MInventoryOrder") {
            if (d == "Add") {
                return "{q:'" + $('#Mvalues').val() + "',Company:'" + $('#Partner option:selected').text() + ';' + $('#Street').val() + ';' + $('#City').val() + ', ' + $('#Region option:selected').text() + ' ' + $('#Zip').val() + ';Phone: ' + $('#Phone').val() + ';Fax: ' + $('#Fax').val() + "',CreatedBy:'" + getCookie("UID") + "',email:'" + $('#VContact').val() + "',Notes:'" + $('#txtNotes').val() + "',Urgent:'" + $("#Urgent").attr("checked") + "'}";
            }
            if (d == "Update") {
                return "{q:'" + $('#Mvalues').val() + "',ID:'" + $('#hid').val() + "',Company:'" + $('#Partner option:selected').text() + ';' + $('#Street').val() + ';' + $('#City').val() + ', ' + $('#Region option:selected').text() + ' ' + $('#Zip').val() + ';Phone: ' + $('#Phone').val() + ';Fax: ' + $('#Fax').val() + "',LastModifiedBy:'" + getCookie("UID") + "',email:'" + $('#VContact').val() + "',Notes:'" + $('#txtNotes').val() + "',Urgent:'" + $("#Urgent").attr("checked") + "'}";
            }
        }
        else if (n == "User") {
            if (d == "Add") {
                return "{FirstName:'" + $('#FirstName').val() + "',LastName:'" + $('#LastName').val() + "',Email:'" + $('#Email').val() + "',PW:'" + $('#PW').val() + "',CreatedBy:'" + getCookie("UID") + "'}";
            }
            if (d == "Update") {
                return "{FirstName:'" + $('#FirstName').val() + "',LastName:'" + $('#LastName').val() + "',ID:'" + $('#hid').val() + "',Email:'" + $('#Email').val() + "',PW:'" + $('#PW').val() + "',CreatedBy:'" + getCookie("UID") + "'}";
            }
        }
    }
    //add name,do function
    function Insert(n, d) {
        //alert(getdata(n, d)); //return true;
        if (checkrequired(n).length > 0)
        { alert("The following error(s) occurred:" + checkrequired(n)); }
        else {
//            $('#Message', document).css('display', 'block');
//            $('#Screen', document).css('display', 'block');
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Ajax.asmx/" + d + n,
                data: getdata(n, d),
                dataType: 'json',
                async: false,
                success: function(result) {
                    if (result.d.indexOf('New:') >= 0) {
                        addview(result.d.substring(4, result.d.length), n, getCookie("UID")); getviewp(getCookie("UID"));
                        $('#hid').val(result.d.substring(4, result.d.length));
                        alert("Submit Successfully."); AfterInsert();
//                        $('#Message', document).css('display', 'none');
//                        $('#Screen', document).css('display', 'none');
                    }
                    else if (result.d.indexOf('Upd:') >= 0) {
                        addview(result.d.substring(4, result.d.length), n, getCookie("UID")); getviewp(getCookie("UID"));
                        $('#hid').val(result.d.substring(4, result.d.length));
                        alert("Updated Successfully.");
//                        $('#Message', document).css('display', 'none');
//                        $('#Screen', document).css('display', 'none');
                    }
                    else {
                        alert(result.d);
//                         $('#Message', document).css('display', 'none');
//                        $('#Screen', document).css('display', 'none');
                    }
                },
                //timeout: 5000,
                error: function(result, status) {
                    if (status == 'error') {
                        alert('Script Error.'); 
//                        $('#Message', document).css('display', 'none');
//                        $('#Screen', document).css('display', 'none');
                    }
                }
            });
        }
    }

    function gup(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null)
            return "";
        else
            return results[1];
    }

    function getCookie(c_name) {
        //alert('test');
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
                //alert(document.cookie.substring(c_start,c_end));
            }
        }
        return "";

    }
    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    }
    function checkCookie() {
        username = getCookie('username');
        if (username != null && username != "") {
            alert('Welcome again ' + username + '!');
        }
        else {
            username = prompt('Please enter your name:', "");
            if (username != null && username != "") {
                setCookie('username', username, 365);
            }
        }
    }
    function getLookupValue(type, id) {
        var v = '';
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Ajax.asmx/getlookupValue",
            data: "{id:'" + id + "',type:'" + type + "'}",
            dataType: 'json',
            async: false,
            success: function(result) {
                //alert(result.d);
                v = result.d;

            },
            //timeout: 5000,
            error: function(result, status) {
                if (status == 'error') {
                    alert('Ajax Error.');
                    // return 'Error';
                }
            }
        });
        return v;
    }
    function setPersonProjects() {
        if ($('#drdProjects').is('select')) {
            var s = getLookupValue('PersonProjects', '9');
            if (s.length > 0) {
                $(s.split(';')[1].split(',')).each(function(index) {
                    $('#drdProjects').append('<option value="' + s.split(';')[0].split(',')[index] + '">' + this.toString() + '</option>');
                });
            }
        }
    }
    $("#btnSub").click(function() {
        $('#Message', top.document).css('display', 'block');
        $('#Screen', top.document).css('display', 'block');
        Insert('Hours', 'Add');
    });
    $("#Subcompany").click(function() {
        //        $('#Message', top.document).css('display', 'block');
    //        $('#Screen', top.document).css('display', 'block');
        alert(getdata('Company', 'Add'));
             Insert('Company', 'Add');
    });

    function checkrequired(n) {
        var r = "";
        if (n == "Hours") {

            if ($('#TotalHour').val().length <= 0) {
                r += "\n Total Hour is required.";
            }

            if ($('#drdProjects').val().length <= 0 || $('#drdProjects').val() == "0") {
                r += "\n Projects is required.";
            }
        }
        else if (n == "VendorContact") {

        }
        else {

        }
        return r;
    }
    $("#login").click(function() {
        login($("#name").val(), $("#password").val());
    });

    function login(email, pw) {
        var v = '';
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Ajax.asmx/elogin",
            data: "{email:'" + email + "',pw:'" + pw + "'}",
            dataType: 'json',
            async: false,
            success: function(result) {
                alert(result.d);
                if (result.d.toLowerCase().indexOf("error") > 0) {
                    v = result.d; alert(v);
                }
                else {
                    var ar = new Array(); ar = result.d.split(';'); alert(ar[0]);
                    if (ar[0] == '2')
                    { window.location.href = "projectsmgr/projectsdis.aspx"; }
                    else if (ar[0] == '3')
                    { window.location.href = "addcompany.aspx"; }
                    else if (ar[0] == '1')
                    { window.location.href = "projectsmgr/projectsdis.aspx"; }
                }


            },
            //timeout: 5000,
            error: function(result, status) {
                if (status == 'error') {
                    alert('Ajax Error.');
                    // return 'Error';
                }
            }
        });
        return v;
    }
    //$('#MainDv').css("position", "absolute");
    //$("#MainDv").css('overflow', 'hidden');
    //$("#MainDv").css('height', $(window).height() - 306);
    //$('#footerMain').css("position", "absolute");
    // $("#footerMain").css('top', $(window).height() - 66);
    //    $('#search').click(function() {
    //    if ($('#search').val() == 'Enter Keyword')
    //    { $('#search').val(''); }
    //    });
});
