﻿/* ***********************************************
Warranty request support scripts
************************************************** */

//issue counter for issue div IDs
var issueCount = 0;


//add warranty to the list
function AddIssue() {

    $("#issueValAreaList").empty();
    $("#issueValArea").hide();

    issueCount++;

    //var lstIssueType = $("#lstIssueType")[0];
    //var lstIssueLocation = $("#lstIssueLocation")[0];

    var newIssueEl = $("#issueTemplate").clone().attr("id", "issueDiv" + issueCount);
    newIssueEl.show();

    var issueType = "N/A"; //lstIssueType.options[lstIssueType.selectedIndex].value;
    var issueLocation = "N/A"; //lstIssueLocation.options[lstIssueLocation.selectedIndex].value;
    var issueDescription = $("#txtIssueDescription").val();

    var errorMessage = ValidateAddIssue(issueType, issueLocation, issueDescription);
    if (errorMessage != "") {
        $("#issueValAreaList").append(errorMessage);
        $("#issueValArea").show();
        return;
    }

    //newIssueEl.find("#issueType").text(issueType);
    //newIssueEl.find("#issueLocation").text(issueLocation);
    issueDescription = issueDescription.replace(/(\r\n|\r|\n)/g, "<br/>");
    newIssueEl.find("#issueDescription").html(issueDescription);
    newIssueEl.find("#issueRemove").click(function(e) {
        if (confirm(removeIssueConfirmMessage)) {
            $(this).parent().parent().remove();
            document.getElementById(issuesFieldId).value = PackIssues();
        }
    });

    newIssueEl.appendTo("#issueWrapper");

    document.getElementById(issuesFieldId).value = PackIssues();

    $("div.issueDiv:odd").css("background", "#eeeeee");
    $("div.issueDiv:even").css("background", "#ffffff");

    ResetAddForm();
}

//validates issue addition
function ValidateAddIssue(issueType, issueLocation, issueDescription) {

    var errorMessage = "";

    if (issueType == "@") {
        errorMessage += "<li>- " + issueValidationSelectTypeMessage + "</li>";
    }

    if (issueLocation == "@") {
        errorMessage += "<li>- " + issueValdiationSelectLocationMessage + "</li>";
    }

    if (issueDescription == "") {
        errorMessage += "<li>- " + issueValidationEnterDescriptionMessage + "</li>";
    }

    return errorMessage;
}


//resets the addition form
function ResetAddForm() {
    //var lstIssueType = $("#lstIssueType")[0].selectedIndex = 0;
    //var lstIssueLocation = $("#lstIssueLocation")[0].selectedIndex = 0;
    $("#txtIssueDescription").val("");
    $("#numChars").text(maxIssueLength + issueLengthMessage);
}


//packs the issues as a string
function PackIssues() {
    var issueString = "";

    $("div.issueDiv:visible").each(function(index) {
        if (issueString != "") {
            issueString += "|";
        }

        var issueDiv = $("#" + this.id);
        //issueString += issueDiv.find("#issueType").text() + "^";
        //issueString += issueDiv.find("#issueLocation").text() + "^";
        issueString += issueDiv.find("#issueDescription").text();

    });

    return issueString;
}

