/* THIS IS THE JAVASCRIPT FOR THE BACK END FORM HANDLING AND VALIDATION */

$cb = jQuery.noConflict();

/* MAP FUNCTIONS */
function cbsm_getLatLong(lookupAddress, googleMapsAPIkey)
	{
	var msg='';
	if(lookupAddress==' ,  , ')
		{
		msg += ' - Enter a valid address\r\n';
		lookupAddress="519 SW Park Ave, Suite 501, Portland, OR";
		}
	if(googleMapsAPIkey=='')
		{
		msg += ' - Your Google Maps API key has not been set.\r\n   Please enter it in the Store Locator settings.';
		}
	if(msg=='')
		{
		//do geocoding to get lat/long
		$cb.getJSON("http://maps.google.com/maps/geo?q="+lookupAddress.replace(/[^a-zA-Z 0-9]+/g,'')+"+&key="+googleMapsAPIkey+"&sensor=false&output=json&callback=?",
		  function(data, textStatus){
			gEBId('cbsm_latitude').value = data.Placemark[0].Point.coordinates[1];
			gEBId('cbsm_longitude').value = data.Placemark[0].Point.coordinates[0];
		  });

		//toggle the display of the two forms
		gEBId('cbsm_saveForm').style.display="block";
		gEBId('cbsm_lookupForm').style.display="none";
		}
		else{
			msg = 'Please correct the following errors\r\n----------------------------------------\r\n\r\n' + msg;
			alert(msg);
			}
		
	return false;
	}

/* FORM FUNCTIONS */
function cbsm_getVals()
	{
	//grab the lookup vals to use for geocoding, passing to the save form

	// these values are passed to the save form from the lookup form
	gEBId('cbsm_name').value = gEBId('cbsm_lookupName').value;
    gEBId('cbsm_address').value = gEBId('cbsm_lookupAddress').value.replace(",","");
    gEBId('cbsm_city').value = gEBId('cbsm_lookupCity').value;
    gEBId('cbsm_state').value = gEBId('cbsm_lookupState').value;
    gEBId('cbsm_zip').value = gEBId('cbsm_lookupZip').value;
    gEBId('cbsm_country').value = gEBId('cbsm_lookupCountry').value;
	gEBId('cbsm_phone').value = gEBId('cbsm_lookupPhone').value;
	gEBId('cbsm_url').value = gEBId('cbsm_lookupURL').value;
	gEBId('cbsm_tags').value = gEBId('cbsm_lookupTags').value;

	// this combined address is sent to google to find the lat/long
	var cbsm_string = gEBId('cbsm_lookupAddress').value + ', ';
		cbsm_string += gEBId('cbsm_lookupCity').value + ', ';
		cbsm_string += gEBId('cbsm_lookupState').value + ' ';
		cbsm_string += gEBId('cbsm_lookupZip').value + ', ';
		cbsm_string += gEBId('cbsm_lookupCountry').value;
	return cbsm_string;
	}
	
	
	
function cbsm_verify()
	{
	//validate the location save form
	var msg = '';
	if(gEBId('cbsm_name').value=='')
		{		msg += ' - Enter a valid name\r\n';		}

	if((gEBId('cbsm_latitude').value=='' || gEBId('cbsm_longitude').value=='') || (isNaN(gEBId('cbsm_latitude').value) || isNaN(gEBId('cbsm_longitude').value)))
		{		msg += ' - Enter a valid latitude and longitude\r\n';		}

	if((gEBId('cbsm_url').value!=='') && (gEBId('cbsm_url').value.indexOf('http://')==-1))
		{
		msg += ' - Enter a valid URL (http://...)\r\n'; 
		}


	if(msg=='')
		{
		return true;
		}
		else{
			msg = 'Please correct the following errors\r\n----------------------------------------\r\n\r\n' + msg;
			alert(msg);
			return false;
			}
	}
	
