var SelectedImage = 0
var xmlHttp
var PromotePage = 1
var NoOfPromotedPages = 1
var PromotionTimer = null;

function LoadImage(imgId, URL, maxwidth, maxheight)
{
	document.getElementById(imgId).onload = null;
	var img = new Image();
	img.onload = function()
	{
		document.getElementById(imgId).style.height = '';
		document.getElementById(imgId).style.width = '';
		if (maxheight==0)
		{
			if (img.width>maxwidth)
			{
				document.getElementById(imgId).style.width = maxwidth;
			}else
			{
				document.getElementById(imgId).style.width = img.width;
			}
		}else if (maxwidth==0)
		{
			if (img.height>maxheight)
			{
				document.getElementById(imgId).style.height = maxheight;
			}else
			{
				document.getElementById(imgId).style.height = img.height;
			}			
		}else
		{
			if ((maxwidth / maxheight)>(img.width / img.height))
			{
				if (img.height>maxheight)
				{
					document.getElementById(imgId).style.height = maxheight;
				}else
				{
					document.getElementById(imgId).style.height = img.height;
				}	
			}else
			{
				if (img.width>maxwidth)
				{
					document.getElementById(imgId).style.width = maxwidth;
				}else
				{
					document.getElementById(imgId).style.width = img.width;
				}				
			}
		}
		//alert(URL);
		document.getElementById(imgId).src = img.src;
		img.onload = null;
	}
	img.src = URL;
}

function SetVisibility(divId, visibility)
{
	if (visibility)
	{
		Show(divId);
		document.getElementById(divId).style.visibility= 'visible';
	}else
	{
		document.getElementById(divId).style.visibility= 'hidden';
	}
}

function ToggleVisibility(divId)
{
	if (document.getElementById(divId).style.visibility=='visible')
		SetVisibility(divId,false);
	else
		SetVisibility(divId,true);
}

function MoveLayerToCursor(divId, event)
{
	//document.getElementById(divId).style.left = event.
}

function Show(divId)
{
	//document.getElementById(divId).style.display = 'block';
	$("#"+divId).slideDown("slow")
}

function Hide(divId)
{
	//document.getElementById(divId).style.display = 'none';
	$("#"+divId).slideUp("slow");
}

function JustShow(divId)
{
	document.getElementById(divId).style.display = 'block';
}

function JustHide(divId)
{
	document.getElementById(divId).style.display = 'none';
}

function Toggle(divId)
{
	if (document.getElementById(divId).style.display == 'none')
	{
		Show(divId);
	}else
	{
		Hide(divId);
	}
}

function Preview(URL, ImageID)
{
	LoadImage("PreviewDisplay",URL,400,300);
	SelectedImage = ImageID;
}

function ChangeBGColor(divId, Color)
{
	document.getElementById(divId).style.backgroundColor = Color;
}

function ChangeValue(divId, text)
{
	document.getElementById(divId).value = text;
	document.getElementById(divId).style.backgroundColor = '#FFFF99';
}

function IsNumber(NumText)
{
	return (!isNaN(NumText) && NumText.length!=0);
}

function isEmail(formInput) {

    if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        return(false);
    }

    var message;

    if (stringEmpty(formInput.value)) {
        message = "Error! There is no input value entered.";
        return false;
    } else if (noAtSign( formInput.value )) {
        message = "Error! The address \"" + formInput.value + "\" does not contain an '@' character.";
        return false;
    } else if (nothingBeforeAt(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain at least one character before the '@' character";
        return false;
    } else if (noLeftBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
        return false;
    } else if (noRightBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
        return false;
    } else if (noValidPeriod(formInput.value)) {
        message = "Error! The address \"" + formInput.value + "\" must contain a period ('.') character.";
        return false;
    } else if (noValidSuffix(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain a two, three or four character suffix.";
        return false;
    } else {
        message = "Success! The email address \"" + formInput.value + "\" validates OK.";
        return true;
    }
}

function checkValid (formField) {
    if ( checkValidation ( formField ) == true ) {
        alert ( 'E-Mail Address Validates OK' );
    }

    return ( false );
}

function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}	

function checkFirmForm()
{
	if (document.FirmForm.teImeFirme.value == '')
	{
		alert('Morate uneti ime firme.');
		return false;
	}						
	if (document.FirmForm.teAdresa.value == '')
	{
		alert('Morate uneti adresu firme.');
		return false;
	}	
	if (document.FirmForm.teMesto.value == '')
	{
		alert('Morate uneti mesto (grad, varosica) u kojem je sediste vase firme.');
		return false;
	}			
	if (document.FirmForm.teTelefon.value == '')
	{
		alert('Morate uneti telefon firme.');
		return false;
	}					
}	

