$(function() {
	$("#agentlink").click(function() {
		$.get("agentinsertclick.php", {agentid: $("#agentid").val(), cliqurl: this.href}, function(result) {
			$("#status").html( result ).fadeIn();
		})
	})
});
$(function() {
        $("#agentlistlink").click(function() {
                $.get("agentinsertclick.php", {agentid: $("#agentid").val(), cliqurl: this.href}, function(result) {
                        $("#status").html( result ).fadeIn();
                })
        })
});
function setType(typename) 
{
	if(typename) {
		href=location.href;
		window.location = this.href+"&type="+typename;
		return;
	}
}

function validateUserAge(age,limit,responseId,form,assume)
{
	// If assume is set, we do no calculations and instead base it off pre-selected years
	if(assume) {
		if(form) {
			if(age != "") {
			form.submit.disabled = false;
			} else {
			form.submit.disabled = true;
			}
		}
	} else {
	if(age < limit) {
		document.getElementById(responseId).innerHTML = "You must be at least "+limit+" years old to continue";
		if(form) {
			form.submit.disabled = true;
		}
	} else {
		if(form) {
			form.submit.disabled = false;
		}
	}
	}
}

/* Check file extension against a comma separated extension string */
function checkExtension(string,extensionString) {
	if(string == "") return 3;
	var extensions = extensionString.split("|");
	var filename = string;
	var dot = filename.lastIndexOf("."); 
	var extension = filename.substr(dot,filename.length); 
	var retValue = 0;
	for(var i=0;i<=extensions.length;i++) {
		if(extension == extensions[i]) {
			retValue = 1;
		}
	}
	return retValue;
}

/* Vaidate a selected image file name */
function validateImage(form) {
	iextensions = ".jpg|.jpeg";
        var txtnode = document.getElementById("imagemessage");
	var result = checkExtension(form.image.value,iextensions)
	if(result == 0) {
		txtnode.innerHTML = "That file is not a jpeg/jpg! Try again!";
                txtnode.style.display = "block";
	} else if(result == 1){
		txtnode.innerHTML = "Valid file!";
       	       txtnode.style.display = "block";
	} else {
		txtnode.style.display = "none";
	}

}

/* Load our sponsor/contribute forms via AJAX */
var userid = null;
function showForm(formnum,cityid,blogid,type,userid)
{
	if(formnum)
	{
	if(!type) {
		var type = "sponsor";
	}
	var fileName;
	var divName;
	if(type == "sponsor") {
		fileName = "getSponsorForm.php";
		divName = "sponsorform";
	} else if(type == "contribute") {
		fileName = "getContribForm.php";
		divName = "contribform";
	}
		var requestUrl = "";
		switch(formnum)
		{
			case 1:
			requestUrl = fileName+"?id=1&cityid="+cityid+"&blogid="+blogid+"&url="+location.href+"&userid="+userid;
			break;
			case 2:
			requestUrl = fileName+"?id=2&cityid="+cityid+"&blogid="+blogid+"&url="+location.href+"&userid="+userid;
			break;
			case 3:
			requestUrl = fileName+"?id=3&cityid="+cityid+"&blogid="+blogid+"&url="+location.href+"&userid="+userid;
			break;
                        case 4:
                        requestUrl = fileName+"?id=4&cityid="+cityid+"&blogid="+blogid+"&url="+location.href+"&userid="+userid;
                        break;
		}
		// Loading...
		var loadingtxt = document.getElementById(divName);
		loadingtxt.innerHTML = "Loading...";
		HTML_AJAX.replace(divName,requestUrl);
		var err = document.getElementById("validateMessage");
		err.innerHTML = "";
		err.style.display = "none";
		
		var err1 = document.getElementById("errortitle");
		if(err1) {
		err1.style.display = "none";
		}
	}
}
var userid = '';
function showFeaturedForm(cityid,blogid,userid) {
	var beta = '&beta=1';
        var requestUri = '';
        requestUri = "id=2&cityid="+cityid+"&blogid="+blogid+"&url="+location.href+"&userid="+userid+beta;
	$('#sponsorform').load('/getSponsorFormBeta.php?'+requestUri).fadeIn('fast');
}        

