﻿// Standard Rules

var objUsername = ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
var objNumber = ".0123456789";
var objMoney = ".,0123456789";
var objWholeNumber = "0123456789";
var objPhone = "-()0123456789 ";
var objAlpha ="ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
var objUrl = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";

function isRule(oComp, sRule, nLength, fdecimal){
	if(fdecimal == "" || typeof(fdecimal) == "undefined"){
		fdecimal = false;
	}

	//If the object is not specified return false
	if (typeof(oComp) == 'undefined' || oComp == null || oComp == ''){
		alert('Error: Input object not specified.');
		return false;
	}
	//If neither rule nor max length is specified, return false
	else if (typeof(sRule) == 'undefined' && typeof(nLength) == 'undefined'){
		alert('Error: No rule/maximum length for input object specified.');
		return false;
	}

	var noErrorFlg = true;

	//If object is specified and either of rule is specified,
	if(typeof(sRule) != 'undefined' && sRule != null){
		var temp;
		sRule = sRule + "";
		var discardChars = false;
		if(sRule.length > 0 && sRule.charAt(0) == "~"){
			sRule = sRule.substring(1);
			discardChars = true;
		}

		if(typeof(oComp) == "undefined" || typeof(sRule) == "undefined")
			return false;

		for (var i = 0;i < oComp.value.length;i++){
			temp = oComp.value.charAt(i);

			if((!discardChars && sRule.indexOf(temp) == -1) || (discardChars && sRule.indexOf(temp) >= 0)){
				//alert("Field disobeys entry rule.  Following are the valid characters:\n" + sRule);
				//alert("Invalid Character!");
				oComp.value = oComp.value.substring(0,i);// + (oComp.value.length > i ? oComp.value.substring(i+1):"");
				noErrorFlg = false;
				break;
			}
		}
	}
	
	if(nLength){
		if(fdecimal){
			nLength -= fdecimal;
			var dp = oComp.value.indexOf(".");
			var p1;
			var p2 = "";
			if(dp >= 0){
				p1 = oComp.value.substring(0,dp);
				p2 = oComp.value.substring(dp+1);
			}
			else{
				p1 = oComp.value;
			}
			if(p1.length > nLength){
				oComp.value = oComp.value.substring(0,nLength);
				return noErrorFlg;
			}
			for(var i = 0;i < p2.length;i++){
				var ch = p2.charAt(i);
				if(ch < '0' || ch > '9'){
					oComp.value = p1 + "." + p2.substring(0,i);
					return noErrorFlg;
				}
			}
			if(p2.length > fdecimal){
				oComp.value = p1 + "." + p2.substring(0,fdecimal);
			}
		}
		else if(oComp.value.length > nLength){
			oComp.value = oComp.value.substring(0,nLength);
		}
	}
	return noErrorFlg;
}

