﻿
function MM_showHideLayers() { //v9.0
  var i, p, v, obj, args = MM_showHideLayers.arguments;
  for (i = 0; i < (args.length - 2); i += 3)
    with (document) if (getElementById && ((obj = getElementById(args[i])) != null)) {
    v = args[i + 2];
    if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
    obj.visibility = v;
  }
}

function MM_preloadImages() { //v3.0
  var d = document; if (d.images) {
    if (!d.MM_p) d.MM_p = new Array();
    var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
      if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
  }
}

function MM_swapImgRestore() { //v3.0
  var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
    d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
  }
  if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
  for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
  if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
    if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

$(document).ready(function() {

  if ($.browser.mozilla) {
    $("body").height($(window).height() + 1);
  }

});


// User.js


var validNoImageString = "<img src='/content/en-US/media/images/Common/validno.png' />";
var validYesImageString = "<img src='/content/en-US/media/images/Common/validyes.png' />";

$(document).ready(function() {

  $("#logout").click(function() {
    jConfirm('Are you sure you want to logout ?', 'Confirmation Dialog', function(result) {

      if (result) {
        $.ajax({

          type: "POST",
          url: "/User/Logout",
          data: "",

          success: function(result) {
		   
            location.reload();
          },
          error: function(result) {
            ////alert(result.status);
          }

        });
      }
    });

  });

  $(".country").change(function() {
    if ($(this).val() != -1) {
      fillStatesDropDown($(this).attr("statesDropDown"), $(this).val(), "-1");
      $("#" + $(this).attr("statesDropDown")).removeAttr("disabled");
    }
    else {
      $("#" + $(this).attr("statesDropDown")).attr({ "disabled": "disabled" });
    }
  });

  $("#sameAsBilling").change(function() {
    if ($(this).attr('checked')) {
//      $("#rifirstname").val($("#bifirstname").val());
//      $("#rimiddlename").val($("#bimiddlename").val());
//      $("#rilastname").val($("#bilastname").val());
//      $("#riaddress").val($("#biaddress").val());
//      $("#riaddress2").val($("#biaddress2").val());
//      $("#ricompany").val($("#bicompany").val());
//      $("#ripostalcode").val($("#bipostalcode").val());
//      $("#riphone").val($("#biphone").val());
//      $("#rifax").val($("#bifax").val());

//      fillCountriesDropDown("ricountry", $("#bicountry").val());
//      fillStatesDropDown("ristate", $("#bicountry").val(), $("#bistate").val());
      $("#rifirstname").attr({ "disabled": "disabled" });
      $("#rimiddlename").attr({ "disabled": "disabled" });
      $("#rilastname").attr({ "disabled": "disabled" });
      $("#riaddress").attr({ "disabled": "disabled" });
      $("#riaddress2").attr({ "disabled": "disabled" });
      $("#ricompany").attr({ "disabled": "disabled" });
      $("#ripostalcode").attr({ "disabled": "disabled" });
      $("#riphone").attr({ "disabled": "disabled" });
      $("#rifax").attr({ "disabled": "disabled" });
      $("#ricountry").attr({ "disabled": "disabled" });
      $("#ristate").attr({ "disabled": "disabled" });
      $("#ricity").attr({ "disabled": "disabled" });
    }
    else {

      $("#rifirstname").removeAttr("disabled");
      $("#rimiddlename").removeAttr("disabled");
      $("#rilastname").removeAttr("disabled");
      $("#riaddress").removeAttr("disabled");
      $("#riaddress2").removeAttr("disabled");
      $("#ricompany").removeAttr("disabled");
      $("#ripostalcode").removeAttr("disabled");
      $("#riphone").removeAttr("disabled");
      $("#rifax").removeAttr("disabled");
      $("#ricountry").removeAttr("disabled");
      $("#ristate").removeAttr("disabled");
      $("#ricity").removeAttr("disabled");
    
    }
  });

  $("#login").click(function() {
    LoginAndExecute(function() {
      location.reload();
    }, "Sorry !");
  });

  $("#changePassword").click(function() {

    LoginAndExecute(function() {

      $.ajax({

        type: "POST",
        url: "/User/ChangePasswordPartial",
        data: "",

        success: function(result) {
          $("#popupContact").html(result);
          centerPopup();
          loadPopup();
        },
        error: function(result) {
          ////alert(result.status);
        }

      });

    }, "");

  });

  $("#myAccount").click(function() {
    LoginAndExecute(function() {
      window.location = "http://myaccount.officexml.net";
    }, "Sorry");

  });

});