/* The redirector for our main drop downs */
function sendToSelection()
{
	var state = document.getElementById("stateselect");
	var city = document.getElementById("cityselect");
	var stateindex = document.getElementById("stateselect").selectedIndex;
	var cityindex = document.getElementById("cityselect").selectedIndex;
	var selectedState = state.options[stateindex].text;
	var selectedCity = city.options[cityindex].text;
	if(selectedCity == "--Select City--") return false;
	selectedState = selectedState.toLowerCase();
	selectedCity = selectedCity.toLowerCase();
	selectedState = selectedState.replace(" ","");
	selectedCity = selectedCity.replace(" ","");
	selectedCity = selectedCity.replace(" ","");
	selectedCity = selectedCity.replace(" ","");
	selectedState = selectedState.replace(".","");
	//selectedCity = selectedCity.replace(".","");
	var nlocation = "http://"+selectedCity+"."+selectedState+"blogpage.com";
	parent.window.location = nlocation;
}

/*
^ Used in the state landing pages to send to a city landing page
*/
function sendCityLanding(script)
{
	if(!script) {
	var script = "contribute.php";
	}
        var city = document.getElementById("cityselect");
        var cityindex = document.getElementById("cityselect").selectedIndex;
        var nlocation = city.options[cityindex].value+"/"+script;
     	
        window.location = nlocation;
}



function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}

function keyValidateEmail(str)
{
	if(str)
	{
		var valid = validateEmail(str);
		var strO = document.getElementById("emailmessage");
		if(valid == 1) {
		strO.style.color="green";
		strO.innerHTML = "Email is valid!";
		} else {
		strO.style.color="#B01111";
		strO.innerHTML = "Email is invalid! Keep trying!"
		}
		strO.style.display = "block";
	return false;
	}
}

function retrieveCategoryStatus(id,cityid,type)
{
	if(id != "" && cityid != "" && type != "")
	{
		var processresult = function(result) {
			var responsetxt = document.getElementById("categorystatus");
			if(result == 0) {
                                if(responsetxt) {
                                    responsetxt.style.display = "none";
                                }
				var form = document.getElementById("sponsor");
				form.submit.disabled = true;
				var queue = document.getElementById("sponsorqueue");
				queue.style.display = "block";
				return 0;
			}else {
				if(responsetxt) {
					responsetxt.innerHTML = "Don't wait! There are only "+result+" spots available for this Park Bench!";
                                        responsetxt.style.display = "block";
				}
                                var queue = document.getElementById("sponsorqueue");
                                queue.style.display = "none";
                                var form = document.getElementById("sponsor");
                                form.submit.disabled = false;
				return 1;
			}
		}
		HTML_AJAX.grab("getCategoryStatus.php?id="+id+"&cityid="+cityid+"&type="+type,processresult);
		return false;
	} 
	// If we get here... they chose "Select"
        var form = document.getElementById("sponsor");
        form.submit.disabled = true;
        var queue = document.getElementById("sponsorqueue");
        queue.style.display = "none";
	var responsetxt = document.getElementById("categorystatus");
	responsetxt.style.display = "none";

}

/* Get the status of a username */
var usertype = ""; // int 1 or null
function getUsernameStatus(form,strstate,usertype) 
{
	var str = form.username.value;
	if(str && strstate)
	{
		if(str.length <= 3) {
		txt = document.getElementById("usernamemessage");
		txt.innerHTML = "Username too short";
		return false;
		}else if(str.length >= 15) {
                txt = document.getElementById("usernamemessage");
                txt.innerHTML = "Username too long";
		return false;
		}
		var spcallback = function(result) {	
			if(result == 1)
			{
				txt = document.getElementById("usernamemessage");
				if(txt) {
					txt.innerHTML = "That username is available!";
				}
				return true;
				/*r catc = document.getElementById("parkbench");
				if(catc) {
					var cati = form.parkbench.selectedIndex;
					var cat = form.parkbench[ cati ].value;
					if(cat != "") {
					}
				} else {
					form.submit.disabled = false;
				}*/	
				
			} else if(result == 2) {
                                txt = document.getElementById("usernamemessage");
                                txt.innerHTML = "INVALID username! Please only alphanumerical values and underscores (_)";
				
			} else {
				txt = document.getElementById("usernamemessage");
				txt.innerHTML = "Sorry, that username is not available.";
				
			}
		}
		HTML_AJAX.grab("getUsernameStatus.php?u="+str+"&st="+strstate+"&type="+usertype,spcallback);
	} 
}

