// tagpay.js  2008 (C) TAGATTITUDE  

// Tips Management --------------------------------------------------

// globals
var PosBullHoriz = 0;
var PosBullVerti = 0;
var TppTipState = 0;
var tppDisplayX = 0;
var tppDisplayY = 0;
var tppTipText = "";
var tppCount = 0;
var tppInfoPlusOff = 0;

var tpponload = null;

// Timer
var tppTipDelay;
var tppDelayTimer = 750;
var tppAutoTimer  = 9000;

// temporary variables
var tdScript = "";
var tdScript2 = "";
var tdId = "";
var tdId2 = "";


function tppMouseNS( ev )
{  
	var x = ev.pageX;
	var y = ev.pageY;
    
    if ( TppTipState > 0 )
    {
    	if ( ( Math.abs (tppDisplayX - x ) > 20 ) || ( Math.abs (tppDisplayY - y ) > 20 ) )
    		tppTipOff();
    }
    
    PosBullHoriz = x;
    PosBullVerti = y;
}

function tppMouseIE( ev )
{  
//	x = event.x + document.documentElement.scrollLeft; // or document.body.scrollLeft regarding DOCTYPE
//	y = event.y + document.documentElement.scrollTop;
	

	y = event.clientY + document.documentElement.scrollTop  - document.documentElement.clientTop;
	x= event.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft;



    if ( TppTipState > 0 )
    {
    	if ( ( Math.abs (tppDisplayX - x ) > 20 ) || ( Math.abs (tppDisplayY - y ) > 20 ) )
    		tppTipOff();
    }
	PosBullHoriz = x;
    PosBullVerti = y;
}

function tppTip( Tip )
{
	tppDisplayX = PosBullHoriz;
	tppDisplayY = PosBullVerti;
	
//	if ( TppTipState == 0 )
	{
		var infoBull = document.getElementById("infoBull"); 
		tppTipText = Tip;
		clearTimeout( tppTipDelay );
		tppTipDelay = setTimeout( 'TipTimer()', tppDelayTimer );
    	TppTipState = 1;
	}                                               
}

function tppTipClick( Tip )
{
	tppDisplayX = PosBullHoriz;
	tppDisplayY = PosBullVerti;
	
//	if ( TppTipState == 0 )
	{
		var infoBull = document.getElementById("infoBull"); 
		tppTipText = Tip;
    	TppTipState = 1;
    	TipDisplay();
	}                                               
}

function TipDebug( Text )
{  
	var TppDebug = document.getElementById("tppDebug");
    tppCount++;
    var s = TppDebug.innerHTML;
    if ( s.length > 300 ) s = s.slice(100); 
    TppDebug.innerHTML = s + "<br/>" + tppCount + " : " + Text;
    
}

function TipTimer()
{  
   if ( TppTipState == 1 )
    {
    	TipDisplay();
    	tppTipDelay = setTimeout( 'TipTimer()', tppAutoTimer );
    	TppTipState = 2;
    }
    else if ( TppTipState == 2 )
    {
    	tppTipOff();
    	TppTipState = 0;
    }
}

function TipDisplay( Tip )
{
	var infoBull = document.getElementById("infoBull"); 
	var ombrBull = document.getElementById("ombrBull"); 
	infoBull.innerHTML = tppTipText;      
//    ombrBull.innerHTML = tppTipText;
    infoBull.style.top = PosBullVerti + 10 + "px";      
    infoBull.style.left = PosBullHoriz + 10 + "px";      
//    ombrBull.style.top = PosBullVerti + 15 + "px";  
//    ombrBull.style.left = PosBullHoriz + 18 + "px";  
	infoBull.style.visibility = "visible";
//	ombrBull.style.visibility = "visible";
}


function tppTipOff()
{
	var infoBull = document.getElementById("infoBull"); 
	var ombrBull = document.getElementById("ombrBull"); 
	infoBull.style.visibility = "hidden";
	ombrBull.style.visibility = "hidden";
	clearTimeout( tppTipDelay );
   	TppTipState = 0;
}

function tppInfoPlus( Idx )
{
	if ( ! tppInfoPlusOff )
	{
		var infoElement = document.getElementById( "td" + Idx );
		var plusElement = document.getElementById( "a" + Idx );
		if ( infoElement.style.display == "none" )
		{
			infoElement.style.display = "";
			plusElement.innerHTML = "&nbsp;-&nbsp;";
		}
		else
		{
			infoElement.style.display = "none";
			plusElement.innerHTML = "&nbsp;+&nbsp;";
		}
	}
}

