/**
 * teclan functionality
 *
 * @version 1.0.1
 * @date 23/05/11
 * @author Fergus Weir
 * @copyright teclan 2011
 */
 
var $i = function(id) {
  return document.getElementById(id);
};

 function formatInfoData() {
  var hidden = $i('uploadData');
  if (hidden) {
    var data = "";
	//add title text
	if ($i('titleText') && $i('titleText').value != '') {data += "<title>" + $i('titleText').value + "</title>";}
	//add message text
	if ($i('messageText') && $i('messageText').value != ''){
	   var msgstr = $i('messageText').value;
	   var newmsg = msgstr.replace(/\r|\n/g, "[NL]");
	   data += "<message>" + newmsg + "</message>";
	   }
	//clear if nothing in either
	if ($i('titleText') && $i('messageText')) {
		if ($i('titleText').value == '' &&  $i('messageText').value == '')
		{data = '[no text entered]';}
	}
	if ($i('titleText') && !$i('messageText')){
		if ($i('titleText').value == '')
				{data = '[no text entered]';}
	}
	if (!$i('titleText') && $i('messageText')){
		if ($i('messageText').value == '')
				{data = '[no text entered]';}
	}

	$i('uploadData').setAttribute("value", data);

  }
}

function validatePersonaliseForm() {
	//check if anything has been entered
	if ($i('titleText').value == ''){
		alert('You must enter some personalised text into the title box!');
		inputErrorHighlight($i('titleText'),true);
		inputErrorHighlight($i('messageText'),false);
		return false;
	}
	else if ($i('messageText').value == '') {
		alert('You must enter some personalised text into the message box!');
		inputErrorHighlight($i('titleText'),false);
		inputErrorHighlight($i('messageText'),true)
		return false;
	}
	else {
		inputErrorHighlight($i('titleText'),false);
		inputErrorHighlight($i('messageText'),false);
		return true;
	}
}

function inputErrorHighlight(target,style) {
	var inputBox = target;
	if (inputBox) {
		if (!style) {
		inputBox.style.border = "2px dotted #B9C6C6";	
		}
		else {
		inputBox.style.border = "inset red 3px";
		inputBox.focus();
		}
	}
}
/* teclan Javascript function to change
 dynamic product choice images within drop down layout  */
function updateDynamicChoiceImage(dropDown,widgetID,imageList)  {
	
	var myindex  = dropDown.selectedIndex
    var SelValue = dropDown.options[myindex].value
	var SelName = dropDown.options[myindex].text;
	//retrieve image map
	var imageMap = getProdChoiceImageMap(imageList);
	//get image
	var selectedImageFile = getImageFileFromName(imageMap,SelName);
	//set the image
	var theImage = document.getElementById("DD_" + widgetID);
	if (theImage) {
		//change image src
		theImage.src = selectedImageFile;
		theImage.alt =  SelName;
		theImage.title = SelName;
	}
}

/* Method retrieves the global variable 
ProdChoiceImageMap
and returns 2-d array of image/file name */
function getProdChoiceImageMap(imageList) {
	
	//check if we have any data to parse
	if (imageList != '') {
		//first split into temp array
		var tempMap = imageList.split(",");
		//create image map array
		var imageMap = new Array(tempMap.length);
		//split and populate array
		for(var i=0; i < tempMap.length; i++) {
			//and newster array into array
			imageMap[i] = new Array(2);
			//split contents into map
			var arr = tempMap[i].split("=");
			for (var x=0; x < arr.length;x++) {
				imageMap[i][x] = arr[x];
			}
		}
	}
	return imageMap;
}



/* method to return image match from image match */
function getImageFileFromName(imageMap,imageName) {
	var imageFileName = "";
	if (imageName != "") {
	
		for (var i=0;i<imageMap.length;i++) {
			if (imageName == imageMap[i][0]) {
				imageFileName = imageMap[i][1];
				break;
			}
			else {
				imageFileName = "blank.png";
			}
		}
	}
	return imageFileName;
}