function isRuleBlank(oComp, sRule, nLength, fdecimal){
	if(fdecimal == "" || typeof(fdecimal) == "undefined"){
		fdecimal = false;
	}

	//If the object is not specified return false
	if (typeof(oComp) == 'undefined' || oComp == null || oComp == ''){
		alert('Error: Input object not specified.');
		return false;
	}
	//If neither rule nor max length is specified, return false
	else if (typeof(sRule) == 'undefined' && typeof(nLength) == 'undefined'){
		alert('Error: No rule/maximum length for input object specified.');
		return false;
	}

	var noErrorFlg = true;

	//If object is specified and either of rule is specified,
	if(typeof(sRule) != 'undefined' && sRule != null){
		var temp; sRule = sRule + ""; var discardChars = false;
		if(sRule.length > 0 && sRule.charAt(0) == "~"){sRule = sRule.substring(1); discardChars = true;}

		if(typeof(oComp) == "undefined" || typeof(sRule) == "undefined") return false;

		for (var i = 0;i < oComp.value.length;i++){
			temp = oComp.value.charAt(i);
			if((!discardChars && sRule.indexOf(temp) == -1) || (discardChars && sRule.indexOf(temp) >= 0)){
				oComp.value = oComp.value.substring(0,i);// + (oComp.value.length > i ? oComp.value.substring(i+1):"");
				noErrorFlg = false; break;
			}
		}
	}
	if(nLength){
		if(fdecimal){
			nLength -= fdecimal; var dp = oComp.value.indexOf("."); var p1; var p2 = "";
			if(dp >= 0){p1 = oComp.value.substring(0,dp);p2 = oComp.value.substring(dp+1);}
			else{p1 = oComp.value;}
			if(p1.length > nLength){oComp.value = oComp.value.substring(0,nLength);return noErrorFlg;}
			for(var i = 0;i < p2.length;i++){
				var ch = p2.charAt(i);
				if(ch < '0' || ch > '9'){oComp.value = p1 + "." + p2.substring(0,i); return noErrorFlg;}
			}
			
			if(p2.length > fdecimal){oComp.value = p1 + "." + p2.substring(0,fdecimal);}
		}
		else if(oComp.value.length > nLength){
			oComp.value = oComp.value.substring(0,nLength);
		}
	}
	return noErrorFlg;
}
// This function will count character for text area.
function CharacterCount(obj, total){
	if(total == null) total = 500; var len = obj.value.length; var newdiv;
	
	if(document.getElementById("note" + obj.id) == null){
		newdiv = document.createElement('div');
		newdiv.id = "note" + obj.id;
	    obj.parentNode.appendChild(newdiv);
	}
	else{newdiv = document.getElementById("note" + obj.id);}
	
	if(len == 0){obj.parentNode.removeChild(newdiv);}
		
	if(len >= total){
		var temp = obj.value.substring(0, total)
		obj.value = temp;
		newdiv.innerHTML = "You have reached maximum characters limit of <b>" + total + "</b>."
	}
	else{
		newdiv.innerHTML = "Your maximum characters limit is: <b>" + total + "</b>. Current character count: <b>" + len + "</b>.";
	}
}
function isImage(obj){
    if (obj.value.length > 0){
        if (obj.value.length > 4){
            var tmpExt= obj.value.split('.');
            var ext = tmpExt[tmpExt.length-1].toLowerCase();
            if (ext == 'jpg' || ext == 'gif' || ext == 'png' || ext == 'jpeg'){return true;}
            else return false;
        }
        else return false;
    }
}
function ChangeState(pageurl,ddlcountry,objddlState,objtxtState,objrfvState,objrfvtxtState,objimgLoading){
    ddlcountry.change(function(){
        objimgLoading.show(); var Count = 0;
         while (document.getElementById(objddlState.attr('id')).length > 0) {
            document.getElementById(objddlState.attr('id')).options[0] = null;
        } 
        var myval = (jQuery("#"+ ddlcountry.attr('id') +" > option:selected").attr("value"));
        if (myval != 0){
            jQuery.getJSON(pageurl + '?CountryID=' + myval, function(options) {
                jQuery.each(options, function() {
                    objddlState.append(jQuery("<option></option>").val(this['ID']).html(this['StateName']));
                    Count++;
                });
                if (Count == 1){
                    objddlState.css("display", "none"); objtxtState.css("display", ""); objtxtState.val("");
                    if(objrfvtxtState != null)
                        ValidatorEnable(document.getElementById(objrfvtxtState.attr('id')), true);
                    if(objrfvState != null)
                        ValidatorEnable(document.getElementById(objrfvState.attr('id')), false);
                }
                else {
                    objddlState.css("display", ""); objtxtState.css("display", "none"); objtxtState.val("");
                    if(objrfvtxtState != null)
                        ValidatorEnable(document.getElementById(objrfvtxtState.attr('id')), false);
                    if(objrfvState != null)
                        ValidatorEnable(document.getElementById(objrfvState.attr('id')), true);
                }
            });
        }
        objimgLoading.hide();
    });
}
function IsValidZip(ZipCode){var regex = /^[\d]{5}$/;if (regex.test(ZipCode)) return true; else return false;}
function formatPhone(source, style){
    var realNumber = '';var pFormat = '';
	for(var i = 0; i < source.value.length; i++){
		if(isNaN(source.value.charAt(i)) == false && source.value.charAt(i) != ' '){realNumber += source.value.charAt(i);}
	}
	if(style == null) style = 'US';
	if(style == 'US'){
		for(var i = 0; i < realNumber.length; i++){
			switch (i){
				case 0: pFormat += "(" + realNumber.charAt(i); break;
				case 3: pFormat += ") " + realNumber.charAt(i); break;
				case 6: pFormat += "-" + realNumber.charAt(i); break;
				case 10: pFormat += "-" + realNumber.charAt(i); break;
				case 15: pFormat = pFormat; break;
				default: if(i < 15) pFormat += realNumber.charAt(i);break;
			}
		}
		if(arguments.length == 3 && arguments[2] == true && source.value.length > 14){pFormat = pFormat.substring(0,14) + ' Ext.' + pFormat.substring(15)}
	}
	else{
		for(var i = 0; i < realNumber.length; i++){
			switch (i){
				case 0: pFormat += "+" + realNumber.charAt(i); break;
				case 3: pFormat += "-" + realNumber.charAt(i); break;
				case 6: pFormat += "-" + realNumber.charAt(i); break;
				case 9: pFormat += "-" + realNumber.charAt(i); break;
				case 13: pFormat += "-(" + realNumber.charAt(i); break;
				case 17: pFormat += realNumber.charAt(i) + ")"; break;
				//case 18: pFormat = pFormat; break;
				default: if(i < 18){pFormat += realNumber.charAt(i);} break;
			}
		}
	}
	source.value = pFormat;
}
function IsDate(strDate){ //Format is mm/dd/yyyy
  var validformat=/^\d{2}\/\d{2}\/\d{4}$/;
  var monthfield=strDate.split("/")[0];
  var dayfield=strDate.split("/")[1];
  var yearfield=strDate.split("/")[2];
  var dayobj = new Date(yearfield, monthfield-1, dayfield);
  if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
    return false;
  else
    return true;
}
