// support.js
// Last changes 2006.11.05.


//-------------------------------------------------------------
tabRollover = function(){
		var x = document.getElementById("sub-nav");
		for( var i=0; x.childNodes[i]; i++){
			if( x.childNodes[i].tagName == "LI"){
				if( x.childNodes[i].firstChild.tagName == "A" ){
					x.childNodes[i].firstChild.onmouseover = function(){
						this.parentNode.className="TabselectedLI";
					}
					x.childNodes[i].firstChild.onmouseout = function(){
						this.parentNode.className="sub-nav";
					}
				}
				if( x.childNodes[i].firstChild.tagName == "SPAN" ){
						 x.childNodes[i].className="TabselectedLInot";
				}
			}
		}
	}
//-------------------------------------------------------------
AddToSearch = function(){
	if( document ){
		var elem = document.getElementById('results');
		for(var i=0; elem.childNodes[i]; i++){
			elem.childNodes[i].onmouseover=function(){
				this.firstChild.style.backgroundColor = '#E9F3FD';
				//this.firstChild.style.backgroundColor = '#f5f5f5';
				this.style.borderColor = '#79B8E8';
				//this.style.borderColor = 'silver';
				this.style.cursor = 'hand';
				this.childNodes[2].style.borderColor = '#E9F3FD';
			}
			elem.childNodes[i].onmouseout=function(){
				this.firstChild.style.backgroundColor = 'white';
				this.style.borderColor = '#f5f5f5';
				this.childNodes[2].style.borderColor = '#eeeeee';
			}
			elem.childNodes[i].onclick=function(){
				document.location = "http://www.companies.lv/"+this.childNodes[1].value+"/"+getCookie("lang");
			}
		}
	}
}
//-------------------------------------------------------------
// utility function called by getCookie( )
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}
//-------------------------------------------------------------
function getCookie(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return "";
}
//-------------------------------------------------------------
function createMail(elem_id){
	if( document ){
		var x  =  document.getElementById(elem_id);
		var searchStr = "{#]";
		var replaceStr = "@";
		var re = new RegExp(searchStr , "g");
		x.innerHTML = x.innerHTML.replace(re, replaceStr);
	}
}
//-------------------------------------------------------------
var tmpNosElem;
var tmpFotoElem;
var tmpTxtElem;

var getNos = true;
var getFoto = true;
var getTxt = true;
var SavedAllForm = false;

var AllForm = new String();
var ImgSrc = new String();
var OldIzvelies = new String();

//-------------------------------------------------------------
function ResetForm(){
/*
try{
	var x = document.getElementById("InsertForm");
	x.innerHTML = AllForm;
}
catch(e){
}
if(e){*/
	document.location.href = '';
//}
}
//-------------------------------------------------------------
function SaveAllForm(){
	if( SavedAllForm == false){
		SavedAllForm = true;
		var x = document.getElementById("InsertForm");
		AllForm = x.innerHTML;
	}
}
//-------------------------------------------------------------
function ShowNice(){
	if(getTxt && !getFoto){
		var x = document.getElementById("txtElem");
		x.style.width = 588;
		var y = document.getElementById("txttxt");
		y.style.width = 572;
		y.style.height = 230;
		var z = document.getElementById("elemSwitch");
		z.visibility = "hidden";
		z.innerHTML = '';
		var y = document.getElementById("resetButton");
		//y.disabled = false;
		//y.onclick = ResetForm();
	}
	if(!getTxt && getFoto){
		var x = document.getElementById("fotoElem");
		x.style.width = 588;
		var z = document.getElementById("elemSwitch");
		z.visibility = "hidden";
		z.innerHTML = '';
		var y = document.getElementById("resetButton");
		//y.disabled = false;
		//y.onclick = ResetForm();
	}
	if( getNos == false){
		var y = document.getElementById("resetButton");
		//y.disabled = false;
	}
}
//-------------------------------------------------------------
function DoSwitch(){

	if( OldIzvelies.length == 0 ){
		var x = document.getElementById("bbut");
		OldIzvelies = x.innerHTML;
	}


	var txt = document.getElementById("toptxtElem");
	var foto = document.getElementById("topfotoElem");
	tmpX = txt.innerHTML;
	txt.innerHTML = foto.innerHTML;
	foto.innerHTML = tmpX;


	var fotopos = document.manaforma.fotopos.value;

	if( fotopos == 'l' ){
		document.manaforma.fotopos.value = 'r';
	}
	if( fotopos == 'r' ){
		document.manaforma.fotopos.value = 'l';
	}

	var x = document.getElementById("bbut");
	x.innerHTML = OldIzvelies;


}
//-------------------------------------------------------------

