// $Revision: 1 $ $Date: 2010-08-21 15:39:36+01:00 $ $Workfile: TT_DragDrop.js $

var conditionSep ='__';

function ttNewAppWindow()
{					
	window.open(window.location.href);	
}

function ttdd_GetXMLHttpRequest() 
{
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		if (window.Ajax_XMLHttpRequestProgID) {
			return new ActiveXObject(window.Ajax_XMLHttpRequestProgID);
		} else {
			var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
			for (var i = 0; i < progIDs.length; ++i) {
				var progID = progIDs[i];
				try {
					var x = new ActiveXObject(progID);
					window.Ajax_XMLHttpRequestProgID = progID;
					return x;
				} catch (e) {
				}
			}
		}
	}
	return null;
}

function ttdd_getPos(e){
	
	try
	{
	for (var lx=0,ly=0;e!=null;
		lx+=e.offsetLeft,ly+=e.offsetTop,e=e.offsetParent);
	}
	catch(e){}
	
	return {x:lx,y:ly}
}

function ttdd_getTargetPosition(e)
{
	var found =false;
	var id = new String("");
	var removeToolbar=false;
	while(found!=true)
	{
		id= e.id;
		if(id.indexOf("MainContextTarget") !=-1 
			|| id.indexOf("DockDiv") !=-1 || id.indexOf("edgeDIV") !=-1)	
		{			
			found=true;
			if(id.indexOf("DockDiv") !=-1)
				removeToolbar=true;
		}
		if(found==false)
			e=e.offsetParent;
	}	
	var pos =ttdd_getPos(e);	
	if(removeToolbar)
		pos.y+=20;
	return pos;
}

function ttdd_getTarget(e)
{
	var found =false;
	var id = new String("");
	while(found!=true)
	{
		id= e.id;
		if(id.indexOf("MainContextTarget") !=-1 
			|| id.indexOf("DockDiv") !=-1 || id.indexOf("edgeDIV") !=-1)				
			found=true;
		if(found==false)
			e=e.offsetParent;
	}	
	return e;
}

function ttdd_getTargetOffSet(e)
{
	var found =false;
	var id = new String("");
	var lx=0;
	var ly=0;;
	while(found!=true)
	{
		id= e.id;
		if(id.indexOf("MainContextTarget") !=-1 
			|| id.indexOf("DockDiv") !=-1 || id.indexOf("edgeDIV") !=-1)				
			found=true;
		if(found==false)
		{
			e=e.offsetParent;
			lx+=e.offsetLeft;
			ly+=e.offsetTop;
		}
	}	
	return {x:lx,y:ly}
}

function ttdd_srcElement(evnt)
{
	return evnt.srcElement;
}

function ttdd_hideSelects(e)
{
	/*Hides all select objects within the page, so the user can drag and drop the
	relevant target obejcts*/
	for(var i=0;i<e.getElementsByTagName("select").length;i++)
	{
		e.getElementsByTagName("select").item(i).style.visibility = "hidden";
	}	
}

function ttdd_showSelects(e)
{	
	/*	Makes the select objects visible, when the user has completed a drag and
		drop task*/
	for(var i=0;i<e.getElementsByTagName("select").length;i++)
	{	
		if(e.getElementsByTagName("select").item(i).id.indexOf("Rs") ==-1)
			e.getElementsByTagName("select").item(i).style.visibility = "";
	}	
}

function ttdd_checkNumeric(item)
	// does not allow any non numeric values in itemId
	{
		var m_textStr;
		var m_char;
		var m_dp;
		var m_neg = false;
		var m_textStr2;
        var tempChar =String.fromCharCode(4);
        var re = new RegExp(tempChar, 'gi');
		
		//first try and parse out the format plumbed in on the server (000.00) for negatives		
		m_textStr = ttdd_parseInputCurrencyToNumeric(item.value);
		m_dp = false;
		for (i=0; i<m_textStr.length; i++)
		{
			m_char = m_textStr.charAt(i);
			//allow 1 decimal point only.
			if (m_textStr.charCodeAt(i) == 46)
			{
				if (m_dp)
				{
					m_textStr2 = m_textStr.substr(i+1);
					while (m_textStr2.indexOf(m_char)>=0)
					// loop through incase user has held down a key
					{
						m_textStr2 = m_textStr2.replace(m_char, tempChar);
					}
					m_textStr = m_textStr.substr(0,i)+m_textStr2;
					item.value = m_textStr;
				}
				else
				{
					m_dp = true;
				}
			}
			else if (((m_textStr.charCodeAt(i) < 48) || (m_textStr.charCodeAt(i) > 57)) 
			        && m_char != tempChar)
			{
				//allow one negative sign only
				if (m_textStr.charCodeAt(i) == 45)
				{
					if (m_neg)
					{
						m_textStr2 = m_textStr.substr(i+1);
						while (m_textStr2.indexOf(m_char)>=0)
						// loop through incase user has held down a key
						{
							m_textStr2 = m_textStr2.replace(m_char, tempChar);
						}
						m_textStr = m_textStr.substr(0,i)+m_textStr2;
						item.value = m_textStr;
					}
					else
					{
						m_neg = true;
					}
				} 
				else
				{
					while (m_textStr.indexOf(m_char)>=0)
					// loop through incase user has held down a key
					{
						m_textStr = m_textStr.replace(m_char, tempChar);
					}
					item.value = m_textStr;
				}
			} 
		}
		
		m_textStr = m_textStr.replace(re, "");
		
		if(item.value != m_textStr)
		{
		    item.value = m_textStr;
		}
	}
	
