var theBrowser=BrowserIdentify();
var theImageUrl="";

function BrowserIdentify()
{
 if (document.layers) return "NS";
 if (document.all) return("IE")
 if (document.getElementById) return "MOZ";
 return("OTHER");
}

 function DoWaitTimer() {
	STEP=0;
	PhoneIDCtl="0";
	if (PhoneIDCtl!="done") 
		timerID = window.setTimeout("NextStep()", 1500); // 1,5-second interval
 }


 function NextStep() {
	
	if (STEP==0 && PhoneIDCtl!="0") {
		
		if (PhoneID>0) {
			// ok, let's wait for call
			start_login_process();
		}
		else { // if negative response, keep waiting on list

  			SetInnerText("responseContainer","Please Wait... Searching for free number, please keep waiting");
			STEP=2;	// lines are all busy, step 2 keeps waiting for free number...
	 		timerID = window.setTimeout("NextStep()", 500); // 0,5 - second interval
		}
			
	}
	else
	if (STEP==2) {	// WaitOnList
	
		negPhoneID=-PhoneID;  // it was negative...
		jsrsExecute("saintlogin_php.ph", WaitOnList_Response,"WaitOnList",Array(negPhoneID));
	
	    
	}
	else
	if (STEP==0)
		timerID = window.setTimeout("NextStep()", 1500); // 1,5-second interval
	
 }

 // After image is loaded, let's wait for user calling

 function imageloaded() {

	timerID = window.setTimeout("StartWaiting()", 1500); // additional 1,5-second interval for image display !

 }

function WaitOnList_Response(returnstring)
{
    PhoneID=returnstring;
    jsrsExecute("saintlogin_php.php", GetPhoneNumber_Response,"GetPhoneNumber",Array(PhoneID));
	
}

// only called to get imageurl if user was place in waiting list...
function GetPhoneNumber_Response(returnstring)
{
   theImageUrl=returnstring;
   STEP=0;	// got phone id, back to step one
   timerID = window.setTimeout("NextStep()", 500); // 0,5 - second interval
}

function StartWaiting() {

   window.clearTimeout(timerID);
   jsrsExecute("saintlogin_php.php", WaitForCall_Response,"WaitForCall",Array(PhoneID));

}

 function WaitForCall_Response(returnstring) {
 
//	document.write(returnstring);
	document.location.href=returnstring;

 }
 
// DIV inner functions used for compatibility between NS, mozilla and IE

function SetInnerText(string,value)
{
//	document.getElementById(string).childNodes[0].nodeValue=value;
	document.getElementById(string).innerHTML =value;
}

function GetInnerText(string)
{
//	return(document.getElementById(string).childNodes[0].nodeValue);

	return(document.getElementById(string).childNodes[0].nodeValue);
}


// The Magic starts here after the LoginForm submit !

function get_PhoneID(returnstring) {
	
// returned string from the GetPhoneID_Url webservice method
// has the form  <phoneid>|<image url>
// (phoneid and image url separated by a 'pipe' symbol)

   return_array=returnstring.split("|");
   PhoneID=return_array[0];
   theImageUrl=return_array[1];	// it's a global, used in DisplayPhoneNumber
   
   STEP=0;
   SetInnerText("responseContainer","Please Wait...");

   document.body.style.cursor = "wait";

  if (theBrowser=="IE") 
	 document.LoginForm.loginbutton.disabled = true;

 // Set Innertext so that timer will generate next post request in NextStep()
   PhoneIDCtl=PhoneID;

}

// function to display image containing the number to be dialled 

function start_login_process()
{
	
	DisplayPhoneNumber(theImageUrl);
    
	// Ok, the number to call is displayed in this image :

	document.body.style.cursor = "wait";
    SetInnerText("responseContainer","Please dial this number ");

}

function DisplayPhoneNumber(returnstring)
{

    SetInnerText("ImageContainer","<IMG alt=\"\" src=\""+returnstring+"\" onload=imageloaded(); >");
//    SetInnerText("ImageContainer",returnstring);

}

