﻿
function swapimage(newimg, imgw, imgh, divw, divh, txt) {
	imgw = (typeof(imgw) == 'undefined') ? 300 : imgw;
	imgh = (typeof(imgh) == 'undefined') ? 300 : imgh;
	divw = (typeof(divw) == 'undefined') ? 300 : divw;
	divh = (typeof(divh) == 'undefined') ? 300 : divh;
	txt = (typeof(txt) == 'undefined') ? '' : txt;
	
	$('#picdiv').fadeOut('slow', function() {
		$('#mainpic').attr('src', newimg).attr('alt', txt).css({'width' : imgw, 'height' : imgh});
		$('#txtdiv').html(txt);
		$('#mainpiclink').attr('href', newimg);
		$(this).css('width', divw).css('height', divh).fadeIn('slow');
	});
}

// Trim white space
function trim(s){
	return s.replace(/^\s*(.*?)\s*$/,"$1")
}

// Form validation
function checkcontents(tp,snd,req,arr){
	// Trim the sent value
	if(tp != 'checkbox') {
		snd = trim(snd.value);
	}
	
	// Required field?
	req = typeof(req) != 'undefined' ? req : false;
	
	// Check required field
	if(req && snd == "") {
		alert('Please fill in the required fields.');
		return false;
	}
	
	// Which type of check?
	switch (tp){
		case "text":
			if(snd != "") {
				var digits = /\d/; // at least one digit?
				if(digits.test(snd)) {
					alert('Please use only text, no numbers.');
					return false;
				}
			}
		break;
		case "checkbox":
			if(req) {
				var checked = false;
				for(var i = 0; i < arr.length; i++) {
				    if(document.getElementById(snd + '[' + arr[i] + ']').checked) {
				        checked = true;
				    }
				}
				if(!checked) {
					alert('Please check at least one item.');
					return false;
				}
			}
		break;
		case "number":
			if (snd != ""){
				var digitonly = /(^\-{0,1}\d+\.\d+$)|(^\-{0,1}\d+$)/;
				if (!digitonly.test(snd)){
					alert('Please use only numbers, no text.');
					return false;
				}
			}
		break;
		case "date":
			if (snd != ""){
				var dateformat = /^\d{1,2}(\-)\d{1,2}\1\d{4}$/;
				if (!dateformat.test(snd)){
					alert('Please enter a valid date (e.g. 01-01-2007).');
					return false;
				}
			}
		break;
		case "time":
			if (snd != ""){
				var timeformat = /^\d{1,2}(\:)\d{2}\1\d{2}$/;
				if (!timeformat.test(snd)){
					alert('Please enter a valid time (e.g. 11:25:45).');
					return false;
				}
			}
		break;
		case "email":
			if (snd != ""){
				var emailformat = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
				if (!emailformat.test(snd)){
					alert('Please enter a valid email address (e.g. example@domain.com).');
					return false;
				}
			}
 		break;
		case "zip":
			if (snd != ""){
				//var digitonly = /(^\d{5}$)|(^\d{5}\-\d{4}$)/;
				//if (!digitonly.test(snd)){
				//	alert('Please enter a valid zip code (e.g. 12345 or 12345-6789).');
				//	return false;
				//}
			}
		break;
		case "phone":
			if (snd != ""){
				//var phone = /^\d{3}\-\d{3}\-\d{4}$/;
				//if (!phone.test(snd)){
				//	alert('Please enter a valid phone number (e.g. 555-555-5555).');
				//	return false;
				//}
			}
		break;
		case "file":
			if (snd != ""){
				var file = snd.split(".");
				if (!(file[1] == 'doc' || file[1] == 'pdf' || file[1] == 'txt' || file[1] == 'docx' || file[1] == 'rtf')){
					alert('File types allowed include: .doc, .docx, .txt, .rtf, and .pdf.');
					return false;
				}
			}
		break;
	}
	return true;
}

