var okcolor="#F3F3F3";
var errorcolor="#ff4e00"
var emailRe = "^([0-9a-zA-Z]+[-._+&amp;])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$";
var emailReNL = "^(([0-9a-zA-Z]+[-._+&amp;])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}((\r)?\n)?){1,5}$";
var phoneRe = "[\-0-9]{10,}";
var dateRe = "(19\\d\\d|2\\d\\d\\d)[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])";
var dateFRe = "(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19\\d\\d|2\\d\\d\\d)";
var futureDateRe = "(2\\d\d\d)[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])";
var dateTimeRe = "(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19\\d\\d|2\\d\\d\\d)\\s(0[1-9]|1[012]):([0-5][0-9])\\s(AM|PM)";

var cities = [];
var states = [];

var test_meteor = {
	json_query:function(){
		
	}
};

var hideTO;
$(
	function(){
		$("input[type='text'],input[type='textfield'],input[type='password'],input[type='file'],input[type='checkbox'],select,textarea").bind("focus",doFocus);
		$("input[type='text'],input[type='textfield'],input[type='password'],input[type='file'],input[type='checkbox'],select,textarea").bind('blur',doBlur);	
	}
);
function escapeRe(strIn){
	var strOut = strIn;
	strOut = strOut.replace(/\+/g,"\+");
	strOut = strOut.replace(/\$/g,"\$");
	strOut = strOut.replace(/\^/g,"\^");
	strOut = strOut.replace(/\*/g,"\*");
	strOut = strOut.replace(/\(/g,"\(");
	strOut = strOut.replace(/\)/g,"\)");
	strOut = strOut.replace(/\-/g,"\-");
	return strOut;
}
function max(el,length){
	if(el.value.length>length){
		el.value = el.value.substr(0,length);
	}
}
function addVal(id,pat,mes){
	if(pat=="checked"){
		$("input[name='"+id+"']:first").before('<input type="hidden" id="'+id+'" value="" />');
		$("input[name='"+id+"']").change(function(){
			if($("input[name='"+id+"']:checked").length>0){
				$("#"+id).val($("input[name='"+id+"']:checked").val());
			}
			else{
				$("#"+id).val("");
			}
		});
		$("#"+id).val($("input[name='"+id+"']:checked").val());
		pat=".+";
	}
	if(pat.indexOf('function')==0){
		$("#"+id).attr("valpattern",pat.replace("function()",''));
		$("#"+id).attr("valmessage",mes);
	}
	else{
		$("#"+id).attr("pattern",pat);
		$("#"+id).attr("message",mes);
	}
}
function validateEl(el){
	var errorMessage="";
	var str="";
	var isgood=true;
	var errors = [];
	var pattern=el.getAttribute('pattern');
	var valpattern=el.getAttribute('valpattern');
	var not=el.getAttribute('not');
	
	if(pattern!=null || valpattern!=null){
		var value=el.value;
		re=new RegExp(pattern,"g");
		var good=re.test(value);
		if(not!=null){
			if(value==not){
				good=false;	
			}
		}
		if(valpattern!=null){
			good=eval(valpattern);
		}
		if(!good){
			$(el).addClass("error");
			$(el).parent().parent().parent().find(".message").css("visibility","visible");
			isgood=false;
			var errMes=el.getAttribute('message');
			if(errMes!=null){
				errors.push(errMes);
				if($(el).attr("type")=="file"){
					$(el).val("");
				}
			}
		}
		else{
			$(el).removeClass("error");
			$(el).parent().parent().parent().find(".message").css("visibility","hidden");
		}
	}
	if(!isgood){
		//message("Please correct the following before proceeding:\r\n - "+errors.join("\r\n - "));
		return false;
	}
	else{
		//message("");
		return false;
	}
}
function validate(frm){
	var errorMessage="";
	var str="";
	var elements=frm.elements;
	var isgood=true;
	var errors = [];
	for(var i=0;i<elements.length;i++){
		var pattern=elements.item(i).getAttribute('pattern');
		var valpattern=elements.item(i).getAttribute('valpattern');
		var not=elements.item(i).getAttribute('not');
		var el = $(elements.item(i));
		if(pattern!=null || valpattern!=null){
			var value=elements.item(i).value;
			re=new RegExp(pattern,"g");
			var good=re.test(value);
			elements.item(i).rel="";
			if(not!=null){
				if(value==not){
					good=false;	
				}
			}
			var valgood=true;
			if(valpattern!=null){
				valgood=eval(valpattern);
			}
			if(!good || !valgood){
				$(el).addClass("error");
				$(el).parent().parent().parent().find(".message").css("visibility","visible");
				isgood=false;
				if(!good){
					var errMes=elements.item(i).getAttribute('message');
					if(errMes!=null){
						errors.push(errMes);
					}
				}
				if(!valgood){
					var errMes=elements.item(i).getAttribute('valmessage');
					if(errMes!=null){
						errors.push(errMes);
					}
				}
				if($(el).attr("type")=="file"){
					$(el).val("");
				}
			}
			else{
				$(el).removeClass("error");
				$(el).parent().parent().parent().find(".message").css("visibility","hidden");
			}
		}
	}
	if(!isgood){
		//message("Please address the following before proceeding:\r\n - "+errors.join("\r\n - "));
		return false;
	}
	else{
		//message("");
		return true;
	}
};
function message(saywhat){
	if(saywhat!=""){
		alert(saywhat);
	}
}
function emailOnly(el){
	if(/[^\+\-a-zA-Z0-9\._@\n]/.test(el.value)){
		el.value=el.value.replace(/[^\+\-a-zA-Z0-9\._@\r\n]/g,"")
	}
}
function only5(el){
	if(el.value.indexOf("\r\n")>-1){
		el.value = el.value.replace(/(\r\n){2,}/g,"\r\n");
		var ems = el.value.split("\r\n");
		if(ems.length>5){
			for(var i=ems.length;i>5;i--){
				ems.pop();
			}
		}
		$.each(ems,function(i,val){
			ems[i]=ems[i].replace(/\s/g,"");				
		});
		el.value=ems.join("\n");
		if(el.value.lastIndexOf("\n")==el.value.length-1){
			el.value = el.value.substr(0,el.value.length-1);

		}
		if(el.value.lastIndexOf("\n ")==el.value.length-2){
			el.value = el.value.substr(0,el.value.length-2);
		}
	}
	else if(el.value.indexOf("\n")>-1){
		el.value = el.value.replace(/\n{2,}/g,"\n");
		var ems = el.value.split("\n");
		if(ems.length>5){
			for(var i=ems.length;i>5;i--){
				ems.pop();
			}
		}
		$.each(ems,function(i,val){
			ems[i]=ems[i].replace(/\s/g,"");				
		});
		el.value=ems.join("\n");
		if(el.value.lastIndexOf("\n")==el.value.length-1){
			el.value = el.value.substr(0,el.value.length-1);
		}
		if(el.value.lastIndexOf("\n ")==el.value.length-2){
			el.value = el.value.substr(0,el.value.length-2);
		}
	}
}
function sar(el,strSearch,strReplace){
	var reSearch = new RegExp(strSearch,"g");
	if(reSearch.test(el.value)){
		el.value=el.value.replace(reSearch,strReplace)
	}
}
function $$(el){
	return document.getElementById(el);
}
function removeTags(el){
	if(/[<|>|=]/.test(el.value)){
		el.value=el.value.replace(/[<|>|=]/g,"")
	}
}
function limitLength(el,length){
	if(el.value.length>length){
		el.value=truncate(el.value,length);
	}
}
function truncate(val,length){
	if(val.length>length){
		return val.substring(0,length);
	}
	else{
		return val;
	}
}
String.prototype.trim = function(char) {
	return this.replace(new RegExp("^"+char+"+|"+char+"+$","g"),"");
}
function doFocus(){
	$(this).addClass("focus");
	if($(this).hasClass("error")){
		$(this).removeClass("error");
		$(this).addClass("haderror");
	}
}
function doBlur(){
	$(this).removeClass("focus");
	if($(this).hasClass("haderror")){
		$(this).removeClass("haderror");
		$(this).addClass("error");
	}
	validateEl(this);
}
function checkType(id,ext){
	var re = new RegExp("\."+ext+"$","gi");
	var good = re.test(id.value);
	if(!good && id.value!=""){
		id.value="";
		return false;
	}
	else{
		return true;	
	}
}
$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