function ChangePassword() {

  var currentPassword = $("#currentPassword").val();
  var newPassword = $("#newPassword").val();
  var confirmNewPassword = $("#confirmPassword").val();
  var captchaText = $("#captchaText").val();

  //alert("userName=" + userName + "&password=" + password + "&persistent=" + persistent);

  var formValid = true;
  var errorMessage = "";

  if (newPassword.length == 0) {
    formValid = false;
    $("#newPassword").addClass("registration_input_error");
    errorMessage += "<li>Please enter new password.</li>";
  }
  if (confirmNewPassword != newPassword) {
    formValid = false;
    errorMessage += "<li>New password and confirm new password should match.</li>";
  }
  if (currentPassword.length == 0) {
    formValid = false;
    $("#currentPassword").addClass("registration_input_error");
    errorMessage += "<li>Please enter your current password.</li>";
  }

  if (!formValid) {
    $(".registration_error").css({

      "display": "block"

    });

    //var errorMessageHeader = "<b>Please perform the following errors to proceed :<br><br></b>";
    //errorMessage = errorMessageHeader + errorMessage;
    $("#errorMessages").html(errorMessage);
    return false;
  }

  $.ajax({
    type: "POST",
    url: "/User/ChangePassword",
    data: "currentPassword=" + currentPassword + "&newPassword=" + newPassword + "&captchaText=" + captchaText,

    success: function(result) {
      var json = JSON2.parse(result);
      if (json.PasswordChanged == "false") {
        $(".registration_error").css({
          "display": "block"
        });

        $("#errorMessages").html("<li>"+json.ErrorMessage+"</li>");
      }
      else {
        $(".registration_error").css({
          "display": "block"
        });

        $("#errorMessages").html("<li>"+json.ErrorMessage+"</li>");
      }
    },
    error: function(result) {
      ////alert(result.status);
    }

  });
}

var FunctionToExecute;

function LoginAndExecute(functionToExecute, errorMessage) {
  FunctionToExecute = functionToExecute;
  var authJson;
  $.ajax({

    type: "POST",
    url: "/User/IsAuthenticated",
    data: "",

    success: function(result) {
      authJson = JSON2.parse(result);
      if (authJson.authenticated == "false") {

        $.ajax({

          type: "POST",
          url: "/User/ReturnLoginPartial",
          data: "",

          success: function(result) {
            $("#popupContact").html(result);
            centerPopup();
            loadPopup();
          },
          error: function(result) {
            ////alert(result.status);
          }

        });

      }
      else {
        functionToExecute();
      }

    },
    error: function(req, status, error) {
      //alert("Sorry !");
    }
  });
}

/*
Adjust the popup menu's position. The calculations are done in a way to 
make the popup load in the center of the page.
*/
var myWidth = 0, myHeight = 0;
myHeight = $(window).height();
myWidth = $(document).width();

var popupHeight = $("#login_form").height();
var popupWidth = $("#login_form").width();

//centering
$("#login_form").css({

  "top": myHeight / 2 - popupHeight / 2,
  "left": myWidth / 2 - popupWidth / 2
});


function Login() {

 var userName = $("#userName").val();
  var password = $("#Password").val();
  var captchaText = $("#captchaText").val();
  var persistent = true;

  //alert("userName=" + userName + "&password=" + password + "&persistent=" + persistent);

  var formValid = true;
  var errorMessage = "";

  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

  if (userName.length == 0) {
    formValid = false;
    $("#userName").addClass("registration_input_error");
    errorMessage += "<li>Enter email id.</li>";
  } else if (!pattern.test(userName)) {
    formValid = false;
    $("#userName").addClass("registration_input_error");
    errorMessage += "<li>Please enter a valid email id.</li>";
  }

  if (!formValid) {
    $(".registration_error").css({

      "display": "block"

    });

    //var errorMessageHeader = "<b>Please perform the following errors to proceed :<br><br></b>";
    //errorMessage = errorMessageHeader + errorMessage;
    $("#errorMessage").html(errorMessage);
    return false;
  }

  $(".registration_error").css({

    "display": "block"

  });

  $("#errorMessage").html("<li>You are being logged in. Please wait...</li>");

  $.ajax({
    type: "POST",
    url: "/User/LoginPartial",
    data: "userName=" + userName + "&password=" + password + "&persistent=" + persistent + "&captchaText=" + captchaText,

    success: function(result) {
      var json = JSON2.parse(result);
      if (json.login == "true")
        FunctionToExecute();
      else {
        $(".registration_error").css({

          "display": "block"

        });
        $(".registration_error").html(json.ErrorMessage);
      }
    },
    error: function(result) {
      ////alert(result.status);
    }

  });

}

function RegisterScreen() {

  //alert("register");
  //centering with css
  //centerPopup();
  //load popup
  loadPopup();

  //alert("ajax");

  $.ajax({

    type: "POST",
    url: "/User/ReturnRegisterPartial",
    data: "",

    success: function(result) {
    $("#popupContact").html(result);
    },
    error: function(result) {
      //alert(result.status);
    }
  });

}