function tppInfoPlusAjax( Idx, script )
{
	if ( ! tppInfoPlusOff )
	{
		var infoElement = document.getElementById( "td" + Idx );
		var plusElement = document.getElementById( "a" + Idx );
		if ( infoElement.style.display == "none" )
		{
			infoElement.style.display = "";
			plusElement.innerHTML = "&nbsp;-&nbsp;";
			tppAjax( script, "tddiv" + Idx );
		}
		else
		{
			infoElement.style.display = "none";
			plusElement.innerHTML = "&nbsp;+&nbsp;";
		}
	}
}


function tppNoInfoPlus()
{
	tppInfoPlusOff = 1;
}



function tppSubmitEnter( myfield, e )
{
	var keycode;
	if ( window.event ) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if ( keycode == 13 )
	{
	   myfield.form.submit();
	   return false;
	}
	else
	   return true;
}

function tppUpdateField( myField, myMessage, oldValue )
{
	var yourValue = prompt( myMessage, oldValue );
  	if ( ( yourValue != null ) && ( yourValue != "" ) )
  	{
  		myField.value +=  " " + yourValue;
     	myField.form.submit();
     	return true;
  	}
	return false;
}

function tppDeleteField( myField, myMessage )
{
	var valueElement = document.getElementById( "delTerminal" );
	var result = confirm( myMessage );
  	if ( result == true )
  	{
  	    valueElement.value = myField;
     	myField.form.submit();
     	return true;
  	}
	return false;
}

function tppConfirmField( myField, myMessage )
{
	var valueElement = document.getElementById( "hConfirm" );
	var myForm = document.getElementById( "tppForm" );
	var result = confirm( myMessage );
  	if ( result == true )
  	{
  	    valueElement.value = myField;
     	myForm.submit();
     	return true;
  	}
	return false;
}

function tppConfirmFieldAjax( id, script, myMessage )
{
	var result = confirm( myMessage );

	if ( result == true )
  	{
  		tppAjax( script, id )
     	return true;
  	}
	return false;
}

function tppPromptFieldAjax( id, script, myMessage, id2, script2 )
{
	var promptElement = document.getElementById( "tppStateConfirmId" + id );
	if ( promptElement )
	{
		promptElement.style.display = "";
		tdId = id;
		tdId2 = id2;
		tdScript = script;
		tdScript2 = script2;
		return true;
	}
	else
	{
		var result = prompt( myMessage, "" );
	  	if ( result.length > 0 )
	  	{
	  		tppAjax2( script + "&prompt=" + result, id, script2, id2 )
	     	return true;
	  	}
	}
	return false;
}


function tppCancelBoxAjax( id )
{
	var promptElement = document.getElementById( "tppStateConfirmId" + id );
	if ( promptElement )
	{
		promptElement.style.display = "none";
	}
}


function tppCancelPromptAjax( id )
{
	var promptElement = document.getElementById( "tppPromptAjaxId" + id );
	if ( promptElement )
	{
		promptElement.innerHTML = "";
	}
}


function tppConfirmBoxAjax( id, script, id2, script2 )
{
	var promptElement = document.getElementById( "tppStateConfirmId" + id );
	if ( promptElement )
	{
		var raisonElement = document.getElementById( "tppRaisonId" + id );
		if ( raisonElement )
		{
			var prompt = raisonElement.value;
			if ( prompt.length > 0 )
			{
				tppAjaxPost2( script + "&prompt=" + prompt, id, script2, id2 )
			}
		}
	}
}




function tppPromptAndConfirmFieldAjax( id, script, myMessage, myMessage2, id2, script2 )
{
	var result = prompt( myMessage, "" );

  	if ( result.length > 0 )
  	{
  		var result2 = confirm( myMessage2 );
  		if ( result2 == true )
  		{	
  			tppAjax2( script + "&prompt=" + result, id, script2, id2 );
  			return true;
  		}
  	}
	return false;
}


function tppValidateField( myField, Message1, Message2 )
{
	var valueElement = document.getElementById( "validateid" );
	var valueCheck = document.getElementById( "idcheck" + myField );
	var myMessage = Message1;
	if  ( ! valueCheck.checked ) myMessage = Message2;
	var result = confirm( myMessage );
  	if ( result == true )
  	{
  		if  ( valueCheck.checked ) myField += ",1";
  		else  myField += ",0";
  	    valueElement.value = myField;
  	    valueElement.form.submit();
     	return true;
  	}
  	else
  	{
  		if  ( valueCheck.checked ) 	valueCheck.checked = false;
  		else valueCheck.checked = true;
  	}
	return false;
}


function tppSmsEmpty()
{
	var smsEmpty = document.getElementById( "smsEmpty" );
	var smsFree = document.getElementById( "smsFree");
	if ( smsEmpty.value == "1" )
	{
		smsEmpty.value = "0";
		smsFree.value = "";
		smsFree.style.fontStyle = "normal";
		smsFree.style.fontWeight = "normal";
//		smsFree.style.fontSize = "8pt";
		smsFree.style.color = "#222222";
	}
}

