// Given a form field object, returns a message to display when the field failed validation. If the
// field has the valid_message attribute, it is returned as is. Else, if the valid attribute is present, it
// is inspected and a message is created based on the type of validation. Otherwise, an empty string is returned.
function ValidationMessage(field)
{
    var msg = $(field).attr('valid_message') || '';
    if (msg.length == 0)
    {
        var validator = $.trim($(field).attr('valid'));
        
        // see if the field was optional
        if (validator.charAt(0) == '(' && validator.charAt(validator.length-1) == ')')
            validator = validator.substr(1, validator.length-2);
            
        if (validator.substr(0,4) == 'num:')
        {
			var range = validator.substring(4).split(' ');
			var lo = range[0];
			var hi = range[1];
            msg = 'Please enter a number between ' + lo + ' and ' + hi;
        }
        else if (validator == 'phone')
            msg = 'Please enter a valid phone number (nnn-nnn-nnnn)'
		else if (validator == 'password')
		    msg = 'Passwords need to be at least 8 characters long and must include at least one digit (0-9)'
		else if (validator == 'posnum')
			msg = 'Please enter a number greater than 0.'
		else if (validator == 'email')
		    msg = 'Please enter a valid email address 50 characters or less.'
		else if (validator == 'notblank')
		    msg = 'Please enter a value.'
		else if (validator.substr(0, 7) == 'string:')
		{
			var range = validator.substring(7).split(' ');
			var lo = range[0];
			var hi = range[1];
			if (lo == hi)
			    msg = 'Please enter a value ' + lo + ' characters long.';
			else
			    msg = 'Please enter no more than ' + hi + ' characters.';
		}
		else if (validator.substr(0, 8) == 'matches:')
		{
			var otherName = validator.substring(8);
			msg = 'This field must match the ' + otherName + ' field.';
		}
		else if (validator.substr(0, 8) == 'hostname')
		{
		    msg = 'This value must be valid for use in domain names.';
		}
    }
    return msg;
}

// Validates a form based on attributes of form inputs. If all fields validate, returns true. Otherwise,
// displays an error message, gives the first erroneous field focus, and returns false.
// Validation happens by checking
// each input - any input with a valid="" attribute is checked. The values currently supported are:
// 'num:m n' - input must be a number between m and n, inclusive
// 'phone' - input must appear to be a phone number
// 'posnum' - input must be a positive number.
// 'notblank' - input must not be blank.
// 'email' - input must appear to be a valid email address
// 'string:m n' - input must be a string between m and n characters long, inclusive
// 'matches:name' - this field's value must match the value of a field named 'name'
// 'password' - qualifies under our password policy - >= 8 chars, at least 1 is a digit
// If the validator is in parentheses, it means that the field is optional. If a value is given, though, it must pass the validator test.
// TODO: possibly allow for comma-separated list or something. Add more.
// complete (optional, defaults to RefreshStatus) tells the function to call after load completes
// Err(badFields) gets called with a list of fields that failed validation. If not supplied, a cheesy
// Err function is used.
function _DefaultErr(badFields)
{   // default error handling imeplementation - throws an alert for the first failed field, sets the focus, and exits.
    var el = badFields[0];
    alert(ValidationMessage(el));
    $(el).focus();
}