function ttdd_MoveToEnd(id)
{
	Element = document.getElementById(id);
	
	if(Element.type =="text")
	{
		if ( Element.createTextRange )
			Element.createTextRange().text += "";
		else if ( Element.insertionPoint )
			Element.insertionPoint = Element.text.length; 
	}
}	


function ttdd_MoveToEndFromKeyPress(id)
{
	try
	{
		if(document.getElementById(id) !=null)
		{
			document.getElementById(id).focus();	
			var m_range = document.getElementById(id).createTextRange();
			m_range.collapse(true);
			m_range.moveEnd('character', document.getElementById(id).value.length);
			m_range.moveStart('character', document.getElementById(id).value.length);
			m_range.select();
		}
	}
	catch(er){}
}

function ttdd_MoveToEndFromKeyPress2(object)
{
	try
	{		
		object.focus();	
		var m_range = object.createTextRange();
		m_range.collapse(true);
		m_range.moveEnd('character', object.value.length);
		m_range.moveStart('character', object.value.length);
		m_range.select();
		
	}
	catch(er){}
}

function ttdd_SetFocus(id)
{
	try
	{
		if(document.getElementById(id) !=null)
		{
			document.getElementById(id).focus();	
			var m_range = document.getElementById(id).createTextRange();
			m_range.collapse(true);
			m_range.moveEnd('character', document.getElementById(id).value.length);
			m_range.moveStart('character', 0);
			m_range.select();
		}
	}
	catch(er){}
}

function ttdd_ho(item, style)
{
	item.className=style
}

function ttdd_HLStd(item, style,controlId,columnId)
{	
	document.getElementById("Stdhidden").value =controlId +"," + columnId;
	item.className=style	
}

function tdd_lo(item,style)
{
	item.className=style
}

function ttdd_forceCase(item)
{
	try
	{
		if(event.keyCode != 37 && event.keyCode != 39 && event.keyCode != 46 && event.keyCode != 8 )
		{
			item.value= item.value.toUpperCase()
		}
	}
	catch(e){}
}

function ttdd_raiseLinkPostBack(linkId)
{
	linkId =linkId.replace(/_/g,"\$");
	linkId =linkId.replace("$ct","_ct");
	__doPostBack(linkId,'');
}

function ttdd_raiseLinkPostBack2(linkId)
{
	linkId =linkId.replace(/_/g,"\$");
	linkId =ttdd_formatPostBack(linkId);
	__doPostBack(linkId,'');
}



function ttdd_formatPostBack(linkId)
{
	var complete =false;
	while(!complete)
	{
		if(linkId.indexOf("$ct") !=-1)
			linkId =linkId.replace("$ct","_ct");								 
		else
			complete= true;
	}
	return linkId;
}

function ttdd_SelectItem(columnId)
{
	try{
	document.getElementById(columnId).focus();	
	ttdd_MoveToEnd(columnId)
	}
	catch(e){}
}

function ttdd_SelectItemOnly(columnId)
{
	try{
	document.getElementById(columnId).focus();		
	}
	catch(e){}
}

function ttdd_raiseClientPostBack(id)
{
	var link = document.getElementById(id);
	var href = link.href;
	href= href.replace("javascript:__doPostBack(","");
	href= href.replace("javascript:__doPostBack(","");
	var split = href.split(",");
	split[0] =split[0].replace("'","");
	split[0] =split[0].replace("'","");
	__doPostBack(split[0],'');
}

function ttdd_raiseClientButtonPostBack(button)
{
	var link = document.getElementById(button.attributes.getNamedItem("LinkId").value);
	var href = link.href;
	href= href.replace("javascript:__doPostBack(","");
	href= href.replace("javascript:__doPostBack(","");
	var split = href.split(",");
	split[0] =split[0].replace("'","");
	split[0] =split[0].replace("'","");
	__doPostBack(split[0],'');
}

function ttdd_parseInputCurrencyToNumeric(currencyValue)
{
    var CURRENCY_NEG_OPEN = "(";
    var CURRENCY_NEG_CLOSE = ")";   
    var CURRENCY_SEPERATOR = ",";
    var CURRENCY_NEG_CHAR = "-";

    if (currencyValue!="")
    {
        var processAsNegative = false;
        var position = currencyValue.indexOf(CURRENCY_NEG_OPEN);
        if (position != -1)
        {
            if (currencyValue.indexOf(CURRENCY_NEG_CLOSE) != -1)
            {
                //2 brackets - consider this a negative value
                processAsNegative = true;
            }
            else
            {
                //if brace was first char then assume the last was deleted for change to value
                processAsNegative = (position == 0);
            }
           
        }
        else
        {
            processAsNegative = (currencyValue.indexOf(CURRENCY_NEG_CLOSE)==currencyValue.length-1);
        }

        //remove any superflous commas
        currencyValue = currencyValue.replace(CURRENCY_SEPERATOR, "");
        currencyValue = currencyValue.replace(/ /gi, "");
        //remove the negative braces and negate value
        if (processAsNegative)
        {
            currencyValue = currencyValue.replace(CURRENCY_NEG_OPEN, "");
            currencyValue = currencyValue.replace(CURRENCY_NEG_CLOSE, "");
            currencyValue = CURRENCY_NEG_CHAR + currencyValue;
        }
    }
    return currencyValue;
}
