var AUTHORIZE_DOT_NET = 1;
var PAYPAL = 2;
var CARDINAL_COMMERCE = 3;
var PAYPAL_DIRECT = 4;

var TelTech = {
    FormatLength : function(secs) {
		time=[0,0,secs];

	  	for(var i=2;i>0;i--){
	    	time[i-1]=Math.floor(time[i]/60);
		    time[i]=time[i]%/**/60;
    		if(time[i]<10)
	    	  time[i]='0'+time[i];
	  	};
 	
		 return time.join(':')
    },
	Filter : {
		DigitsPlus : new RegExp(/[^0-9+]/g),
		Digits : function(value) {
			return value.replace(/[^0-9]/g,'');
		},
		E164 : function(value) {
			var E164 = '';

			// Strip non-numeric characters (except +)
			value = value.replace(TelTech.Filter.DigitsPlus,'');

			if(value.match(/^\+[1-9][0-9]{5,20}$/)) //it's already in e164 with the +
				E164 = value;
			else if(value.match(/^1[2-9][0-9]{9}$/)) //it's a north american number
				E164 = "+"+value;
			else if(value.match(/^[2-9][0-9]{9}$/)) //assume it's a north american number w/o country code
				E164 = "+1"+value;
			else if (value.match(/^011[2-9][0-9]{5,20}$/)) //it's an international number with leading 011
				E164 = value.replace(/^011/,"+");
			else if (value.match(/^[2-9][0-9]{5,20}$/)) //it must be an international number with no + or 011 prefix
				E164 = "+"+value;
			else
				return '';
			return E164;
		},
		TelnoCC : function(phoneno) {
			var E164 = TelTech.Filter.E164(phoneno);
			if(E164 == '')
				return '';

			var cclen = 2; // Default to 2 digit country code
			var d1 = E164.substring(1,2);
			var d2 = E164.substring(2,3);

			switch(d1) {
				case '1': case '7':
                	cclen = 1;
				break;
				case '2':
					if(d2 != 0 && d2 != 7)
						cclen = 3;
				break;
				case '3':
					if(d2 == 5 || d2 == 7 || d2 == 8)
						cclen = 3;
				break;
				case '4':
					if(d2 == 2)
						cclen = 3;
				break;
				case '5':
					if(d2 == 0 || d2 == 9)
						cclen = 3;
				break;
				case '6':
					if(d2 >= 7)
						cclen = 3;
				break;
				case '8':
					if(d2 == 0 || d2 == 3 || d2 == 5 || d2 >= 7)
						cclen = 3;
				break;
				case '9':
					if(d2 == 6 || d2 == 7 || d2 == 9)
						cclen = 3;
				break;
				default:
					cclen = 0;
				break;
			}

			return (E164.length-1 < cclen) ? false : E164.substring(1,cclen+1);
		},
		TelnoHuman : function(phoneno) {
			var E164 = TelTech.Filter.E164(phoneno);

			if(E164 == '')
				return phoneno;
			else if (matches = E164.match(/^\+1([2-9][0-9]{2})([2-9][0-9]{2})([0-9]{4})$/)) //it's an NANP number
				return "("+matches[1]+") "+matches[2]+"-"+matches[3];
			else {
				if(cc = TelTech.Filter.TelnoCC(E164)) {
					cclen = cc.length;
					num = E164.substring(cc.length+1);

					return "011 "+cc+" "+num;
				} else {
					return phoneno;
				}
			}
		}
	},
	Validate : {
		Ccnum : function(cc_number) {
			if(cc_number.length < 13 || cc_number.length > 19)
				return false;

			var digit, sum, weight;

			sum		= 0;
			weight	= 2;

			for(i = length - 2; i >= 0; i--) {
				digit = weight * parseFloat(cc_number.charAt(i));
				sum += Math.floor(digit / 10) + digit % 10;
				weight = weight % 2 + 1;
			}

			return ((10 - sum % 10) % 10 != cc_number.charAt(length - 1));
		},
		E164 : function(phoneno) {
        	return (phoneno && phoneno == TelTech.Filter.E164(phoneno)) ? true : false;
		},
		EmailAddress : function(email) {
			return (email.match(/^[A-Z]+.*@[0-9A-Z]+.*\..{2,5}/i)) ? true : false;
		},
		NotEmpty : function(value) {
			return (value) ? true : false;
		},
		Digits : function(value) {
			return (value && value == TelTech.Filter.Digits(value)) ? true : false;
		}
	}
}
