var strURLBaseER = "www.eresearch.ro"
var iframeID = "iframeERSyncCookies";
var iframeURLBase = "http://" + strURLBaseER + "/webc/synccookies.php?cn="
var strHidden =""
strHidden  = "frameborder=\"0\" scrolling=\"no\" width=\"0\" height=\"0\" style=\"display:none;\""
var strHTMLIframe="<iframe  " + strHidden + " id=\"" + iframeID + "\" src=\"" +iframeURLBase + "$cn&cv=$cv\" onload=\"iframeonload();\"></iframe>";
var cnt  = 0;
var frameSync ;

var resultCookieValue = null;

function iframeonload()
{
	if(!frameSync)
		frameSync = document.getElementById(iframeID);
	
//	xdebug(frameSync.restoreddomain, "frameSync.restoreddomain",true)
	
	if (!frameSync .restoreddomain)
	{		
		//alert("!iframe.restoreddomain")
		frameSync .restoreddomain=true
		
		var bOK = true;
		try{
			frameSync .contentWindow.location ="elements/blank.html";
		}catch(ee)		{bOK  = false;}
		if(!bOK)				frameSync .src = "elements/blank.html";
	}
	else
	{
		//alert("iframe.restoreddomain")		
		syncCookie_part_2();
		}
		
	cnt  ++
};

var syncCookieState  = 0;
function syncCookie(cookieName, cookieValue , stateExpected, callback)
{
	//0 : nothing 
	if (syncCookieState  == 0 )
	{
		syncCookieState  =1 
		syncCookie_part_1(cookieName, cookieValue);
		syncCookieState  =2
		// wait for state 2 after syncCookie_part_2
		setTimeout(function(){syncCookie(cookieName, cookieValue, 3,callback)},100);
	}
	else if (syncCookieState == 2)
	{
		// still waiting
		setTimeout(function(){syncCookie(cookieName, cookieValue, 3,callback)},100);
	}
	else if (syncCookieState == 3)
	{
		//finnaly
		if(callback)
			callback();
		syncCookieState = 0;
	}	
		
}

//function syncCookie(cookieName, cookieValue)
//{
//	resultCookieValue = null;
//	syncCookie_part_1(cookieName, cookieValue)
//	
//}
function syncCookie_part_1(cookieName, cookieValue)
{
	var bOK = true;
	var bJustBuilt = false;
	frameSync = document.getElementById(iframeID);
	bOK = (frameSync != null);
	if (!bOK)
	{
		xdebug("iframe does not exist. will build");
		buildIFrame(cookieName, cookieValue);
		frameSync = document.getElementById(iframeID);
		
		bOK = (frameSync != null);
		bJustBuilt  = true;
	}
	if(!bOK)
	{
		
		xdebug("iframe does not exist :(");
		return;
	}
	
	bJustBuilt=false;
	if(!bJustBuilt )
	{
		frameSync.restoreddomain = false;
		xdebug(frameSync.src , "frameSync.src ");
		var strURL  = iframeURLBase + "$cn&cv=$cv"
		strURL  = strURL .replace("$cn", cookieName);
		strURL  = strURL .replace("$cv", cookieValue);
		frameSync.src  = strURL;
		
		xdebug(frameSync.src , "frameSync.src ");
	}
	
}
function syncCookie_part_2()
{
	try
	{
		//bDebug = true;
//		var frm = document.getElementById("dbgForm"); 
//		
//		xdebug(window.name , "window.name" , 33, frm);
//		xdebug(frameSync.contentWindow.name,"frameSync.contentWindow.name", 33, frm);
		
		if (frameSync.contentWindow.name != '')
		{
			resultCookieValue = frameSync.contentWindow.name ;
			}
		
		//document.write( frameSync.contentWindow.name +"frameSync.contentWindow.name");
		//alert(frm)
	  //alert(frameSync.contentWindow.name +"frameSync.contentWindow.name")
	   // document.write (frameSync.contentWindow.name +"frameSync.contentWindow.name")
	 	//bDebug = false;
	}catch(ee){
		alert(ee)		
	}
	syncCookieState  = 3
}

function buildIFrame(cookieName, cookieValue)
{
	var str = strHTMLIframe.replace("$cn", cookieName);
	str = str.replace("$cv", cookieValue);
	var d = document.createElement("DIV");
	d.innerHTML = str;
	inAppendChild = true;
	document.documentElement.appendChild(d);
	inAppendChild = false;
	frameSync= document.getElementById(iframeID);
}
