/**
 * General javascript for Customer Portal
 */

//JQuery Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

jQuery(document).ready(function() {
  jQuery("form").keypress(function (event) {
	if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
		jQuery(".fireOnEnter").click();
	}
  });
  jQuery("#btnNext").click(function(event) {
	jQuery("#enabled").hide();
	jQuery("#disabled").show();
    jQuery("#mainForm").submit();
  });

  jQuery("#btnBack").click(function(event) {
	jQuery("#enabled").hide();
	jQuery("#disabled").show();
	jQuery('<input />').attr('type', 'hidden').attr('name', 'back').attr('value', 'true').appendTo('#mainForm');
	jQuery("#mainForm").submit();
  });

  jQuery("#btnCancel").click(function(event) {
	jQuery("#enabled").hide();
	jQuery("#disabled").show();
  });

  jQuery('#IdLogout').click(function()
  {
	var target = jQuery('#confirmLogout');
	target.show();
	if(!target.dialog('isOpen'))
	{
	  target.dialog({title:"Continue with the Logout?",autoOpen:false,bgiframe:true,modal:true,resizable:false,width:300,height:150,position:['center', 'center'],draggable:false})
	};
	target.dialog('open');
	jQuery('#logoutYes').blur();
	jQuery('#logoutNo').blur();
  });

  jQuery('#logoutYes').click(function() {
	var target = jQuery('#confirmLogout');
	target.hide();
	target.dialog('close');
	location.replace('logout.html');
  });

  jQuery('#logoutNo').click(function() {
	var target = jQuery('#confirmLogout');
	target.hide();
	target.dialog('close');
  });

  getEnrollmentStageRequest();
});

//Java script Functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>

function displaySection(object) {
	var objectToDisable = "#owner_two";
	if ("Joint Credit" == object.value) {
		jQuery(objectToDisable).show('fast');
		jQuery('#excludeValidate').val('');
	} else {
		jQuery(objectToDisable).hide();
		jQuery('#excludeValidate').val('Two');
	}
}

function sliderInit() {
  featuredcontentslider.init({
    id: "slider1",  //id of main slider DIV
    contentsource: ["inline", ""],  //Valid values: ["inline", ""] or ["ajax", "path_to_file"]
    toc: "#increment",  //Valid values: "#increment", "markup", ["label1", "label2", etc]
    nextprev: ["", ""],  //labels for "prev" and "next" links. Set to "" to hide.
    revealtype: "click", //Behavior of pagination links to reveal the slides: "click" or "mouseover"
    enablefade: [true, 0.1],  //[true/false, fadedegree]
    autorotate: [true, 3000],  //[true/false, pausetime]
    onChange: function(previndex, curindex){  //event handler fired whenever script changes slide
      //previndex holds index of last slide viewed b4 current (1=1st slide, 2nd=2nd etc)
      //curindex holds index of currently shown slide (1=1st slide, 2nd=2nd etc)
    }
  });
}

/**
 * Select Cuts Off Options for IE Fix
 */
function selectIECutOff(selectObj) {
	if(jQuery.browser.msie){
		var origWidth = jQuery(selectObj).css("width");
		jQuery(selectObj)
			.mousedown(function(){
				jQuery(this).css("width", "auto");
			})
			.blur(function(){
				jQuery(this).css("width", origWidth);
			})
			.change(function(){
				jQuery(this).css("width", origWidth);
			});
	}
}


/**
 * Load the select object using AJAX
 * @param [required] selectObj, select html object
 * @param [required] currentID, the id of the current field
 * @param [required] model, the model of the JSON
 * @param [required] url, the AJAX URL
 * @param [optional] paramID, the parameter of the URL
 * @param [optional] parentID, the id of the parent
 */
function ajax_loadSelect(selectObj, currentID, model, url, paramID, parentID, viewMode) {
	var isViewMode = "";
  if ((parentID == undefined) || (parentID == '')) {
		loadingAction(selectObj, 'Select an Item...');
	} else {
	  isViewMode = (viewMode == undefined || viewMode == '') ? 'false' : viewMode;
		loadingAction(selectObj, 'Loading...');
		jQuery.getJSON(url + jQuery.query.set(paramID, parentID).toString(),
			function(json) {
	          // Parsing JSON strings can throw a SyntaxError exception, so we wrap the call
	          // in a try catch block
	          try {
	            var optSelected = '';
	            var options = '<option value="">Select an Item...</option>';
	            if (isViewMode == 'false') {
	           	jQuery(selectObj).removeAttr('disabled');
	            }
	            jQuery(json['linked-hash-map'].entry).each(function() {
	              if (currentID == this[model][1]) {
	              optSelected = 'selected="selected"';
	              } else {
	              optSelected = '';
	              }
	              options += '<option ' + optSelected + ' value="' + this[model][0]
	              + '">' + this[model][1] + '</option>';
	            });
	            jQuery(selectObj).html(options);
	          } catch (e) {
	            /*Display the null return*/
	        	loadingAction(selectObj, 'Select an Item...');
	          }
	        }
		);
	}

	function loadingAction(obj, message) {
		jQuery(obj).attr('disabled', 'disabled');
		jQuery(obj).html('<option value="" selected="selected">' + message + '</option>');
	}
}

/**
 * Check the Json Response
 * @param [required] data, json data value
 */
function checkJSON (data) {
    var response;
    if (window.execScript) {
      data = 'var json = ' + data;
      window.execScript(data);
      response = json;
    } else {
      response = JSON.parse(data);
    }
    return response;
  }

/**
 * Return the enrollment stage
 */
function getEnrollmentStageRequest() {
  jQuery.getJSON('json_enrollment.html?action=enrollmentStageRequest', function(jsonResponse) {
	try {
		if ((jsonResponse.string === undefined) || (jsonResponse.string == "")) {
			jQuery('.loadingInfo').hide();
		} else {
			jQuery('.loadingInfo').corner();
			jQuery('.loadingInfo').show();
			jQuery('.loadingInfo > span').html(jsonResponse.string);
		}
	} catch (e) {
		jQuery('.loadingInfo').hide();
	}
  });
}
