var validate = {
	
	string:  function(field, field_title, limit){
	
		if(typeof(field.value) == 'undefined' || field.value.trim() == ''){
			this.message(field, field_title + ' cannot be blank');
			return false;
		}
		
		if(field.value.length > limit){
			this.message(field, field_title + ' cannnot exceed ' + limit + ' characters');
			return false;
		}
			
		return true;
	},
	
	number:  function(field, field_title){
	
		if(typeof(field.value) == 'undefined' || field.value == ''){
			this.message(field, field_title + ' cannot be blank');
			return false;
		}
		
		if(field.value==0){
			this.message(field, field_title + ' cannot be 0');
			return false;
		}
		
		if(/^\d+$/.test(field.value) == false){
			this.message(field, field_title + 'is invalid');
			return false;
		}
			
		return true;
	},
	
	price: function(field, field_title, limit){
	
		if(typeof(field.value) == 'undefined' || field.value == ''){
			this.message(field, field_title + ' cannot be blank');
			return false;
		}
		
		if(field.value==0){
			this.message(field, field_title + ' cannot be 0');
			return false;
		}
		
		if(!isNaN(field.value)){
			if(field.value.indexOf('.') != -1){
				if(/^\d+\.\d{2}$/.test(field.value) == false && /^\.\d{2}$/.test(field.value) == false){
					this.message(field, field_title + ' is invalid');
					return false;
				}
			}
			
		}else{
			this.message(field, field_title + ' is invalid');
			return false;
		}
			
		var price_limit = (typeof(limit)=='undefined' || limit==null) ? 0 : limit;
			
		if(field.value <= price_limit){
			this.message(field, field_title + ' must be at least ' + price_limit);
			return false;
		}
		
		return true;
			
	},
	
	postalcode: function (field, field_title){
		
		if(field.value.length !=5){
			this.message(field, field_title + 'is invalid');
			return false;
		}
		
		if(/^\d+$/.test(field.value) == false){
			this.message(field, field_title + 'is invalid');
			return false;
		}
	},
	
	email: function(field, field_title){
		//var str = ;
		//email syntax is valid
		if(/^[^0-9][A-z0-9_\-.]+[@][A-z0-9_-]+([.][A-z0-9_-]+)*[.][A-z]{2,4}$/.test(field.value) == false){
		//if( !str.match( field.value ) ){
			this.message(field, field_title + ' is invalid');
			return false;
		}
		
		return true;
	},
	
	select: function(field, field_title){
		select_value = field.options[field.selectedIndex].value;
		if(select_value == '0' || select_value == ''){
			this.message(field, 'Please select a ' + field_title);
			return false;
		}
		return true;
	},
	
	
	date: function(field, field_title){
	
		var dtCh = "/";
		var dtStr = field.value;
		var daysInMonth = this.DaysArray(12);
		var pos1 = dtStr.indexOf(dtCh);
		var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
		var strMonth = dtStr.substring(0, pos1);
		var strDay = dtStr.substring(pos1 + 1, pos2);
		var strYear = dtStr.substring(pos2 + 1);
		
		var strYr = strYear;
		
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
		
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		
		var month = parseInt(strMonth);
		var day = parseInt(strDay);
		var year = parseInt(strYr);
		
		if (pos1==-1 || pos2==-1){
			this.message(field, field_title + ' format should be : mm/dd/yyyy');
			return false;
		}
		if (strMonth.length<1 || month<1 || month>12){
			this.message(field, field_title + ' format should be : mm/dd/yyyy');
			return false;
		}
		if (strDay.length < 1 || day < 1 || day > 31 || (month==2 && day > this.daysInFebruary(year)) || day > daysInMonth[month]){
			this.message(field, field_title + ' contains a date in February that does not exist');
			return false;
		}
		if (strYear.length != 4 || year==0 || year<1900 || year>2100){
			this.message(field, field_title + ' format should be : mm/dd/yyyy');
			return false;
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || this.isInteger(this.stripCharsInBag(dtStr, dtCh))==false){
			this.message(field, field_title + ' format should be : mm/dd/yyyy');
			return false;
		}
		
		return true
	
	},
	
	stripCharsInBag: function(s, bag){
		var i;
	    var returnString = "";
	    // Search through string's characters one by one.
	    // If character is not in bag, append to returnString.
	    for (i = 0; i < s.length; i++){   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) returnString += c;
	    }
	    
	    return returnString;
	},
	
	daysInFebruary: function(year){
		// February has 29 days in any year evenly divisible by four,
	    // EXCEPT for centurial years which are not also divisible by 400.
	    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	},
	
	DaysArray: function(n) {
		var day_arr = new Array();
		for (var i = 1; i <= n; i++) {
			day_arr[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {day_arr[i] = 30}
			if (i==2) {day_arr[i] = 29}
	   }
	   return day_arr;
	},
	
	isInteger: function(s){
	
		var i;
	    for (i = 0; i < s.length; i++){   
	        // Check that current character is number.
	        var c = s.charAt(i);
	        if (((c < "0") || (c > "9"))) return false;
	    }
	    // All characters are numbers.
	    return true;
	},
	
	message: function(field, msg){

		if(typeof(field) != 'undefined' && field != null){
			field.focus();
		}
		//TODO: custom dialog
		alert(msg);
	}
}