//page init
function InitPage() {

    //init the data picker for the close date
    var dp = $("#" + closeDateFieldId).datePicker(
		{
		    startDate: '01/01/2001',
		    endDate: (new Date()).asString()
		}
	).bind(
			'dpDisplayed',
			function(e, $datePickerDiv) {
			    var dateValidator = document.getElementById(closeDateValidatorId);
			    document.getElementById("dp-popup").scrollIntoView();
			}
		).bind(
			'dateSelected',
			function(e, selectedDate, $td) {
			    var dateValidator = document.getElementById(closeDateValidatorId);
			}
		);

    //override MS VaidatorOnChange from the .Net script library
    //to fix bug when the "vals" are not defined
    ValidatorOnChange = function(event) {
        if (!event) {
            event = window.event;
        }
        Page_InvalidControlToBeFocused = null;
        var targetedControl;
        if ((typeof (event.srcElement) != "undefined") && (event.srcElement != null)) {
            targetedControl = event.srcElement;
        }
        else {
            targetedControl = event.target;
        }
        var vals;
        if (typeof (targetedControl.Validators) != "undefined") {
            vals = targetedControl.Validators;
        }
        else {
            if (targetedControl.tagName.toLowerCase() == "label") {
                targetedControl = document.getElementById(targetedControl.htmlFor);
                vals = targetedControl.Validators;
            }
        }
        if (vals != null) {
            var i;
            for (i = 0; i < vals.length; i++) {
                ValidatorValidate(vals[i], null, event);
            }

            ValidatorUpdateIsValid();
        }

    }

    //prevent form submission on ENTER key
    //    $("body").bind("keypress", function(e) {
    //        var key;
    //        if (window.event)
    //            key = window.event.keyCode; //IE
    //        else
    //            key = e.which; //firefox     

    //        return (key != 13);
    //    });

    //bind ENTER key to the add issue description text field
    //    $("#txtIssueDescription").bind("keypress", function(e) {
    //        var keycode;

    //        if (window.event) {
    //            keycode = window.event.keyCode;
    //        }
    //        else if (e) {
    //            keycode = e.which;
    //        }

    //        if (keycode == 13) {
    //            AddIssue();
    //        }
    //    });

    $("#txtIssueDescription").bind("keyup change input", function(e) {
        validateIssueLength($(this));
    });

    if (navigator.userAgent.indexOf("MSIE") > -1) {
        $("#txtIssueDescription").bind("paste", function(e) {
            $("#lnkAddIssue").focus();
        });

        $("#txtIssueDescription").bind("blur", function(e) {
            validateIssueLength($(this));
        });
    }

//    $("#" + phoneFieldId).bind("blur", function(e) {
//        var matchpattern = /\(?([0-9]{3})\)?-?([0-9]{3})-?([0-9]{4})/;
//        var matcher = new RegExp(matchpattern);
//        var matched = matcher.exec($(this).val());
//        if (matched != null) {
//            var new_num = "(" + matched[1] + ") " + matched[2] + "-" + matched[3];
//            $(this).val(new_num);
//        }
//    });
}
function ValidatePhoneNumber(source, args) {
    var matchpattern = /\(?([0-9]{3})\)?-?([0-9]{3})-?([0-9]{4})/;
    var matcher = new RegExp(matchpattern);
    var matched = matcher.exec($("#" + phoneFieldId).val());
    if (matched != null) {
        var new_num = "(" + matched[1] + ") " + matched[2] + "-" + matched[3];
        $("#" + phoneFieldId).val(new_num);
    }
    var matchpattern2 = /\(([0-9]{3})\) ([0-9]{3})-([0-9]{4})$/;
    matcher = new RegExp(matchpattern2);
    matched = matcher.exec($("#" + phoneFieldId).val());
    if (matched != null)
        args.IsValid = true;
    else
        args.IsValid = false;

}
function ValidateMobilePhoneNumber(source, args) {
    var matchpattern = /\(?([0-9]{3})\)?-?([0-9]{3})-?([0-9]{4})/;
    var matcher = new RegExp(matchpattern);
    var matched = matcher.exec($("#" + mobilePhoneFieldId).val());
    if (matched != null) {
        var new_num = "(" + matched[1] + ") " + matched[2] + "-" + matched[3];
        $("#" + mobilePhoneFieldId).val(new_num);
    }
    var matchpattern2 = /\(([0-9]{3})\) ([0-9]{3})-([0-9]{4})$/;
    matcher = new RegExp(matchpattern2);
    matched = matcher.exec($("#" + mobilePhoneFieldId).val());
    if (matched != null)
        args.IsValid = true;
    else
        args.IsValid = false;

}
function validateIssueLength(txtObject) {
    var count = txtObject.val().length;
    if (count > maxIssueLength) {
        txtObject.val(txtObject.val().substr(0, maxIssueLength));
        count = maxIssueLength;
    }
    
    $("#numChars").text((maxIssueLength - count) + issueLengthMessage);
}

//validates that at least one issue has been entered
function vldIssues_Validate(source, args) {
    args.IsValid = IsAtLeastIssueEntered();
}

function IsAtLeastIssueEntered() {
    return $("div.issueDiv:visible").length > 0;
}

//validates that the closing date is not in the future
function vldCloseDateFuture_Validate(source, args) {
    try {
        var closeDate = Date.fromString(document.getElementById(closeDateFieldId).value);
        var currentDate = new Date();
        args.IsValid = (closeDate <= currentDate);

    }
    catch (e) { args.isValid = true; }
}



