﻿
//Startup Code
$().ready(function() {
	vldRequiredMark = '<span style="color:red">*</span>';
	vldRequiredClass= 'lblRequired';

	setValidationClasses();
	setValidationLabels();
	$("#msiForm").validate();
	$("#msiGuid").val(guid());
	
});


//Setup any custom validation labels
function setValidationLabels() {
	$("."+vldRequiredClass).append(vldRequiredMark);
	return false;
}

//Setup all validation custom classes
function setValidationClasses(){
	$.validator.addMethod("notNone",
	function(value,element) {
		var r = true;
		var isvisible = $(element).is(':visible');
		if (isvisible && value == 'none'){
			r = false;
		}
		return r; 
	}
	,"Selection Required");
	

	$.validator.addMethod("CCExpDateCheck",
		function(value,element) {
			var r = true;
			var isvisible = $(element).is(':visible');
			var regex = /^\d{3,4}\.?\d{0}$/;
			if (isvisible && value.length > 0)
			{
				if (value.search(regex) ==-1) {
					r = false;
				  } else {
					r = true;
				  }
			}
			
			return r; 
		}
		,"Must be Format MMYY");
	$.validator.addMethod("phoneUS",
		function(phone_number, element) {
			var isvisible = $(element).is(':visible');
			var r = true;
			phone_number = formatPhone(phone_number);
			element.value = phone_number;
			if (phone_number.length > 0 && isvisible && phone_number.length != 12) {
				r = false;
			}
			return r;
		},
		"Please specify a valid phone number");

	$.validator.addClassRules({
		vldEmail: {
			email: function(element) { return $(element).is(':visible');}
		},
		vldPhoneUS: {
			phoneUS: function(element) { return $(element).is(':visible');}
		},
		vldDate: {
			date: function(element) { return $(element).is(':visible');}
		},
		vldDecimal: {
			NumericWithDecimal: function(element) { return $(element).is(':visible');}
		},
		vldCurrency: {
			NumericWithDecimal: function(element) { return $(element).is(':visible');},
			greaterThanZero: function(element) { return $(element).is(':visible');}
		},
		vldCreditCard: {
			creditcard: function(element) { return $(element).is(':visible');}
		},
		vldCCExp: {
			vldCCExpCheck: function(element) { return $(element).is(':visible');}
		},
		vldNumeric: {
			NumericOnly: function(element) { return $(element).is(':visible');}
		},
		vldCCExpDate: {
			CCExpDateCheck: function(element) { return $(element).is(':visible');}
		},
		vldSelect:{
			required: function(element) { return $(element).is(':visible');},
			notNone: function(element) { return $(element).is(':visible');}
		},
		vldRequired:{
			required:  function(element) { return $(element).is(':visible');}
		},
		vldUsZip:{
			digits:true,
			minlength:5,
			maxlength:5
		}
	});
}

/* Supporting Functions */
function formatPhone(s) {
	var iLen = s.length;
	var r = '';
	var c = '';
	for (var i = 0; i< iLen; i++) {
		c = s.substring(i,i+1);
		if (!isNaN(c)) {
			r += c;
		}
	}
	if (r.length == 10) {
		r = r.substring(0,3)+"."+r.substring(3,6)+"."+r.substring(6);
	}
	return r;
}

function getToday() {
	var d = new Date();
	return d.getMonth() + "/" + d.getDate() + "/" + getFullYear();
}

function isValidDate(strValue)
{
	var strSeparator='';
	for(var i=0;i<strValue.length;i++){
		strSeparator=strValue.charAt(i);
		if(isNaN(strSeparator)){
			break;
		}
	}
	var arrayDate = strValue.split(strSeparator); //split date into month, day, year
	var intMonth = parseInt(arrayDate[0],10);
	var intDay = parseInt(arrayDate[1],10);
	var intYear = parseInt(arrayDate[2],10);
    //alert("month="+intMonth+"day="+intDay+"year="+intYear);
	if (intMonth > 12) { return false; } //if month is invalid quit    
	//create a lookup for months not equal to Feb.
	var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
						'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
	if (parseInt(intMonth) < 10)
	{
		intMonth = '0' + intMonth;
	}
    
	//check if month value and day value agree
	if(arrayLookup[intMonth] != null) {
	  if(intDay <= arrayLookup[intMonth] && intDay != 0)
		return true; //found in lookup table, good date
	}    
	//check for February    
	if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
	  return true; //Feb. had valid number of days
	return false;
}


//Add or remove required class to field and handle label
function msiAddRequired(sId) {
	$('#'+sId).addClass('vldRequired');
	var oLbl = $("label[for='" + sId + "']");
	if (oLbl != null) {
		var sLabel = oLbl.html();
		if (sLabel.indexOf(vldRequiredMark) <= 0) {
			sLabel += vldRequiredMark;
		}
		oLbl.html(sLabel);
		oLbl.addClass(vldRequiredClass);
	}
	return false;
}
function msiRemoveRequired(sId) {
	$('#'+sId).removeClass('vldRequired');
	var oLbl = $("label[for='" + sId + "']");
	if (oLbl != null) {
		var sLabel = oLbl.html();
		if (sLabel.indexOf(sMark) > 0) {
			sLabel = sLabel.substring(0,indexOf(sMark));
		}
		oLbl.html(sLabel);
		oLbl.removeClass(vldRequiredClass);
	}
	return false;
}

//Generate pseudo-GUIDs
function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