var photoPop = {
	id:'',
	wa_id:'',
	title:'',
	photo_date:'',
	image_number:'',
	extension:'',
	width:0,
	height:0
}
$(function(){
	$(".imagePop").click(
		function(){
			photoPop = {
				id:$(this).attr("id").replace(/pop/g,""),
				wa_id:$(this).attr("waid"),
				title:$(this).attr("title"),
				photo_date:$(this).attr("date"),
				image_number:$(this).attr("imagenumber"),
				extension:'',
				width:parseInt($(this).attr("w")),
				height:parseInt($(this).attr("h"))
			};
			if($("#blanket").length==0){
				$("body").append('<div id="blanket"></div>');
			}
			if($("#podContainer").length!=0){
				$("#podContainer").remove();
			}
			$("body").append(''
				+'	<div id="podContainer" style="width:'+(photoPop.width+40)+'px;">'
				+'		<div class="margin">'
				+'			<div class="close">close</div>'
				+'			<div class="image" style="height:'+(photoPop.height)+'px"><img src="/media/images/photo_archive/'+photoPop.id+(photoPop.id.indexOf(".")==-1?".jpg":"")+'" alt="'+photoPop.title+'" /></div>'
				+'			<h2>'+photoPop.title+'</h2>'
				+'			<div class="info">'
				+(photoPop.photo_date=='' || photoPop.photo_date==undefined?'':'<div class="date">Date: '+photoPop.photo_date+'</div>')
				+(photoPop.wa_id=='' || photoPop.wa_id==undefined || photoPop.wa_id.toString()=="0"?'':'<div class="imageNumber">Image Number: '+photoPop.image_number+'</div>')
				+'			</div>'
				+'			<div class="moreInfo">'
				+(photoPop.wa_id=='' || photoPop.wa_id==undefined || photoPop.wa_id.toString()=="0"?"":'<a class="closePop" href="/photo_archive/search_the_photo_collection.php?search=id&id='+photoPop.wa_id+'&q='+photoPop.image_number+'#search">More information about this photo</a>*<br />')
				+'				<a class="closePop" href="/photo_archive/search_the_photo_collection.php?#search">Search the  Photo Archive</a>*'
				+'			</div>'
				+'			<div class="clear"></div>'
				+'			<div class="explain">* You will be prompted to agree  to our Terms & Conditions, which describes how images may be used for educational, public and private use.</div>'
				+'		</div>'
				+'	</div>'
			);
			$("#podContainer a.closePop").click(
				function(){
					$("#blanket").hide();
					$("#podContainer").hide();
				}
			);
			$("#podContainer").css("margin-left",-((photoPop.width/2)+20)+"px");
			centerPOD();
			showPOD();
			$("#podContainer .close, #blanket").click(
				function(){
					hidePOD();
				}
			);
			return false;
		}
	);
	$(window).resize(centerPOD);
});
function centerPOD(){
	$("#podContainer").css("top",Math.max(0,( $(document).scrollTop()+ ($(window).height()-$("#podContainer").height())/2) ));
}
function showPOD(){
	$("#blanket").fadeIn();
	$("#podContainer").fadeIn();
}
function hidePOD(){
	$("#blanket").fadeOut();
	$("#podContainer").fadeOut();
}
function scrollTween(x){
	$('html,body').animate({scrollTop: x}, 500);
}
function hideForms(){
	clearTimeout(hideTO);
	$(".popup .thanks").hide();
	$(".popup .form").show();
	$(".popup").hide();
}
hitTest = function(o, l){
	function getOffset(o){
		for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight};
			o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
		return r.r += r.l, r.b += r.t, r;
	}
	var a = arguments, j = a.length;
	j > 2 && (o = {offsetLeft: o, offsetTop: l, offsetWidth: j == 5 ? a[2] : 0,
	offsetHeight: j == 5 ? a[3] : 0, offsetParent: null}, l = a[j - 1]);
	for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j ? l = [l] : l).length; i;
		b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r))
		&& (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) && (r[r.length] = l[i]));
	return j ? !!r.length : r;
};
states.push('Alabama');
states.push('Alaska');
states.push('American Samoa');
states.push('Arizona');
states.push('Arkansas');
states.push('Baker Island');
states.push('California');
states.push('Colorado');
states.push('Connecticut');
states.push('Delaware');
states.push('District of Columbia');
states.push('Florida');
states.push('Georgia');
states.push('Guam');
states.push('Hawaii');
states.push('Howland Island');
states.push('Idaho');
states.push('Illinois');
states.push('Indiana');
states.push('Iowa');
states.push('Jarvis Island');
states.push('Johnston Atoll');
states.push('Kansas');
states.push('Kentucky');
states.push('Kingman Reef');
states.push('Louisiana');
states.push('Maine');
states.push('Maryland');
states.push('Massachusetts');
states.push('Michigan');
states.push('Midway Islands');
states.push('Minnesota');
states.push('Mississippi');
states.push('Missouri');
states.push('Montana');
states.push('Navassa Island');
states.push('Nebraska');
states.push('Nevada');
states.push('New Hampshire');
states.push('New Jersey');
states.push('New Mexico');
states.push('New York');
states.push('North Carolina');
states.push('North Dakota');
states.push('Northern Mariana Islands');
states.push('Ohio');
states.push('Oklahoma');
states.push('Oregon');
states.push('Palmyra Atoll');
states.push('Pennsylvania');
states.push('Puerto Rico');
states.push('Rhode Island');
states.push('South Carolina');
states.push('South Dakota');
states.push('Tennessee');
states.push('Texas');
states.push('Utah');
states.push('Vermont');
states.push('Virgin Islands');
states.push('Virginia');
states.push('Wake Island');
states.push('Washington');
states.push('West Virginia');
states.push('Wisconsin');
states.push('Wyoming');
states.push('Aguascalientes');
states.push('Baja California');
states.push('Campeche');
states.push('Chiapas');
states.push('Chihuahua');
states.push('Coahuila de Zaragoza');
states.push('Colima');
states.push('Distrito Federal');
states.push('Durango');
states.push('Guanajuato');
states.push('Guerrero');
states.push('Hidalgo');
states.push('Jalisco');
states.push('Mexico');
states.push('Michoacan de Ocampo');
states.push('Morelos');
states.push('Nayarit');
states.push('Nuevo Leon');
states.push('Oaxaca');
states.push('Puebla');
states.push('Queretaro de Arteaga');
states.push('Quintana Roo');
states.push('San Luis Potosi');
states.push('Sinaloa');
states.push('Sonora');
states.push('Tabasco');
states.push('Tamaulipas');
states.push('Tlaxcala');
states.push('Veracruz-Llave');
states.push('Yucatan');
states.push('Zacatecas');
states.push('Alberta');
states.push('British Columbia');
states.push('Manitoba');
states.push('New Brunswick');
states.push('Newfoundland');
states.push('Northwest Territories');
states.push('Nova Scotia');
states.push('Nunavut');
states.push('Ontario');
states.push('Prince Edward Island');
states.push('Quebec');
states.push('Saskatchewan');
states.push('Yukon Territory');
states.sort();


