//----------------------------------------------------------------------
// Calendar
//----------------------------------------------------------------------

var popUp; 
function OpenCalendar(idname, postBack, cultureInfoName)
{
	popUp = window.open('Calendar.aspx?formname=' + getMainForm().name + 
		'&id=' + idname + '&selected=' + getMainForm().elements[idname].value + '&postBack=' + postBack + '&cultureInfoName=' + cultureInfoName, 
		'popupcal', 
		'width=240,height=250,left=200,top=250');	
}

function SetDate(formName, id, newDate, postBack)
{
	eval('var theform = document.' + formName + ';');
	popUp.close();
	theform.elements[id].value = newDate;
	if (postBack)
	{
		__doPostBack(id,'');		
	}
}

function OpenFullScreen(url, trg, clsWnd)
{
	OpenPopUp(url, trg, 'resizable=yes,', window.screen.availWidth, (window.screen.availHeight-30), 0, 0, clsWnd);
}
function OpenPopUp(url, trg, startProp, w, h, l, t, clsWnd)
{
	var hndl=null;
	
	if(trg != '_parent')
		hndl = window.open (url, trg , startProp + 'width='+w+',height='+h+',left='+l+',top='+t);
		
	if (hndl == null)
	{
		location.replace(url);
	}
	else if(clsWnd)
	{
		window.close();
	}
}
function SendOrderFromPopUp()
{
	var url='OrderEdit.aspx?OrderSend=true';
	var wnd=null;
	
	if (opener && opener.name=='frmMain')
		wnd=opener;
	else if (opener && opener.frmMain)
		wnd=opener.frmMain;
	else if (self.frmMain) 
		wnd=frmMain;
	else 
		wnd=self;

	wnd.location.replace(url);

	if(wnd != self)
		window.close();
}

//----------------------------------------------------------------------
// Submit buttons with target
//----------------------------------------------------------------------


function SubmitWithTarget(targetName)
{	
	var f=getMainForm();
	f.desiredTarget=targetName;	
	if (!self.origPostBack){
		
		self.origPostBack=__doPostBack;
		self.__doPostBack=__repl;
	}
	return true;		
}

function getMainForm(){
	if (self.mainForm)
		return self.mainForm;
	return self.mainForm=document.forms[0];
}
function __repl(eventTarget, eventArgument){
	targetClearance(getMainForm());
	origPostBack(eventTarget, eventArgument);
}

function targetClearance(f){
	if (!f.desiredTarget)
		f.target='';
	else{		
		f.target=f.desiredTarget;		
	}
		
	f.desiredTarget='';
	return true;
}

/*
var formObject;
var formObjectTimeoutID;

function SubmitWithTargetOld(formObj, targetName){
	formObjectTimeoutID = window.setTimeout(ResetFormTarget,100)
	formObject = formObj;
	formObj.target = targetName;

}
function ResetFormTarget()
{
	window.clearTimeout(formObjectTimeoutID);
	formObject.target = '';
}
*/

//----------------------------------------------------------------------
//Speedometer
//----------------------------------------------------------------------

var CHECK_INTERVAL = 30000;
var CHECK_FILE = "Layout/Scheme1/Images/Others/Check.gif";
var CHECK_SIZE = 1269;
var SPEED_LIMITS = [9, 4.5, 3, 1.8, 1];//the last is actually not used...
var CHECK_NOCONNECTION = 60000;
var speedRequestTime = 0;
var timeoutID;
var noConnectionID;