function HideNos(){
	SaveAllForm();
	var nos = document.getElementById('catNos');
	tmpNosElem = nos.innerHTML;
	nos.innerHTML = '';
	getNos = false;
	ShowNice();
}
//-------------------------------------------------------------
function HideFoto(){
	SaveAllForm();
	var nos = document.getElementById('fotoElem');
	tmpFotoElem = nos.innerHTML;
	nos.innerHTML = '';
	getFoto = false;
	ShowNice();
}
//-------------------------------------------------------------
function HideTxt(){
	SaveAllForm();
	var nos = document.getElementById('txtElem');
	tmpTxtElem = nos.innerHTML;
	nos.innerHTML = '';
	getTxt = false;
	ShowNice();
}
//-------------------------------------------------------------
function showBrowse(){
	var x = document.getElementById("bbut");
	OldIzvelies = x.innerHTML;
	x.innerHTML = "<div id=\"bilde\"></div><input onchange=\"showpic(this);\" style=\"width:-20px !important; width:180px;margin:auto auto;\" type=\"file\" name=\"userfile\" id=\"browsebutton\" />";
	var y = document.getElementById("browsebutton");
	/*if( y ){
		y.click();
	}*/
}
//-------------------------------------------------------------
function showpic(elem){
if( elem.value.length == 0) return;
ImgSrc = elem.value;

var _needWidth = 150;
var _needHeight;

	var img = new Image();
	img.src = elem.value;
	var k = (img.width / _needWidth);
	_needHeight = Math.round(img.height / k);
	var x = document.getElementById("bilde");
	if( _needHeight > 5 ){
		x.innerHTML =	"<img src=\""+img.src+"\" width=\""+_needWidth+"\" height=\""+_needHeight+"\" />";
	}
	else x.innerHTML = '';
}
//-------------------------------------------------------------
function Save(){
	return true;
}


//-------------------------------------------------------------
var req; //AJAX handler
var loadNoz = "";
var nozares = new Array();
var ShowDelete = false;
var AkcijaNozareSet = false;