function Register() {

  //alert("Register");
  var password = $("#password").val();
  var confirmPassword = $("#confirmpassword").val();
  var firstname = $("#firstname").val();
  var middlename = $("#middlename").val();
  var lastname = $("#lastname").val();
  var company = $("#company").val();
  var email = $("#email").val();
  var country = $("#country").val();
  var address = $("#address").val();
  var address2 = $("#address2").val();
  var securityquestion = "";
  var securityanswer = "";
  var city = $("#city").val();
  var state = $("#state").val();
  var postalcode = $("#postalcode").val();
  var phone = $("#phone").val();
  var fax = $("#fax").val();
  var captchaText = $("#captchaText").val();

  var formValid = true;
  var errorMessage = "";

  if (password != confirmPassword) {
    formValid = false;
    errorMessage += validNoImageString + " Password and confirm password should match.";

  }

  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

  if (email.length == 0) {
    formValid = false;
    $("#email").addClass("registration_input_error");
    errorMessage += validNoImageString + " Enter email id.<br>";
  } else if (!pattern.test(email)) {
    formValid = false;
    $("#email").addClass("registration_input_error");
    errorMessage += validNoImageString + " Please enter a valid email id.<br>";
  }

  if (password.length == 0) {
    formValid = false;
    $("#password").addClass("registration_input_error");
    errorMessage += validNoImageString + " Please enter a password.<br>";
  }
  else if (password.length < 6 || password.length > 20) {
    formValid = false;
    $("#password").addClass("registration_input_error");
    errorMessage += validNoImageString + " Password should be 6 to 20 characters long.<br>";
  }
  if (firstname.length == 0) {
    formValid = false;
    errorMessage += validNoImageString + " Please enter first name.<br>";
    $("#firstname").addClass("registration_input_error");
  }

  if (!formValid) {
    $(".registration_error").css({

      "display": "block"

    });

    //var errorMessageHeader = "<b>Please perform the following errors to proceed :<br><br></b>";
    //errorMessage = errorMessageHeader + errorMessage;
    $(".registration_error").html(errorMessage);
    return false;
  }

  //alert("sending request");
  var data = "email=" + email + "&password=" + password + "&confirmPassword=" + confirmPassword + "&captchaText=" + captchaText;
  //alert(data);
  var data1 = "&securityQuestion =" + securityquestion + "&securityAnswer=" + securityanswer + "&firstname=" + firstname;
  //alert(data1);
  var data2 = "&middlename=" + middlename + "&lastname=" + lastname + "&company=" + company + "&country=" + country;

  //alert(data2);
  var data3 = "&address=" + address + "&address2=" + address2 + "&city=" + city + "&state=" + state + "&postalcode=" + postalcode + "&phone=" + phone + "&fax=" + fax;

  //alert(data3);
  $(".registration_error").css({

    "display": "block"

  });
  $(".registration_error").html("Please wait...");
  $.ajax({

    type: "POST",
    url: "/User/RegisterPartial",
    data: data + data1 + data2 + data3,

    success: function(result) {
      var json = JSON2.parse(result);

      $(".registration_error").css({

        "display": "block"

      });

      if (json.AccountCreated == "") {

        //alert("");

      }

      $(".registration_error").html(json.Message);

    },
    error: function(result) {
      //alert(result.status);
    }

  });


}

function fillCountriesDropDown(dropDownId, selectedCountry) {

  $.ajax({

    type: "POST",
    url: "/User/GetCountries",
    data: "",

    success: function(result) {
    var json = JSON2.parse(result);
    $("#" + dropDownId).append("<option value='-1'>- Select Country -</option>");
      $(json).each(function() {
        if (this.Id == selectedCountry)
          $("#" + dropDownId).append("<option selected=selected value=" + this.Id + ">" + this.Name + "</option>");
        else
          $("#" + dropDownId).append("<option value=" + this.Id + ">" + this.Name + "</option>");
      });
    },
    error: function(result) {
      //alert(result.status);
    }
  });

}

function fillStatesDropDown(dropDownId, countryId, selectedState) {
  if (countryId == "-1")
    return;
  $("#" + dropDownId).html("");
  $("#" + dropDownId).append("<option value='-1'>- Select State -</option>");
  $.ajax({

    type: "POST",
    url: "/User/GetStates",
    data: "countryId=" + countryId,

    success: function(result) {
    var json = JSON2.parse(result);

    $("#" + dropDownId).removeAttr("disabled");
      $(json).each(function() {
      if (this.Id == selectedState) {
          $("#" + dropDownId).append("<option selected=selected value=" + this.Id + ">" + this.Name + "</option>");
        } else
          $("#"+dropDownId).append("<option value=" + this.Id + ">" + this.Name + "</option>");
      });
    },
    error: function(result) {
      //alert(result.status);
    }
  });

}