function ValidateForm(form, Err)
{
    var badFields = [];
	var els = $(form).find(':input');
	for (var i=0; i < els.length; i++)
	{
		var el = els[i];
		var validator = $.trim(el.getAttribute('valid') || '');
		if (!validator)
			continue;

		var v = $.trim($(el).val()); // todo: make this work for checkboxes... if we need it
		
		// if the validator is in parentheses then that means the field is optional. So, it can be blank but if a value is
		// given, it must pass the validator test.
		//alert($(el).attr('name') + ' ' + v)
		if (validator.charAt(0) == '(' && validator.charAt(validator.length-1) == ')')
		{
		    validator = validator.substr(1, validator.length-2);
		    if (!v || v.length == 0)
		        continue;
		}

		if (validator.substr(0,4) == 'num:')
		{
			var range = validator.substring(4).split(' ');
			var lo = range[0];
			var hi = range[1];
			if (v.length == 0 || isNaN(v) || parseFloat(v) < lo || parseFloat(v) > hi)
			    badFields.push(el);
		}
		else if (validator == 'phone')
		{
		    v = v.toLowerCase()
		    var hadLetter = false;
		    var digits = [];
		    for (var j=0; j < v.length; j++)
		    {
		        var ch = v.charAt(j);
		        if (ch >= 'a' && ch <= 'z')
		            hadLetter = true;
		        else if (ch >= '0' && ch <= '9')
		            digits.push(ch)
		    }
		    
		    if (hadLetter > 0)
		        badFields.push(el);
		    else
		    {
		        if (digits.length == 11 && digits[0] == '1') // strip off long-distance '1'
		            digits = digits.slice(1);
		        if (digits.length != 10)
		            badFields.push(el)
		        else
		        {
		            // put a pretty-printed version of the phone number back into the field
		            digits = digits.join('')
		            $(el).val(digits.substr(0,3) + '-' + digits.substr(3,3) + '-' + digits.substr(6))
		        }
		    }
		}
		else if (validator == 'password')
		{
		    var digits = 0;
		    var other = 0;
		    for (var j=0; j < v.length; j++)
		    {
		        var ch = v.charAt(j);
		        if (ch >= '0' && ch <= '9')
		            digits++;
		        else
		            other++;
		    }
		    
		    if ((digits + other < 8) || digits < 1)
		        badFields.push(el);
		}
		else if (validator == 'posnum' && (v.length == 0 || isNaN(v) || parseFloat(v) <= 0))
		    badFields.push(el);
		else if (validator == 'email')
		{
		    var emailRE = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		    if (v.length == 0 || !emailRE.test(v) || v.length > 50)
		        badFields.push(el);
		}
		else if (validator == 'notblank' && $.trim(v || '').length == 0)
		    badFields.push(el);
		else if (validator.substr(0, 7) == 'string:')
		{
			var range = validator.substring(7).split(' ');
			var lo = range[0];
			var hi = range[1];
			var len = $.trim(v || '').length;
			if (((lo == hi) && (len != lo)) || (len < lo || len > hi))
			    badFields.push(el);
		}
		else if (validator.substr(0, 8) == 'matches:')
		{
			var otherName = validator.substring(8);
			var other = $(form).find('[name=' + otherName + ']').val();
			if ($.trim(other) != v)
			    badFields.push(el);
		}
		else if (validator.substr(0, 8) == 'hostname')
		{
		    v = v.toLowerCase();
		    re = /^[a-z]+[a-z0-9\-]+$/;
		    if(v.length == 0 || !re.test(v)){
		        badFields.push(el);
		    }
		}
	}

    if (badFields.length > 0)
    {
        if (!Err) Err = _DefaultErr;
        Err(badFields);
        return false;
    }
    
	return true;
}

// Validates the form using ValidateForm and, if all validate, submits it via Ajax. Always returns false.
// Target should be a jQuery identifer, e.g. '#someDivID' to hold the response.
// complete (optional, defaults to RefreshStatus) tells the function to call after load completes
function ValidateAndSubmit(form, target, complete)
{
	if (!ValidateForm(form))
		return false;
	if (!complete) complete = window.RefreshStatus; // todo: move this elsewhere
	var opts = {complete:complete};
	if (target)
	    opts.target = target;
	$(form).ajaxSubmit(opts);
	return false;
}

function CreateCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function ReadCookie(name, default_val) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') 
		    c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		    return c.substring(nameEQ.length,c.length);
	}
	return default_val;
}

function EraseCookie(name) 
{
	CreateCookie(name,"",-1);
}