function CheckSpeedStart()
{
	var theimage;
	speedRequestTime = (new Date()).getTime();
	//alert("Start");
	if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 
	{
		theimage = document.images["SpeedBenchMark"];
	}
	else 
	{
		theimage = document.images.SpeedBenchMark;
	}
	noConnectionID = window.setTimeout(NoConnection, CHECK_NOCONNECTION);
	theimage.src = 
		CHECK_FILE + "?reload="
		+ speedRequestTime;
}
function NoConnection()
{
	CheckSpeedOnLoad();
}
function CheckSpeedOnLoad()
{
	if(speedRequestTime == 0) {
		timeoutID = window.setTimeout(CheckSpeedDelayed, CHECK_INTERVAL);
		return;
	}
	window.clearTimeout(noConnectionID);
	miliSec = (new Date()).getTime() - speedRequestTime;
	speed = CHECK_SIZE / miliSec;
	if(speed >= SPEED_LIMITS[0])
	{//very good
		col1 = '#00FF00';
		col2 = '#00FF00';
		col3 = '#00FF00';
		col4 = '#00FF00';
		col5 = '#00FF00';
		tooltip = 'Verbindung: Sehr gut';
	}
	else if(speed >= SPEED_LIMITS[1])
	{//good
		col1 = '#00FF00';
		col2 = '#00FF00';
		col3 = '#00FF00';
		col4 = '#00FF00';
		col5 = '#FF0000';
		tooltip = 'Verbindung: Gut';
	}
	else if(speed >= SPEED_LIMITS[2])
	{//middle
		col1 = '#00FF00';
		col2 = '#00FF00';
		col3 = '#00FF00';
		col4 = '#FF0000';
		col5 = '#FF0000';
		tooltip = 'Verbindung: Durchschnittlich';
	}
	else if(speed >= SPEED_LIMITS[3])
	{//bad
		col1 = '#00FF00';
		col2 = '#00FF00';
		col3 = '#FF0000';
		col4 = '#FF0000';
		col5 = '#FF0000';
		tooltip = 'Verbindung: Schlecht';
	}
	else if(speed > CHECK_SIZE / CHECK_NOCONNECTION)
	{//very bad
		//alert("Speed: " + speed + ". nc speed: " + (CHECK_SIZE / CHECK_NOCONNECTION));
		col1 = '#00FF00';
		col2 = '#FF0000';
		col3 = '#FF0000';
		col4 = '#FF0000';
		col5 = '#FF0000';
		tooltip = 'Verbindung: Sehr schlecht';
	}
	else
	{//No connection
		col1 = '#FF0000';
		col2 = '#FF0000';
		col3 = '#FF0000';
		col4 = '#FF0000';
		col5 = '#FF0000';
		tooltip = 'Verbindung: Keine';
	}
	
	document.getElementById('Bar1').style.backgroundColor = col1;
	document.getElementById('Bar2').style.backgroundColor = col2;
	document.getElementById('Bar3').style.backgroundColor = col3;
	document.getElementById('Bar4').style.backgroundColor = col4;
	document.getElementById('Bar5').style.backgroundColor = col5;

	document.images["Img1"].alt = tooltip;
	document.images["Img2"].alt = tooltip;
	document.images["Img3"].alt = tooltip;
	document.images["Img4"].alt = tooltip;
	document.images["Img5"].alt = tooltip;
	
	document.images["Img1"].title = tooltip;
	document.images["Img2"].title = tooltip;
	document.images["Img3"].title = tooltip;
	document.images["Img4"].title = tooltip;
	document.images["Img5"].title = tooltip;
	
	timeoutID = window.setTimeout(CheckSpeedDelayed, CHECK_INTERVAL);
}
function CheckSpeedDelayed()
{
	window.clearTimeout(timeoutID);
	CheckSpeedStart();
}
function ShowSpeedInfo()
{
	window.open ('SpeedInfo.aspx', '_blank', 'resizable=no, width=530, height=550, left=0, top=0');
}

//----------------------------------------------------------------------
// SelectButton
//----------------------------------------------------------------------

