var wordingChunks = new Array();
var wordingUndo = new Array();

function loadAndAddWordingChunk(nodeID) {
	if(wordingChunks[nodeID] === undefined) {
		$('img.ajaxLoading').show();
		$.ajax({
			url: '/processors/Adviser/GetSuitabilityWording?nodeid=' + nodeID,
			dataType: 'html',
			cache: false,
			success: function(data) {
				wordingChunks[nodeID] = data;
				addWordingChunk(nodeID);
				$('img.ajaxLoading').hide();
			},
			error: function(request, status, error) {
				$('img.ajaxLoading').hide();
			}
		});
	} else {
		addWordingChunk(nodeID);
	}
}

function addWordingChunk(nodeID){
	//alert("add chunk " + nodeID + " " + wordingChunks[nodeID]);
	var text = wordingChunks[nodeID];
	// Have moved these to further on in the function as it wasn't working in IE (due to the unescape)
	//text = text.replace(/##/g,'\r');	
	//text = text.replace(/@@/g,'\n');
	var amphersand = '&amp;'
	text = text.replace(amphersand,'&');
	
	var memo = document.getElementById("selectedSuitabilityWording");
	wordingUndo.push(memo.value);
	
	if(memo){
		// unescape the html
		var unEscape = document.createElement("DIV");
		unEscape.innerHTML = text;
		if(unEscape.innerText)
			text = unEscape.innerText;
		else
			text = unEscape.textContent;
			
		// Javascripts newline character is \n, so ignore the \r
		text = text.replace(/@@/g,'\n');
		text = text.replace(/##/g,'');
		
		memo.value += text + "\n\n";
	}
	
	resizeWordingMemo();
}

function resizeWordingMemo(){
	var memo = document.getElementById("selectedSuitabilityWording");
	
	var text = memo.value;
	var lines = 0;
	for(var i=0;i < text.length;i++){
		if(text[i] == '\n'){
			lines++;
		}
	}	
	
	//alert(lines);
}

function copySuitabilityWording(){
	var memo = document.getElementById("selectedSuitabilityWording");

	copy(memo);
	//copy_clip(memo.value);
	// currently only works in IE :(
	/*
	Copied = memo.createTextRange();
	Copied.execCommand("Copy");
	*/	
	
	alert("The Suitability Wording you selected has now been copied to your clipboard so you can paste it into any application you require.");	
}

function clearSuitabilityWording(){
	var memo = document.getElementById("selectedSuitabilityWording");
	memo.value = "";
}

function undoSuitabilityWording(){
	var memo = document.getElementById("selectedSuitabilityWording");
	memo.value = wordingUndo.pop();
}



function copy(inElement) {
  if (inElement.createTextRange) {
    var range = inElement.createTextRange();
    //if (range && BodyLoaded==1){
		range.execCommand('Copy');
    //}
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="/wcm/site/csi/ifa/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