var swfobject=function(){var X="undefined",P="object",a="visibility:visible",e="visibility:hidden",B="Shockwave Flash",h="ShockwaveFlash.ShockwaveFlash",V="application/x-shockwave-flash",K="SWFObjectExprInst",G=window,g=document,N=navigator,f=[],H=[],Q=null,L=null,S=false,C=false;var Y=function(){var l=typeof g.getElementById!=X&&typeof g.getElementsByTagName!=X&&typeof g.createElement!=X&&typeof g.appendChild!=X&&typeof g.replaceChild!=X&&typeof g.removeChild!=X&&typeof g.cloneNode!=X,t=[0,0,0],n=null;if(typeof N.plugins!=X&&typeof N.plugins[B]==P){n=N.plugins[B].description;if(n){n=n.replace(/^.*\s+(\S+\s+\S+$)/,"$1");t[0]=parseInt(n.replace(/^(.*)\..*$/,"$1"),10);t[1]=parseInt(n.replace(/^.*\.(.*)\s.*$/,"$1"),10);t[2]=/r/.test(n)?parseInt(n.replace(/^.*r(.*)$/,"$1"),10):0}}else{if(typeof G.ActiveXObject!=X){var o=null,s=false;try{o=new ActiveXObject(h+".7")}catch(k){try{o=new ActiveXObject(h+".6");t=[6,0,21];o.AllowScriptAccess="always"}catch(k){if(t[0]==6){s=true}}if(!s){try{o=new ActiveXObject(h)}catch(k){}}}if(!s&&o){try{n=o.GetVariable("$version");if(n){n=n.split(" ")[1].split(",");t=[parseInt(n[0],10),parseInt(n[1],10),parseInt(n[2],10)]}}catch(k){}}}}var v=N.userAgent.toLowerCase(),j=N.platform.toLowerCase(),r=/webkit/.test(v)?parseFloat(v.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,i=false,q=j?/win/.test(j):/win/.test(v),m=j?/mac/.test(j):/mac/.test(v);/*@cc_on i=true;@if(@_win32)q=true;@elif(@_mac)m=true;@end@*/return{w3cdom:l,pv:t,webkit:r,ie:i,win:q,mac:m}}();var d=function(){if(!Y.w3cdom){return }J(I);if(Y.ie&&Y.win){try{g.write("<script id=__ie_ondomload defer=true src=//:><\/script>");var i=b("__ie_ondomload");if(i){i.onreadystatechange=function(){if(this.readyState=="complete"){this.parentNode.removeChild(this);U()}}}}catch(j){}}if(Y.webkit&&typeof g.readyState!=X){Q=setInterval(function(){if(/loaded|complete/.test(g.readyState)){U()}},10)}if(typeof g.addEventListener!=X){g.addEventListener("DOMContentLoaded",U,null)}M(U)}();function U(){if(S){return }if(Y.ie&&Y.win){var m=W("span");try{var l=g.getElementsByTagName("body")[0].appendChild(m);l.parentNode.removeChild(l)}catch(n){return }}S=true;if(Q){clearInterval(Q);Q=null}var j=f.length;for(var k=0;k<j;k++){f[k]()}}function J(i){if(S){i()}else{f[f.length]=i}}function M(j){if(typeof G.addEventListener!=X){G.addEventListener("load",j,false)}else{if(typeof g.addEventListener!=X){g.addEventListener("load",j,false)}else{if(typeof G.attachEvent!=X){G.attachEvent("onload",j)}else{if(typeof G.onload=="function"){var i=G.onload;G.onload=function(){i();j()}}else{G.onload=j}}}}}function I(){var l=H.length;for(var j=0;j<l;j++){var m=H[j].id;if(Y.pv[0]>0){var k=b(m);if(k){H[j].width=k.getAttribute("width")?k.getAttribute("width"):"0";H[j].height=k.getAttribute("height")?k.getAttribute("height"):"0";if(O(H[j].swfVersion)){if(Y.webkit&&Y.webkit<312){T(k)}}else{if(H[j].expressInstall&&!C&&O("6.0.65")&&(Y.win||Y.mac)){D(H[j])}else{c(k)}}}}A("#"+m,a)}}function T(m){var k=m.getElementsByTagName(P)[0];if(k){var p=W("embed"),r=k.attributes;if(r){var o=r.length;for(var n=0;n<o;n++){if(r[n].nodeName.toLowerCase()=="data"){p.setAttribute("src",r[n].nodeValue)}else{p.setAttribute(r[n].nodeName,r[n].nodeValue)}}}var q=k.childNodes;if(q){var s=q.length;for(var l=0;l<s;l++){if(q[l].nodeType==1&&q[l].nodeName.toLowerCase()=="param"){p.setAttribute(q[l].getAttribute("name"),q[l].getAttribute("value"))}}}m.parentNode.replaceChild(p,m)}}function F(i){if(Y.ie&&Y.win&&O("8.0.0")){G.attachEvent("onunload",function(){var k=b(i);for(var j in k){if(typeof k[j]=="function"){k[j]=function(){}}}k.parentNode.removeChild(k)})}}function D(j){C=true;var o=b(j.id);if(o){if(j.altContentId){var l=b(j.altContentId);if(l){L=l}}else{L=Z(o)}if(!(/%$/.test(j.width))&&parseInt(j.width,10)<310){j.width="310"}if(!(/%$/.test(j.height))&&parseInt(j.height,10)<137){j.height="137"}g.title=g.title.slice(0,47)+" - Flash Player Installation";var n=Y.ie&&Y.win?"ActiveX":"PlugIn",k=g.title,m="MMredirectURL="+G.location+"&MMplayerType="+n+"&MMdoctitle="+k,p=j.id;if(Y.ie&&Y.win&&o.readyState!=4){var i=W("div");p+="SWFObjectNew";i.setAttribute("id",p);o.parentNode.insertBefore(i,o);o.style.display="none";G.attachEvent("onload",function(){o.parentNode.removeChild(o)})}R({data:j.expressInstall,id:K,width:j.width,height:j.height},{flashvars:m},p)}}function c(j){if(Y.ie&&Y.win&&j.readyState!=4){var i=W("div");j.parentNode.insertBefore(i,j);i.parentNode.replaceChild(Z(j),i);j.style.display="none";G.attachEvent("onload",function(){j.parentNode.removeChild(j)})}else{j.parentNode.replaceChild(Z(j),j)}}function Z(n){var m=W("div");if(Y.win&&Y.ie){m.innerHTML=n.innerHTML}else{var k=n.getElementsByTagName(P)[0];if(k){var o=k.childNodes;if(o){var j=o.length;for(var l=0;l<j;l++){if(!(o[l].nodeType==1&&o[l].nodeName.toLowerCase()=="param")&&!(o[l].nodeType==8)){m.appendChild(o[l].cloneNode(true))}}}}}return m}function R(AE,AC,q){var p,t=b(q);if(typeof AE.id==X){AE.id=q}if(Y.ie&&Y.win){var AD="";for(var z in AE){if(AE[z]!=Object.prototype[z]){if(z=="data"){AC.movie=AE[z]}else{if(z.toLowerCase()=="styleclass"){AD+=' class="'+AE[z]+'"'}else{if(z!="classid"){AD+=" "+z+'="'+AE[z]+'"'}}}}}var AB="";for(var y in AC){if(AC[y]!=Object.prototype[y]){AB+='<param name="'+y+'" value="'+AC[y]+'" />'}}t.outerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+AD+">"+AB+"</object>";F(AE.id);p=b(AE.id)}else{if(Y.webkit&&Y.webkit<312){var AA=W("embed");AA.setAttribute("type",V);for(var x in AE){if(AE[x]!=Object.prototype[x]){if(x=="data"){AA.setAttribute("src",AE[x])}else{if(x.toLowerCase()=="styleclass"){AA.setAttribute("class",AE[x])}else{if(x!="classid"){AA.setAttribute(x,AE[x])}}}}}for(var w in AC){if(AC[w]!=Object.prototype[w]){if(w!="movie"){AA.setAttribute(w,AC[w])}}}t.parentNode.replaceChild(AA,t);p=AA}else{var s=W(P);s.setAttribute("type",V);for(var v in AE){if(AE[v]!=Object.prototype[v]){if(v.toLowerCase()=="styleclass"){s.setAttribute("class",AE[v])}else{if(v!="classid"){s.setAttribute(v,AE[v])}}}}for(var u in AC){if(AC[u]!=Object.prototype[u]&&u!="movie"){E(s,u,AC[u])}}t.parentNode.replaceChild(s,t);p=s}}return p}function E(k,i,j){var l=W("param");l.setAttribute("name",i);l.setAttribute("value",j);k.appendChild(l)}function b(i){return g.getElementById(i)}function W(i){return g.createElement(i)}function O(k){var j=Y.pv,i=k.split(".");i[0]=parseInt(i[0],10);i[1]=parseInt(i[1],10);i[2]=parseInt(i[2],10);return(j[0]>i[0]||(j[0]==i[0]&&j[1]>i[1])||(j[0]==i[0]&&j[1]==i[1]&&j[2]>=i[2]))?true:false}function A(m,j){if(Y.ie&&Y.mac){return }var l=g.getElementsByTagName("head")[0],k=W("style");k.setAttribute("type","text/css");k.setAttribute("media","screen");if(!(Y.ie&&Y.win)&&typeof g.createTextNode!=X){k.appendChild(g.createTextNode(m+" {"+j+"}"))}l.appendChild(k);if(Y.ie&&Y.win&&typeof g.styleSheets!=X&&g.styleSheets.length>0){var i=g.styleSheets[g.styleSheets.length-1];if(typeof i.addRule==P){i.addRule(m,j)}}}return{registerObject:function(l,i,k){if(!Y.w3cdom||!l||!i){return }var j={};j.id=l;j.swfVersion=i;j.expressInstall=k?k:false;H[H.length]=j;A("#"+l,e)},getObjectById:function(l){var i=null;if(Y.w3cdom&&S){var j=b(l);if(j){var k=j.getElementsByTagName(P)[0];if(!k||(k&&typeof j.SetVariable!=X)){i=j}else{if(typeof k.SetVariable!=X){i=k}}}}return i},embedSWF:function(n,u,r,t,j,m,k,p,s){if(!Y.w3cdom||!n||!u||!r||!t||!j){return }r+="";t+="";if(O(j)){A("#"+u,e);var q=(typeof s==P)?s:{};q.data=n;q.width=r;q.height=t;var o=(typeof p==P)?p:{};if(typeof k==P){for(var l in k){if(k[l]!=Object.prototype[l]){if(typeof o.flashvars!=X){o.flashvars+="&"+l+"="+k[l]}else{o.flashvars=l+"="+k[l]}}}}J(function(){R(q,o,u);A("#"+u,a)})}else{if(m&&!C&&O("6.0.65")&&(Y.win||Y.mac)){A("#"+u,e);J(function(){var i={};i.id=i.altContentId=u;i.width=r;i.height=t;i.expressInstall=m;D(i);A("#"+u,a)})}}},getFlashPlayerVersion:function(){return{major:Y.pv[0],minor:Y.pv[1],release:Y.pv[2]}},hasFlashPlayerVersion:O,createSWF:function(k,j,i){if(Y.w3cdom&&S){return R(k,j,i)}else{return undefined}},createCSS:function(j,i){if(Y.w3cdom){A(j,i)}},addDomLoadEvent:J,addLoadEvent:M,getQueryParamValue:function(m){var l=g.location.search||g.location.hash;if(m==null){return l}if(l){var k=l.substring(1).split("&");for(var j=0;j<k.length;j++){if(k[j].substring(0,k[j].indexOf("="))==m){return k[j].substring((k[j].indexOf("=")+1))}}}return""},expressInstallCallback:function(){if(C&&L){var i=b(K);if(i){i.parentNode.replaceChild(L,i);L=null;C=false}}}}}();

// opens a div as a modal dialog. the div should have classes like "popup large green_large" and then have a containing div
// with a class of popup_content.Use ClosePopup when done. If URL is provided, it is used via AJAX to populate the div before display.
var _popupDiv = null;
var _popupCloseCB = null;
function _popSetDisplay(idSelector, display)
{
    $('#overlay').css('display', display);
    $(idSelector).css('display', display);
}

function Popup(idSelector, url, openCB, closeCB)
{
    _popupDiv = idSelector;
    _popupCloseCB = closeCB;
	if (url)
	{
	    $.get(url, {}, function(data)
    	{
    	    $(idSelector + ' > .popup_content').html(data);
    	    //$(idSelector + ' > .popup_content').each(function() { alert(this); })
    	    _popSetDisplay(idSelector, 'block')
    		if (openCB && typeof(openCB) == 'string')
    		    openCB = window[openCB];
    		if (openCB && typeof(openCB) == 'function')
    		    openCB();
    	});
    }
    else
    {
        _popSetDisplay(idSelector, 'block')
		if (openCB && typeof(openCB) == 'string')
		    openCB = window[openCB];
		if (openCB && typeof(openCB) == 'function')
		    openCB();
    }
	return false;
}

// utility function to close the most recent popup
// by default, clears out the container contents
function ClosePopup(leaveContents)
{
    if (!_popupDiv)
        return;
    _popSetDisplay(_popupDiv, 'none')
    //if (!leaveContents)
    //    $(_popupDiv).html('');
    if (_popupCloseCB)
        setTimeout(_popupCloseCB, 1);
    _popupDiv = null;
    _popupCloseCB = null;
}

function GetPageParams()
{   // reads off a parameter string and returns it as a dictionary
    var url = window.location.search;
    var i = url.indexOf('?');
    if (i == -1)
        return {};

    var ret = {};
    var params = url.substr(i+1).split('&');
    for (var i=0; i<params.length; i++)
    {
        var param = params[i].split('=', 2);
        if (param.length == 1)
            ret[param[0]] = true;
        else if (param.length == 2)
            ret[param[0]] = decodeURIComponent(param[1]);
    }
    return ret;
}
