// This secion will preload the images for rollovers.
if(document.images){
	marketing = new Image();
	marketing.src = 'images/Top1M.gif';
	marketing2 = new Image();
	marketing2.src = 'images/Top2M.gif';
	education = new Image();
	education.src = 'images/Top1E.gif';
	education2 = new Image();
	education2.src = 'images/Top2E.gif';
	resources = new Image();
	resources.src = 'images/Top1R.gif';
	resources2 = new Image();
	resources2.src = 'images/Top2R.gif';
	login = new Image();
	login.src = 'images/Top1L.gif';
	login2 = new Image();
	login2.src = 'images/Top2L.gif';
	communicator = new Image();
	communicator.src = 'images/Top1C.gif';
	communicator2 = new Image();
	communicator2.src = 'images/Top2M.gif';
	postcard = new Image();
	postcard.src = 'images/Top1P.gif';
	postcard2 = new Image();
	postcard2.src = 'images/Top2M.gif';

}

function swap(img){
	// This function will swap images
	if(img == 'education'){
		document.getElementById('one').src = education.src;
		document.getElementById('two').src = education2.src;
	}
	else if(img == 'marketing'){
		document.getElementById('one').src = marketing.src;
		document.getElementById('two').src = marketing2.src;
	}
	else if(img == 'resources'){
		document.getElementById('one').src = resources.src;
		document.getElementById('two').src = resources2.src;
	}
	else if(img == 'login'){
		document.getElementById('one').src = login.src;
		document.getElementById('two').src = login2.src;
	}
	else if(img == 'communicator'){
		document.getElementById('one').src = communicator.src;
		document.getElementById('two').src = communicator2.src;
	}
	else if(img == 'postcard'){
		document.getElementById('one').src = postcard.src;
		document.getElementById('two').src = postcard2.src;
	}
}

function validate_common(){
	var frm = document.thisForm;
	if(isEmpty(frm.FName, 'First Name'))
		return false;
	if(isEmpty(frm.LName, 'Last Name'))
		return false;
	if(isEmpty(frm.Company, 'Company'))
		return false;
	if(!isEmail(frm.Email, 'Email'))
		return false;
	if(isEmpty(frm.Phone, 'Phone Number'))
		return false;
	if(isEmpty(frm.County, 'County'))
		return false;
	return true;
}

function isEmpty(fld, txt){
	if(fld.value == '' || fld.value == ' '){
		alert(txt + ' is a required field.\n\rPlease enter it and try again.');
		fld.focus();
		return true;
	}
	return false;
}

function isEmail(fld, txt){
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(fld.value))
		return true;
	alert(txt + ' is an invalid email.\n\rPlease check it and try again.');
	fld.focus();
	return false;

}

function isCurrency(txtfld, msg) {
	var strfld = parseFloat(clean(txtfld.value));
	if (isNaN(strfld)) {
		if (msg != "") {
			alert(msg + " has invalid characters.  Rectify and Try again.");
			txtfld.select();
			txtfld.focus();
		}
		return false;
	}
	txtfld.value = FmtMoney(strfld,2);
	return true;	
}

function replace(s, F, R) {
	var find = 0;
	var start = 0
	while (find != -1) {
		find = s.indexOf(F, start);
		if (find != -1) {
			s = s.substring(0,find) + R + s.substring(find + F.length);
			start = find + R.length;
		}
	}
	return s;
}

function trim(s) {
	return s.replace(/(^\s*)|(\s*$)/g, "");
}

function clean(s) {
	if (!s) return s;
	s = replace(s, '$', '');
	s = replace(s, ',', '');
	s = replace(s, '%', '');	
	return s;
}

function FmtMoney(A,D) {
	N=Math.abs(Math.round(A*100));
	S=((N<10)?"00":((N<100)?"0":""))+N;
	S=((A<0)?"-":"")+"$"+WGgroup(S.substring(0,(S.length-2))) + 
	      ((D>0)?"."+S.substring((S.length-2),S.length):"");
	return S;
}

function isDate(txtFld, msg) {
	var str=txtFld.value
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
	var matchArray = str.match(datePat); // is the format ok?
	if (matchArray == null) {
		alert("The Date value in " + msg + " is not in a valid format.")
		txtFld.select();
		txtFld.focus();
		return false;
	}
	var month = matchArray[1]; 
	var day = matchArray[3];
	var year = matchArray[5];
	if (month < 1 || month > 12) { 
		alert("The Month in " + msg + " must be between 1 and 12.");
		txtFld.select();
		txtFld.focus();
		return false;
	}
	if (day < 1 || day > 31) {
		alert("The Day in " + msg + " must be between 1 and 31.");
		txtFld.select();
		txtFld.focus();
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
		txtFld.select();
		txtFld.focus();
		return false
	}
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
			txtFld.select();
			txtFld.focus();
			return false;
		}
	}
	return true;
}

function WGgroup(S) {
	return (S.length<4)?S:(WGgroup(S.substring(0,S.length-3))+","+S.substring(S.length-3,S.length));
}
