
//this function is used for field validation in forms
/*
* id = id of model in db, model with this id will be loaded before validation if specified
* classname = hte name of the model to be validated
* fieldname = the model field to be validated
* fieldname2 = if a field validation requires another form value to validate specifiy it here, ie: confirmation fields
* fieldvalue2 = the second field value
* field_id = the prefix for all error display div ids
*/
function validate(id, classname, fieldname, fieldvalue, fieldname2, fieldvalue2, field_id)
{
	//var field_id = classname + '_' + fieldname;
	var datastring = "id="+id+"&classname="+escape(classname)+"&fieldname="+escape(fieldname)+"&fieldvalue="+escape(fieldvalue)+"&fieldname2="+escape(fieldname2)+"&fieldvalue2="+escape(fieldvalue2);
	$.ajax({
	type: "GET",
	url: base_url + "field_validation",
	data: datastring,
	dataType: "json",
	success: function(data) {
 		if(data.valid == "true")
		{
			
			$('#'+field_id+'-tick').css("display", "block");
	
	
			$('#'+field_id+'-cross').css("display", "none");
	
			$('#'+field_id+'-message').css("display", "none");
			$('#'+field_id+'-message').html("");
			

			$('#'+field_id).attr("valid", "true");
		}
		else
		{
			
			$('#'+field_id+'-cross').css("display", "block");
	
			$('#'+field_id+'-tick').css("display", "none");
	
			$('#'+field_id+'-message').css("display", "block");
			$('#'+field_id+'-message').html(data.message);
			
			$('#'+field_id).attr("valid", "false");
		}
	}
	,
	error:function (xhr, ajaxOptions, thrownError){
		if(xhr.responseText != "")
		{
		    //alert(xhr.responseText);
		}
	   
	}    
	});	

	
}

//attach tooltip to specified element, used for adding tool tips to for fields
function attachTooltip(element_id, tooltip)
{
	$('#'+element_id).tipsy({fallback: tooltip });
}


function getIcon()
{		
	var myIcon = new GIcon();
	myIcon.image = base_url+'www/images/marker.png';	
	myIcon.shadow = base_url+'www/images/marker_shadow.png';
	myIcon.iconSize = new GSize(24,34);
	myIcon.shadowSize = new GSize(41,34);
	myIcon.iconAnchor = new GPoint(12,34);
	myIcon.infoWindowAnchor = new GPoint(12,0);
	myIcon.imageMap = [16,0,18,1,19,2,20,3,21,4,21,5,22,6,22,7,23,8,23,9,23,10,23,11,23,12,23,13,23,14,23,15,22,16,22,17,21,18,21,19,20,20,19,21,18,22,17,23,16,24,16,25,15,26,15,27,14,28,13,29,13,30,13,31,13,32,13,33,11,33,11,32,10,31,10,30,10,29,9,28,9,27,8,26,8,25,7,24,6,23,5,22,4,21,3,20,2,19,1,18,1,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,1,6,1,5,2,4,3,3,4,2,5,1,7,0];
	return myIcon;
}

//this function adds/removes the specified address from the specifid campaign
function addAddressToCampaign(address_id, campaign_id, add)
{
	var url = base_url + "campaign/add_address/" + campaign_id + "/?address_id=" + escape(address_id) + "&add=" + add;
	//alert(url);
	$.ajax({
	type: "GET",
	url: url,
	dataType: "json",
	success: function(data) {
 		
	}
	,
	error:function (xhr, ajaxOptions, thrownError){
		if(xhr.responseText != "")
		{
		    alert(xhr.responseText);
		}
	   
	}    
	});	
}

//used to limit the size of input fields, eg tweets, the outputElement will have its html set to the remaining count
function limitInputSize(maxlength, inputElement, outputElement)
 {
 	if(inputElement.value.length > maxlength) 
 	{
 		inputElement.value = inputElement.value.substring(0, maxlength);
 	} 
 	outputElement.html((maxlength - inputElement.value.length) + " characters")
 }