function SwitchSelectButtons(divID, alignElemID)
{
	var div;
	div = document.getElementById(divID);
	if(div.style.visibility == "visible")
		ShowSelectButtons(divID, alignElemID, false);
	else
		ShowSelectButtons(divID, alignElemID, true);
}
function ShowSelectButtons(divID, alignElemID, show)
{
	var div;
	var offLeft = 0;
	var offTop = 0;
	var elem;
	elem = document.getElementById(alignElemID);
	offTop += elem.offsetHeight + 1;
	//offTop += 23;
	while(elem.offsetParent!=null)
	{
		offLeft += elem.offsetLeft;
		offTop += elem.offsetTop;
		elem = elem.offsetParent;
	}
	div = document.getElementById(divID);
	if(show)
	{
		div.style.visibility = "visible";
		div.style.display = "block";
		div.style.left = offLeft;
		div.style.top = offTop;	
		if (div.focus)
			div.focus();
	}
	else
	{
		div.style.visibility = "hidden";
		div.style.display = "none";
	}
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
// Functions to check delivery date in *PositionEdit pages 
//----------------------------------------------------------------------



function CheckDeliveryDate(dateControlID, separator, dayMonthYearIndexes, workDays, askMessage)
{
    var dateControl = getMainForm().elements[dateControlID];
    if (dateControl == null)
        return;
        
    var dateString = dateControl.value;
    
    var d = ParseDate(dateString, separator, dayMonthYearIndexes);    
    
    if (d == "") return;   
    
    var dayOfWeek;
    if (d.getDay()==0) dayOfWeek = "Sunday";
    if (d.getDay()==1) dayOfWeek = "Monday";
    if (d.getDay()==2) dayOfWeek = "Tuesday";
    if (d.getDay()==3) dayOfWeek = "Wednesday";
    if (d.getDay()==4) dayOfWeek = "Thursday";
    if (d.getDay()==5) dayOfWeek = "Friday";
    if (d.getDay()==6) dayOfWeek = "Saturday";
    
    var day;
    for (day in workDays)
    {
        if (workDays[day] == dayOfWeek)
            return;
    }
    
    return confirm(askMessage);
}

function ParseDate(dateString, separator, dayMonthYearIndexes)
{
    var dateArray = dateString.split(separator);

    if(dateArray.length != 3)       
        return "";
    
    var d = new Date();
    d.setTime(0);

    try
    {
        d.setFullYear(dateArray[dayMonthYearIndexes[2]]);
        if(d.getFullYear() != dateArray[dayMonthYearIndexes[2]])
            return "";
        
        d.setMonth(dateArray[dayMonthYearIndexes[1]]-1);
        if(d.getMonth() != dateArray[dayMonthYearIndexes[1]]-1)
            return "";
        
        d.setDate(dateArray[dayMonthYearIndexes[0]]);
        if(d.getDate() != dateArray[dayMonthYearIndexes[0]])
            return "";
    }
    catch(e)
    {
        return "";
    }

    return d;
}

//----------------------------------------------------------------------


//----------------------------------------------------------------------
// Function for auction time left
//----------------------------------------------------------------------

var hoursCount; var minCount; var secCount; var timeLeftID;

var TimeLeftCounter_LastCallDate;

function TimeLeftCounter()
{
    if(timeLeftID==null || timeLeftID=='')
        return;
        
    if(TimeLeftCounter_LastCallDate == null)
    {
        TimeLeftCounterCountDownOnce();
    }
    else
    {
        var totalSecs = TotalSeconds(new Date()) - TotalSeconds(TimeLeftCounter_LastCallDate);
        
        while(totalSecs > 0)
        {
            totalSecs--;
            TimeLeftCounterCountDownOnce();
        }
    }
    
    $('#'+timeLeftID).text(hoursCount+':'+((minCount<10)?'0'+minCount:minCount)+':'+((secCount<10)?'0'+secCount:secCount));
    
    TimeLeftCounter_LastCallDate = new Date();
}
function TotalSeconds(d)
{
    return d.getSeconds() + d.getMinutes()*60 + d.getHours()*60*60 + d.getDay()*60*60*24;
}
function TimeLeftCounterCountDownOnce()
{
    secCount--; 
    if(secCount == -1)
    {
        secCount = 59; 
        minCount--;
        if(minCount == -1)
        {
            minCount = 0;
            hoursCount--;
        }
    }
    if(hoursCount < 0 || minCount  < 0 || secCount < 0)
    {
        hoursCount = 0; 
        minCount = 0;
        secCount = 0;
    }
    if (hoursCount <= 0 && minCount <= 0 && secCount <= 0)
    {    
        secCount = 1;
    }    
}

var AddJavaScript_Last_ForRemove=null;

function AddJavaScript(src)
{
    if(AddJavaScript_Last_ForRemove!=null)
    {
        document.getElementsByTagName("head")[0].removeChild(AddJavaScript_Last_ForRemove);
    }
    
    var headID = document.getElementsByTagName("head")[0];         
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = src;
    headID.appendChild(newScript);
    
    AddJavaScript_Last_ForRemove = newScript;
}

//----------------------------------------------------------------------