function tppInitPassword()
{
	var password = document.getElementById( "password" );
	var password2 = document.getElementById( "password2" );
	if ( password && password2 )
	{
		password.value = "";
		password2.value = "";
	}
}


function tppSmsSize( max )
{
	var smsFree = document.getElementById( "smsFree" );
	var smsFix = document.getElementById( "smsFix" );
	var smsFixSav = document.getElementById( "smsFixSav" );
	var smsCount = document.getElementById( "smsCount");
	var smsCheck = document.getElementById( "smsCheck" );
	var smsLocation = document.getElementById( "smsLocation" );
	var location = "";
	if ( smsLocation.selectedIndex >= 0 ) location = "<br/>" + smsLocation.options[smsLocation.selectedIndex].text;
	if ( location.length == 5 ) location = '';

	var smsVisibility = "visible";
	if ( ! smsCheck.checked )
	{
		smsVisibility = "hidden";
		smsSize = 0;
	}
	else
	{
		var s = smsFixSav.value + location;
		var smsSize = s.length + 4 + smsFree.value.length;
		if ( smsSize >= max )
		{
			var m = max - s.length ; 
			smsFree.value = smsFree.value.substring( 0, m );
			smsSize = s.length + 4 + smsFree.value.length;
		}
		smsFix.innerHTML = s + "<br/>" + smsFree.value;
	}
	smsCount.innerHTML = smsSize;
	smsFree.style.visibility = smsVisibility;
	smsFix.style.visibility = smsVisibility;

}


// Get the HTTP Object for AJAX
//-----------------------------
function getHTTPObject()
{
	// source from http://openweb.eu.org/articles/objet_xmlhttprequest
	var xmlhttp = false;

	 /* Compilation conditionnelle d'IE */
	 /*@cc_on
	 @if (@_jscript_version >= 5)
	    try
	    {
	       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e)
	    {
	       try
	       {
	          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	       }
	       catch (E)
	       {
	          xmlhttp = false;
	       }
	    }
	 @else
	    xmlhttp = false;
	 @end @*/
	
	 /* on essaie de cr�er l'objet si ce n'est pas d�j� fait */
	 if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	 {
	    try
	    {
	       xmlhttp = new XMLHttpRequest();
	    }
	    catch (e)
	    {
	       xmlhttp = false;
	    }
	 }
	// source from http://openweb.eu.org/articles/objet_xmlhttprequest
	 return  xmlhttp;
}

// Ajax
//-----
function tppAjax( script, elementId )
{
	var tppHttpObject = getHTTPObject();

	if ( tppHttpObject )
	{
		var now = new Date();
	  	var time = now.getTime();
	  	tppHttpObject.open( "GET", script, true );
	  	tppHttpObject.send( null );
	  	tppHttpObject.onreadystatechange = function()
	  	{
	  		if ( tppHttpObject.readyState == 4 ) // complete
	  		{
	  			var tppAjaxElement = document.getElementById( elementId );
	  			if ( tppAjaxElement )
	  			{
	  				tppAjaxElement.innerHTML = tppHttpObject.responseText;
	  			}
	  		}
	  	}
	}
}

//Ajax
//-----
function tppAjaxPost( script, elementId )
{
	var tppHttpObject = getHTTPObject();

	if ( tppHttpObject )
	{
		var now = new Date();
	  	var time = now.getTime();
		var param = script.split( "?" );
		
	  	tppHttpObject.open( "POST", param[0], true );
	  	tppHttpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//	  	tppHttpObject.setRequestHeader("Connection", "close");
	  	if ( param[1] )
	  	{
		  	var p = param[1];
		  	tppHttpObject.setRequestHeader("Content-length", p.length);
	  		tppHttpObject.send( param[1] );
	  	}
	  	else
	  	{
		  	tppHttpObject.setRequestHeader("Content-length", 0);
	  		tppHttpObject.send( null );
	  	}

	  	tppHttpObject.onreadystatechange = function()
	  	{
	  		if ( tppHttpObject.readyState == 4 ) // complete
	  		{
	  			var tppAjaxElement = document.getElementById( elementId );
	  			if ( tppAjaxElement )
	  			{
	  				tppAjaxElement.innerHTML = tppHttpObject.responseText;
	  			}
	  		}
	  	}
	}
}