function checkUserForm()
{
	if (!isEmail(document.UserForm.teEmail))
	{
		alert('Morate uneti ispravnu e-mail adresu.');
		return false;
	}							
	if (document.UserForm.teSifra.value != document.UserForm.teSifra2.value)
	{
		alert('Sifre koje ste uneli se ne poklapaju.');
		return false;
	}	
	if (document.UserForm.teIme.value == '')
	{
		alert('Morate uneti vase ime.');
		return false;
	}			
	if (document.UserForm.tePrezime.value == '')
	{
		alert('Morate uneti vase prezime.');
		return false;
	}
	if (document.UserForm.teTelefon.value=='' && document.UserForm.teMobilni.value=='')
	{
		alert('Morate uneti bar jedan broj telefona.');
		return false;
	}			
	if (document.UserForm.teAdresa.value == '')
	{
		alert('Morate uneti adresu.');
		return false;
	}
	if (document.UserForm.teGrad.value == '')
	{
		alert('Morate uneti iz kog ste mesta.');
		return false;
	}								
}		

function checkNekretnineForm()
{
	if (document.NekretnineForm.teNaziv.value == '')
	{
		alert('Morate uneti naziv oglasa.');
		return false;
	}	
	if (!IsNumber(document.NekretnineForm.tePovrsina.value))
	{
		alert('Neodzvoljena vrednost za povrsinu nekretnine.');
		return false;
	}	
	if (document.NekretnineForm.teNaziv.value == '')
	{
		alert('Morate uneti cenu nekretnine.');
		return false;
	}	
}

function checkCarForm()
{
	if (document.CarForm.teModel.value == '')
	{
		alert('Morate uneti model vozila.');
		return false;
	}
	if (!IsNumber(document.CarForm.teZapremina.value))
	{
		alert('Neodzvoljena vrednost za zapreminu motora.');
		return false;
	}
	if (!IsNumber(document.CarForm.teGodiste.value))
	{
		alert('Neodzvoljena vrednost za godinu proizvodnje.');
		return false;
	}	
	if (!IsNumber(document.CarForm.teCena.value))
	{
		alert('Nedozvoljena vrednost za cenu.');
		return false;
	}		
	if (!IsNumber(document.CarForm.teVrata.value))
	{
		alert('Nedozvoljena vrednost za broj vrata.');
		return false;
	}
	if (!IsNumber(document.CarForm.teKilometraza.value))
	{
		alert('Nedozvoljena vrednost za predjenu kilometrazu.');
		return false;
	}							
	if (document.CarForm.teBoja.value == '')
	{
		alert('Morate uneti boju vozila.');
		return false;
	}				
}
	
function ChangeURL(URL)
{
	window.location.assign(URL);	
}

function ConfirmRequest(message, url)
{
	if(confirm(message)) 
		location.href = url;
}

function SetNoOfPromotedPages(x)
{
	NoOfPromotedPages = x;
}

function PromotionTimerTick()
{
	LoadPromoted();
}

//Automatsko menjanje promotivnih automobila
function NextPromotionPage()
{
	$.get("promocija.asp", { pageNo: PromotePage},
		function(data){
			ChangePromotion(data);
		});
  	PromotePage++;
	if (PromotePage>NoOfPromotedPages)
		PromotePage = 1;		
}

function LoadPromoted()
{
	if (NoOfPromotedPages>1)
	{
		$("#Promocija").slideUp("slow",function(){NextPromotionPage()});
	}else if(NoOfPromotedPages<1)
	{
		$('#OkvirZaPromocije').slideUp("slow");
	}
}

function ChangePromotion(data)
{
	document.getElementById('Promocija').innerHTML = data;
	$("#Promocija").slideDown("slow",function(){SetPromotionTimer()});
}

function SetPromotionTimer()
{
	window.clearTimeout(PromotionTimer);
	PromotionTimer = window.setTimeout('PromotionTimerTick()',5000)
}

function ClearPromotionTimer()
{
	window.clearTimeout(PromotionTimer);	
}
//Kraj automatskog menjanja promo automobila
//Opstine
function UcitajOpstine(CountryID, FN)
{
	$.get("select.asp", { Country: CountryID, FieldName: FN},
		function(data){
			PrikaziOpstine(data);
		});	
}

function UcitajFormZaOpstine(a, id, fn)
{
	$.get("FormZaMesto.asp", {action: a, mesto: id, FieldName: fn},
		function(data){
			PrikaziFormZaOpstine(data);
		});	
}

function PrikaziFormZaOpstine(data)
{
	document.getElementById('MestoDiv').innerHTML = data;
}

function PrikaziOpstine(data)
{
	document.getElementById('Opstine').innerHTML = data;
}
//Kraj opstina
//Select All
function SelectAll(divId)
{
	var i;
	i = 1;
	while (document.getElementById(divId+i)!=null)
	{
		document.getElementById(divId+i).checked = true;
		i++;
	}
}

function DeSelectAll(divId)
{
	var i = 1;
	while (document.getElementById(divId+i)!=null)
	{
		document.getElementById(divId+i).checked = false;
		i++;
	}
}
//End Select All