function changePassword(userid)
{
	var form = document.getElementById("changepasswordform");
        var str1 = form.currpass.value;
	var str2 = form.newpass.value;
        if(str1 && str2 && userid)
        {
	alert(userid);
                if(str2.length <= 3) {
                txt = document.getElementById("changepasswordmessage");
                txt.innerHTML = "Password too short";
                return false;
                }else if(str2.length >= 15) {
                txt = document.getElementById("changepasswordmessage");
                txt.innerHTML = "Password too long";
                return false;
                }
                var spcallback = function(result) {
                        if(result == 1)
                        {
                                txt = document.getElementById("changepasswordmessage");
                                if(txt) {
                                        txt.innerHTML = "Password successfully changed.";
                                }
                                return true;

                        } else {
                                txt = document.getElementById("changepasswordmessage");
                                txt.innerHTML = "Invalid Password.";
				return false;
                        }
                }
                HTML_AJAX.grab("/changePassword.php?uid="+userid+"&st1="+str1+"&st2="+str2,spcallback);
        }
}


function ajaxAddToQueue(form)
{
        emailVal = form.emailaddressq.value;
        var catid = document.getElementById("parkbench");
	if(typeof(catid) == "undefined" || !catid) catid = 1;
        var id = catid.options[document.getElementById("parkbench").selectedIndex].value;

        var aqcallback = function(result) {
                if(result == 1) {
                        // Show message
                        var spNode = document.getElementById("becomeSponsor");
                        spNode.innerHTML = "Thank you for your interest in becoming an Agent Sponsor. You will receive an email when a spot opens up.";
                } else {
                        // Most likely already exists
                        var spNode = document.getElementById("becomeSponsor");
                        spNode.innerHTML = "Sorry, but it appears that email address is already in our queue.";
                }
        }
        HTML_AJAX.grab("ajaxNotifyQueue.php?addNew="+emailVal+"&id="+id,aqcallback);
}

/* Function to toggle the abuse form on and off */
function toggleAbuseForm(id,link) {
	// Turn it off
	var effectnodename = "abuse"+id;
	var txtnoded = document.getElementById("abuse"+id);
	if(txtnoded) {
		if(txtnoded.style.display == "block") {
			$('#'+effectnodename).slideUp(300);
			txtnoded.style.display = "none";
			//node.innerHTML = "";
			return;
		} else {
                      $('#'+effectnodename).slideDown(300);		
		      txtnoded.style.display = "block";
		}
	}
	
	/*
	var cb = function(response) {
		if(response != "") {
			var effectnodename = "abuse"+id;
			var txtnode = document.getElementById("abuse"+id);
			txtnode.innerHTML = response;
			$('#'+effectnodename).corner();
			$('#'+effectnodename).fadeIn('slow');
			txtnode.style.display = "block";
		}
	}*/
	
	// Send the request
	//HTML_AJAX.grab("reportAbuse.php?id="+id+"&getform=1&postlink="+link,cb);
	//HTML_AJAX.replace("abuse"+id,"reportAbuse.php?id="+id+"&getform=1&postlink="+link);
	$("#abuse"+id).load("/reportAbusetest.php?id="+id+"&getform=1&postlink="+link);
	return;
}

function submitForm(form,responseId) {
	if(form) {
		var options = {
			target: '#'+responseId,
			url: form.action
		};
		$('#'+form.name).ajaxSubmit(options);
	}
}