//Ajax
//-----
function tppAjaxSelect( script, elementId )
{
	var tppHttpObject = getHTTPObject();

	if ( tppHttpObject )
	{
		var now = new Date();
	  	var time = now.getTime();
	  	var tppAjaxElement = document.getElementById( elementId );
	  	if ( tppAjaxElement )
	  	{
	  		script += tppAjaxElement.value;
	  	}
	  	tppHttpObject.open( "GET", script, true );
	  	tppHttpObject.send( null );
	  	tppHttpObject.onreadystatechange = function()
	  	{
	  		if ( tppHttpObject.readyState == 4 ) // complete
	  		{
	  			var tppAjaxElement = document.getElementById( elementId );
	  			if ( tppAjaxElement )
	  			{
	  				tppAjaxElement.selectedIndex = tppHttpObject.responseText;
	  			}
	  		}
	  	}
	}
}



//Ajax with 2 sequential scripts
//------------------------------
function tppAjax2( script, elementId, script2, elementId2 )
{
	var tppHttpObject = getHTTPObject();

	if ( tppHttpObject )
	{
		var now = new Date();
	  	var time = now.getTime();
	  	tppHttpObject.open( "GET", script, true );
	  	tppHttpObject.send( null );
	  	tppHttpObject.onreadystatechange = function()
	  	{
	  		if ( tppHttpObject.readyState == 4 ) // complete
	  		{
	  			var tppAjaxElement = document.getElementById( elementId );
	  			if ( tppAjaxElement )
	  			{
	  				tppAjaxElement.innerHTML = tppHttpObject.responseText;
	  			}
	  			if ( script2 )
	  				tppAjax( script2, elementId2 );
	  		}
	  	}
	}
}

//Ajax with 2 sequential scripts
//------------------------------
function tppAjaxPost2( script, elementId, script2, elementId2 )
{
	var tppHttpObject = getHTTPObject();
	
	if ( tppHttpObject )
	{
		var now = new Date();
	  	var time = now.getTime();
		var param = script.split( "?" );
	  	tppHttpObject.open( "POST", param[0], true );
	  	tppHttpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//	  	tppHttpObject.setRequestHeader("Connection", "close");
	  	if ( param[1] )
	  	{
		  	var p = param[1];
		  	tppHttpObject.setRequestHeader("Content-length", p.length);
	  		tppHttpObject.send( param[1] );
	  	}
	  	else
	  	{
		  	tppHttpObject.setRequestHeader("Content-length", 0);
	  		tppHttpObject.send( null );
	  	}	  	
	  	tppHttpObject.onreadystatechange = function()
	  	{
	  		if ( tppHttpObject.readyState == 4 ) // complete
	  		{
	  			var tppAjaxElement = document.getElementById( elementId );
	  			if ( tppAjaxElement )
	  			{
	  				tppAjaxElement.innerHTML = tppHttpObject.responseText;
	  			}
	  			if ( script2 )
	  				tppAjaxPost( script2, elementId2 );
	  		}
	  	}
	}
}

//add border Div
//--------------
function tppAddBorderDiv( ori, oriDiv, topDiv, midDiv, botDiv )
{
	ori.className = oriDiv;

	var bor = document.createElement('div');
	bor.className = '';

	ori.parentNode.replaceChild(bor, ori);

	var top = document.createElement('div');
	top.className = topDiv;

	var mid = document.createElement('div');
	mid.className = midDiv;

	var bot = document.createElement('div');
	bot.className = botDiv;

	bor.appendChild(top);
	bor.appendChild(mid);
	mid.appendChild(ori);
	bor.appendChild(bot);
	
}

// add border
//------------
function tppAddBorder()
{
	var divs = document.getElementsByTagName('div');
	for( i = 0; i < divs.length; i++ )
	{
		if ( divs[i].className == "tppHalfInfoBorder" )
		{
			tppAddBorderDiv( divs[i], 'tppHalfInfoContent', 'tppHalfInfoTop', 'tppHalfInfoMiddle', 'tppHalfInfoBottom' );

		}
		if ( divs[i].className == "tppFullFormBorder" )
		{
			tppAddBorderDiv( divs[i], 'tppFullFormContent', 'tppFullFormTop', 'tppFullFormMiddle', 'tppFullFormBottom' );
		}
		if ( divs[i].className == "tppHalfFormBorder" )
		{
			tppAddBorderDiv( divs[i], 'tppHalfFormContent', 'tppHalfFormTop', 'tppHalfFormMiddle', 'tppHalfFormBottom' );
		}
		if ( divs[i].className == "tppError" && divs[i].innerHTML.length > 0 )
		{
			tppAddBorderDiv( divs[i], 'tppErrorContent', 'tppErrorTop', 'tppErrorMiddle', 'tppErrorBottom' );
		}
	}
}

if ( navigator.appName.substring(0,3) == "Net" )
	document.addEventListener("mousemove", tppMouseNS, true);
else document.onmousemove = tppMouseIE;  

window.onload = function()
{
	tppAddBorder();
	if (tpponload) tpponload();
}



// Tips management --------------------------------------------------