function getsubnoz(noz_id,lang){
if( window.XMLHttpRequest){
	req = new XMLHttpRequest();
	req.onreadystatechange = OnChange;
	req.open("GET","http://www.companies.lv/nozares.php?&x="+Math.random(4,2)+"&noz_id="+noz_id+"&lang="+lang+"&s_delete="+ShowDelete, true);
	req.send(null);
 }
 else if (window.ActiveXObject) {
  req = new ActiveXObject("Microsoft.XMLHTTP");
  if (req){
		req.onreadystatechange = OnChange;
		req.open("GET","http://www.companies.lv/nozares.php?&x="+Math.random(4,2)+"&noz_id="+noz_id+"&lang="+lang+"&s_delete="+ShowDelete, true);
		req.send();
	}
 }
 window.focus();
}
//---------------------------------------------------------
function getsubnoz2(noz_id,lang){
AkcijaNozareSet = false;
if( window.XMLHttpRequest){
	req = new XMLHttpRequest();
	req.onreadystatechange = OnChange;
	req.open("GET","http://www.companies.lv/nozares_ak.php?&x="+Math.random(4,2)+"&noz_id="+noz_id+"&lang="+lang+"&s_delete="+ShowDelete, true);
	req.send(null);
 }
 else if (window.ActiveXObject) {
  req = new ActiveXObject("Microsoft.XMLHTTP");
  if (req){
		req.onreadystatechange = OnChange;
		req.open("GET","http://www.companies.lv/nozares_ak.php?&x="+Math.random(4,2)+"&noz_id="+noz_id+"&lang="+lang+"&s_delete="+ShowDelete, true);
		req.send();
	}
 }
 window.focus();
}
//---------------------------------------------------------
function OnChange(){
if( (typeof(loadNoz)=="undefined") || (loadNoz=="")  ) loadNoz = "loading...";
var x = document.getElementById("noz");
x.innerHTML = loadNoz;
if (req.readyState == 4) {
  if (req.status == 200) {
	  x.innerHTML = req.responseText;
		//alert(req.responseText);
		req = null;
		return;
  } else {
   alert("AJAX kļūda:\n" +
    req.statusText);
	req.abort();
	req = null;
  }
 }
}
//---------------------------------------------------------
function saveNoz(noz,lang){
var str = new String();
	for( var i = 0; i<noz.length; i++){
		str = str + "&n"+i+"="+noz[i];
	}

if( window.XMLHttpRequest){
	req = new XMLHttpRequest();
	req.onreadystatechange = OnChangeSave;
	req.open("GET","http://www.companies.lv/nozares.php?&x="+Math.random(4,2)+"&lang="+lang+str, true);
	req.send(null);
 }
 else if (window.ActiveXObject) {
  req = new ActiveXObject("Microsoft.XMLHTTP");
  if (req){
		req.onreadystatechange = OnChangeSave;
		req.open("GET","http://www.companies.lv/nozares.php?&x="+Math.random(4,2)+"&lang="+lang+str, true);
		req.send();
	}
 }
window.focus();
}
//---------------------------------------------------------
function saveNoz2(noz,lang){
var str = new String();
	for( var i = 0; i<noz.length; i++){
		str = str+noz[i]+",";
	}
	if( str.length > 0){
		AkcijaNozareSet = true;
		if( typeof(document.manaforma.nozares) != "undefined" ){
			document.manaforma.nozares.value = str;
			var y = document.getElementById("addBut");
			y.innerHTML = " - nozare OK, aizpildi nākamos lauciņus!";
		}
	}
	//alert(document.manaforma.nozares.value);
}
//---------------------------------------------------------
function OnChangeSave(){
if( (typeof(loadNoz)=="undefined") || (loadNoz=="")  ) loadNoz = "loading...";
var x = document.getElementById("noz");
x.innerHTML = loadNoz;
if (req.readyState == 4) {
  if (req.status == 200) {
	  x.innerHTML = req.responseText;
		//alert(req.responseText);
		req = null;
		return;
  } else {
   alert("AJAX kļūda:\n" +
    req.statusText);
	req.abort();
	req = null;
  }
 }
}
//---------------------------------------------------------
CheckCount = function(){
	var x = document.getElementById("txttxt");
	var y = document.getElementById("countme");
	if( (typeof(x)!="undefined") && (typeof(y)!="undefined") ){
		y.innerHTML = (300 - x.value.length);
	}
}
//---------------------------------------------------------
IsOkAkcija = function(){

	if(AkcijaNozareSet == false){
		alert("Nav norādīta īpašā piedāvājuma nozare!\n Lai pievienotu nozari, spied uz \n \"Izvēlies produkta vai pakalpojuma nozari\"\n formas augšā!");
		return false;
	}
	if( document.manaforma.catNos.value.length < 3){
		alert("Nav norādīts īpašā piedāvājuma nosaukums!");
		document.manaforma.catNos.focus();
		return false;
	}
	if( document.manaforma.txtElem.value.length < 3){
		alert("Nav norādīts īpašā piedāvājuma apraksts!");
		document.manaforma.txtElem.focus();
		return false;
	}

	if(document.manaforma.atlaide[0].checked){ //Atlaide
		if( document.manaforma.procenti.value.length < 1){
			alert("Nav norādīta Atlaide!\n (Jānorāda procentos)");
			document.manaforma.proc_val.focus();
			return false;
		}
	}
	if(document.manaforma.atlaide[0].checked){
		if( document.manaforma.old_price.value.length < 1){
			alert("Nav norādīta iepriekšējā cena!\n (Jānorāda latos, Ls)");
			document.manaforma.old_price.focus();
			return false;
		}
		if( document.manaforma.new_price.value.length < 1){
			alert("Nav norādīta jaunā cena!\n (Jānorāda latos, Ls)");
			document.manaforma.new_price.focus();
			return false;
		}
	}
}
//---------------------------------------------------------
function AkcijaKind(){

	if(document.manaforma.atlaide[0].checked){
		document.manaforma.old_price.disabled = true;
		document.manaforma.old_price.style.backgroundColor = "#eeeeee";
		document.manaforma.new_price.disabled = true;
		document.manaforma.new_price.style.backgroundColor = "#eeeeee";

		document.manaforma.proc_val.disabled = false;
		document.manaforma.proc_val.style.backgroundColor = "white";
	}

	if(document.manaforma.atlaide[1].checked){
		document.manaforma.old_price.disabled = false;
		document.manaforma.old_price.style.backgroundColor = "white";
		document.manaforma.new_price.disabled = false;
		document.manaforma.new_price.style.backgroundColor = "white";

		document.manaforma.proc_val.disabled = true;
		document.manaforma.proc_val.style.backgroundColor = "#eeeeee";
	}
}