// global variables for ajax tell a friend
var sent = false;
var err = false;

function submitSocialTag() {
	var defaultMsg = "enter a tag";
	
	// get data
	var productId = "";
	var productName = "";
	var colorId = "";
	var h2s = null;
	if ($("prodID")) {
		productId = $("prodID").value;
	}
	if ($("prodName")) {
		productName = $("prodName").value;
	}
	if ($("prodColor")) {
		colorId = $("prodColor").value;
	}
	if (colorId.length == 0) {
		// get colorId from query string: search result
		var colorKey = "&color=";
		var nameValuePair = location.search.substring(location.search.indexOf(colorKey));
		colorId = nameValuePair.substring(colorKey.length);
		// strip off any extra query string parameters
		if (colorId.indexOf("&") > 0) {
			colorId = colorId.substring(0, colorId.indexOf("&"));
		}		
	} else {
		colorId = "01";
	}
	
	// blur send link
	$("sendEmail").blur();

	// get social tag value
	var socialTag = "";
	if (($("socialTagName")) && (productId.length > 0) && (colorId.length > 0) && (productName.length > 0)) {
		if ($("socialTagName").value != defaultMsg) {
			// check if social tag matches regular expression
			var test = checkExpression($("socialTagName"));
			if (test) {
				socialTag = $("socialTagName").value;
				err = false;
			} else {
				err = true;
			}
		} else {
			alert("Please enter a tag.");
			err = true;
		}
	}
	
	// set response message object
	var msg = null;
	if ($("socialTaggingMsg")) {
		msg = $("socialTaggingMsg");
	}
	
	// build query string to send with ajax request
	var qString = "?formAction=sendMessage&productId=" + productId + "&productName=" + productName + "&colorId=" + colorId + "&socialTagName=" + socialTag;
	//alert(qString);

	// make ajax call
	var url = "/urban/catalog/ajax_socialtagging.jsp" + qString;
	// check boolean and if object exists
	if ((socialTag.length > 0) && (!sent) && (!err) && (msg != null)) {
		msg.innerHTML = "Sending ...";
		// set boolean flag to prevent user from repeatedly submitting ajax request
		sent = true;
		// ajax call
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				// reset form
				$("socialTagName").value = defaultMsg;
				// display message
				msg.innerHTML = transport.responseText;
				// reset boolean to allow customer to submit more tags
				sent = false;
			},
		    onFailure: function(transport) {
		    	// do nothing
		    }
		});
	}
}
function checkExpression(obj) {
	var txt = "enter a tag";
	var pattern = /[^a-zA-Z \-_0-9]/;
	if (obj.value != txt) {
		// trim whitespace
		//alert("hit");
		var val = trim(obj.value);	
		// strip out non-valid characters 
		//val =  val.replace(/[^a-zA-Z 0-9]+/g,'');
		// check length of submission
		if (val.length <= 0) {
			alert("Please enter a tag.");	
			obj.select();
			return false;
		// check for invalid characters
		} else if (val.match(pattern)) {
			alert("Please use only letters and numbers.");
			obj.select();
			return false;
		} else {
			// valid tag
			return true;
		}
	} else {
		alert("Please enter some text.");
		obj.select();
		return false;
	}
}
function trim(str) {
	while(''+str.charAt(0)==' ') {
		str=str.substring(1,str.length);
	}
	while(''+str.charAt(str.length-1)==' ') {
		str=str.substring(0,str.length-1);
	}
	return str;
}