cities.push('Abilene');
cities.push('Akron');
cities.push('Albany');
cities.push('Albuquerque');
cities.push('Alexandria');
cities.push('Allentown');
cities.push('Amarillo');
cities.push('Anaheim');
cities.push('Anchorage');
cities.push('Ann Arbor');
cities.push('Arlington');
cities.push('Atlanta');
cities.push('Aurora');
cities.push('Austin');
cities.push('Bakersfield');
cities.push('Baltimore');
cities.push('Baton Rouge');
cities.push('Beaumont');
cities.push('Berkeley');
cities.push('Birmingham');
cities.push('Boise City');
cities.push('Boston');
cities.push('Bridgeport');
cities.push('Buffalo');
cities.push('Cedar Rapids');
cities.push('Charlotte');
cities.push('Chattanooga');
cities.push('Chesapeake');
cities.push('Chicago');
cities.push('Chula Vista');
cities.push('Cincinnati');
cities.push('Citrus Heights');
cities.push('Cleveland');
cities.push('Colorado Springs');
cities.push('Columbus');
cities.push('Concord');
cities.push('Corpus Christi');
cities.push('Dallas');
cities.push('Dayton');
cities.push('Denver');
cities.push('Des Moines');
cities.push('Detroit');
cities.push('Durham');
cities.push('East Los Angeles');
cities.push('El Monte');
cities.push('El Paso');
cities.push('Elizabeth');
cities.push('Erie');
cities.push('Escondido');
cities.push('Eugene');
cities.push('Evansville');
cities.push('Flint');
cities.push('Fort Lauderdale');
cities.push('Fort Wayne');
cities.push('Fort Worth');
cities.push('Fremont');
cities.push('Fresno');
cities.push('Fullerton');
cities.push('Garden Grove');
cities.push('Garland');
cities.push('Gary');
cities.push('Glendale');
cities.push('Glendale');
cities.push('Grand Rapids');
cities.push('Greensboro');
cities.push('Hampton');
cities.push('Hartford');
cities.push('Hayward');
cities.push('Hialeah');
cities.push('Hollywood');
cities.push('Honolulu');
cities.push('Houston');
cities.push('Huntington Beach');
cities.push('Huntsville');
cities.push('Independence');
cities.push('Indianapolis');
cities.push('Inglewood');
cities.push('Irvine');
cities.push('Irving');
cities.push('Jackson');
cities.push('Jacksonville');
cities.push('Jersey City');
cities.push('Kansas City');
cities.push('Knoxville');
cities.push('Lakewood');
cities.push('Lansing');
cities.push('Laredo');
cities.push('Las Vegas');
cities.push('Lexington-Fayette');
cities.push('Lincoln');
cities.push('Little Rock');
cities.push('Livonia');
cities.push('Long Beach');
cities.push('Los Angeles');
cities.push('Louisville');
cities.push('Lowell');
cities.push('Lubbock');
cities.push('Macon');
cities.push('Madison');
cities.push('Memphis');
cities.push('Mesa');
cities.push('Mesquite');
cities.push('Metairie');
cities.push('Miami');
cities.push('Milwaukee');
cities.push('Minneapolis');
cities.push('Mobile');
cities.push('Modesto');
cities.push('Montgomery');
cities.push('Moreno Valley');
cities.push('Nashville');
cities.push('New Haven');
cities.push('New Orleans');
cities.push('New York');
cities.push('Newark');
cities.push('Newport News');
cities.push('Norfolk');
cities.push('Oakland');
cities.push('Oceanside');
cities.push('Oklahoma City');
cities.push('Omaha');
cities.push('Ontario');
cities.push('Orange');
cities.push('Orlando');
cities.push('Overland Park');
cities.push('Oxnard');
cities.push('Paradise');
cities.push('Pasadena');
cities.push('Paterson');
cities.push('Peoria');
cities.push('Philadelphia');
cities.push('Phoenix');
cities.push('Pittsburgh');
cities.push('Plano');
cities.push('Pomona');
cities.push('Portland');
cities.push('Portsmouth');
cities.push('Providence');
cities.push('Raleigh');
cities.push('Rancho Cucamonga');
cities.push('Reno');
cities.push('Richmond');
cities.push('Riverside');
cities.push('Rochester');
cities.push('Rockford');
cities.push('Sacramento');
cities.push('Salem');
cities.push('Salinas');
cities.push('Salt Lake City');
cities.push('San Antonio');
cities.push('San Bernardino');
cities.push('San Diego');
cities.push('San Francisco');
cities.push('San Jose');
cities.push('Santa Ana');
cities.push('Santa Clarita');
cities.push('Santa Rosa');
cities.push('Savannah');
cities.push('Scottsdale');
cities.push('Seattle');
cities.push('Shreveport');
cities.push('Simi Valley');
cities.push('Sioux Falls');
cities.push('South Bend');
cities.push('Spokane');
cities.push('Springfield');
cities.push('Stamford');
cities.push('Sterling Heights');
cities.push('Stockton');
cities.push('St. Louis');
cities.push('St. Paul');
cities.push('St. Petersburg');
cities.push('Sunnyvale');
cities.push('Syracuse');
cities.push('Tacoma');
cities.push('Tallahassee');
cities.push('Tampa');
cities.push('Tempe');
cities.push('Thousand Oaks');
cities.push('Toledo');
cities.push('Topeka');
cities.push('Torrance');
cities.push('Tucson');
cities.push('Tulsa');
cities.push('Vallejo');
cities.push('Virginia Beach');
cities.push('Waco');
cities.push('Warren');
cities.push('Washington');
cities.push('Waterbury');
cities.push('Wichita');
cities.push('Winston-Salem');
cities.push('Worcester');
cities.push('Yonkers');