function cbsm_verify_online()
	{
	//validate the web-store add form
	var msg = '';
	if(gEBId('cbsm_online_name').value=='')
		{
		msg += ' - Enter a valid name\r\n';
		}
	if(gEBId('cbsm_online_url').value.indexOf('http://')==-1)
		{
		msg += ' - Enter a valid URL (http://...)\r\n'; 
		}
	if(msg=='')
		{
		return true;
		}
		else{
			msg = 'Please correct the following errors\r\n----------------------------------------\r\n\r\n' + msg;
			alert(msg);
			return false;
			}
	}
	
function cbsm_delete(type, id)
	{
	//validate the delete form	
	if (confirm("Do you really want to delete id: "+id+"?")) 
		{
		return true;
		}
		else{
			return false;
			}
	}	
	
function cbsm_edit(type, id, fields)
	{
	if(document.getElementById('cbsm_editForm')){alert("You may only edit one entry at a time");}else{
	var output = "";
	gEBId('edit-'+type+'-'+id).style.display='';
	switch(type)
		{
		case "markers":
			output += "<form id=\"cbsm_editForm\" action=\"\" class=\"addStore\" method=\"POST\" onsubmit=\"return cbsm_verify();\">";
			output += "<input type=\"hidden\" name=\"cbsm_id\" value=\""+fields['id']+"\"/>";
			output += "<table class=\"form-table\">";
			output += "<tr><td><label for=\"cbsm_name\">Name</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_name\" id=\"cbsm_name\" value=\""+fields['name']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_address\">Address</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_address\" id=\"cbsm_address\" value=\""+fields['address']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_city\">City</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_city\" id=\"cbsm_city\" value=\""+fields['city']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_state\">State (region)</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_state\" id=\"cbsm_state\"  value=\""+fields['state']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_zip\">Zip / Postal Code</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_zip\" id=\"cbsm_zip\" value=\""+fields['zip']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_country\">Country</label></td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_country\" id=\"cbsm_country\" value=\""+fields['country']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_url\">URL</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_url\" id=\"cbsm_url\" value=\""+fields['url']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_phone\">Phone</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_phone\" id=\"cbsm_phone\" value=\""+fields['phone']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_tags\">Tags <small>(tag, tag)</small></td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_tags\" id=\"cbsm_tags\" value=\""+fields['tags']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_latitude\">Latitude</td><td><input type=\"text\" class=\"text autofill\" name=\"cbsm_latitude\" id=\"cbsm_latitude\" value=\""+fields['latitude']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_longitude\">Longitude</td><td><input type=\"text\" class=\"text autofill\" name=\"cbsm_longitude\" id=\"cbsm_longitude\" value=\""+fields['longitude']+"\"/></td></tr>";
			output += "<tr><td colspan=\"2\"><input type=\"submit\" value=\"save location\"/></td></tr>";
			output += "</table>";
			output += "</form>";
		break;
		
		case "online":
			output += "<form id=\"cbsm_editForm\" action=\"\" class=\"addStore\" method=\"POST\" onsubmit=\"return cbsm_verify_online();\">";
			output += "<input type=\"hidden\" name=\"cbsm_online_id\" value=\""+fields['id']+"\"/>";
			output += "<table class=\"form-table\">";
			output += "<tr><td><label for=\"cbsm_online_name\">Name</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_online_name\" id=\"cbsm_online_name\" value=\""+fields['name']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_online_url\">URL</td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_online_url\" id=\"cbsm_online_url\" value=\""+fields['url']+"\"/></td></tr>";
			output += "<tr><td><label for=\"cbsm_online_tags\">Tags <small>(tag, tag)</small></td><td><input type=\"text\" class=\"regular-text\" name=\"cbsm_online_tags\" id=\"cbsm_online_tags\" value=\""+fields['tags']+"\"/></td></tr>";
			output += "<tr><td colspan=\"2\"><input type=\"submit\" value=\"save website\"/></td></tr>";
			output += "</table>";
			output += "</form>";
		break;
		}
	gEBId('form-'+type+'-'+id).innerHTML=output;
	}
	return false;
	}
	
	
	
/* LIBRARY SAFE SHORTCUT */	
function gEBId($id)
	{
	return document.getElementById($id);
	}