/*
  $Id: general.js 1739 2007-12-20 00:52:16Z hpdl $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
var selecturi=new Array();
selecturi[0]="model";
selecturi[1]="fabricatie";
selecturi[2]="grupa";
selecturi[3]="categoria";
function populate_boxes(){
	var url = 'selectboxes.php?marca='+$('marca').value;
	for(i=0;i<4;i++)
	{
		notice = $(selecturi[i]);
		if(notice.disabled!='disabled')
		{
			url+='&'+selecturi[i]+'='+notice.value
		}
	}
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			aa=transport.responseText.split('|')
			for(i=0;i<4;i++)
			{
				notice = $(selecturi[i]);
				if(aa[i]=='')
				{
					notice.disable();
					notice.update('');
				}
				else
				{
					notice.enable();
					notice.update(aa[i]);
				}
			}
		}
	});
}

function creeazaObjReq(){
    var cerere;
    if(window.XMLHttpRequest){
        cerere=new XMLHttpRequest();
    }else if(window.ActiveXObject){
        cerere=new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        alert("Ihr Browser kann AJAX nicht unterstützen!");
    }
    return cerere;
}

function SendForm(formid,phpscript,div){
    var http=creeazaObjReq();
    if(phpscript=='') phpscript="ajax.php"
    //alert(phpscript);
    form=document.getElementById(formid);
    hd="";
    for(i=0;i<form.elements.length;i++){

         if(i!=0)
            hd= hd + "&"
        if(form.elements[i].type=="checkbox"){
            if(form.elements[i].checked){

            hd =hd + form.elements[i].name + "=" +form.elements[i].value;
            }
        }
        else{
            if(form.elements[i].type=="textarea"){
                //alert("@@@");
                try{
                    str=FCKeditorAPI.GetInstance(form.elements[i].id).GetXHTML();
                    result = str.replace(/&/gi, "##");
                    hd =hd + form.elements[i].name + "=" + result;

                }
                catch(err){
                    //alert(err);
                    hd =hd + form.elements[i].name + "=" +form.elements[i].value;
                }
            }
            else{

                hd =hd + form.elements[i].name + "=" +form.elements[i].value;
            }
        }
    }
    //alert(hd);
    http.open('post',phpscript);
    http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    http.onreadystatechange = function (){
            if(http.readyState==1){
                if(document.getElementById(div)!=null)
                    void(0);

            }else if(http.readyState==4 && http.status == 200){
                var raspuns=http.responseText;
                if(raspuns){
                        document.getElementById(div).innerHTML=raspuns;

                }
            }
        }
    http.send(hd);
    return false;
}


function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];

  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();

  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }

  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}


function setMenu(){
	$('support').observe('mouseover',function(){$('submeniu').show()});
	$('submeniu').observe('mouseover',function(){$('submeniu').show()});
	$('support').observe('mouseout',function(){$('submeniu').hide()});
	$('submeniu').observe('mouseout',function(){$('submeniu').hide()});
	$('submeniu').hide();
	resetInput();
}


function resetInput(){
	$$('.resetInput').findAll(
		function(elemx)
		{
			var resetValue = elemx.value;
			elemx.observe('focus' , function()
			{
				if(resetValue==elemx.value)
				{
					elemx.value='';
				}
			});
			elemx.observe('blur' , function()
			{
				if(elemx.value=='')
				{
					elemx.value=resetValue;
				}
			});
		}
	);
}

function infoPage(url){
	new Ajax.Request(url,
	{
		method: 'get',
		onSuccess: function(transport) {
			var newH = getDocHeight()+'px';
			$('infoPageBG').style.height=newH;

			//alert(newH); 	Element.writeAttribute($('infoPageBG'), 'height', newH);
			$('infoPageBG').setOpacity(0.4);
			$('infoPageBG').style.display="block";

			$('infoPage').update(transport.responseText);

			$('infoPage').up().style.display="block";
			$('infoPage').style.display="block";

			$$('#infoPage a[href]').findAll(function(elem){
				var draci = Element.readAttribute(elem, "href");
				if(draci.split('http://').join('')==draci)
				{
					aa = draci.split('#');
					if (aa.length>1) {
						draci = curentLink + '#' + aa[1];
						elem.href = "" + draci + "";
					}
					else
					{
						elem.href = "javascript:infoPage('" + draci + "')";
					}

				}
				else
				{
					Element.writeAttribute(elem, 'target', '_blank');
					Element.writeAttribute(elem, 'onclick', 'window.focus()');
				}
			});
		}
	});
}

function infoPageClose(){
	$('infoPageBG').style.display="none";
	$('infoPage').update('');
	$('infoPage').up().style.display="none";
	$('infoPage').style.display="none";

}

function catsUl()
{
	$$('.lititle').findAll(function(elemx){

			if(elemx.next())
				elemx.next().hide();
/*
			elemx.observe('click',function(e){
				if(elemx.next())
				{
					ulsHide();
					elemx.next().show();
					e.stop();
				}
			});
*/

		})
	$$('.openCat').findAll(function(elemx){ elemx.show(); })
}

function ulsHide(){
	$$('.lititle').findAll(function(elemx){
		if(elemx.next())
			elemx.next().hide();
	});
}

function validateAttribut(){
	var ret = true;

	if($('attribut'))
	{
		if($('attribut').selectedIndex == 0 )
		{
			 ret = false;
			 $('mesajAttribut').innerHTML = 'Selectaţi una din opţiunile selectorului de mai sus';
		}
	}
	return  ret;
}

function getDocHeight() {
    var aa = getPageSize();
    return aa[1];
}

function getPageSize() {
 var xScroll, yScroll;
 if (window.innerHeight && window.scrollMaxY) {
  xScroll = window.innerWidth + window.scrollMaxX;
  yScroll = window.innerHeight + window.scrollMaxY;
 }
 else if (document.body.scrollHeight > document.body.offsetHeight){
  // all but Explorer Mac
  xScroll = document.body.scrollWidth;
  yScroll = document.body.scrollHeight;
 }
 else{
  // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  xScroll = document.body.offsetWidth;
  yScroll = document.body.offsetHeight;
 }
 var windowWidth, windowHeight;
 if (self.innerHeight) {
  // all except Explorer
  if(document.documentElement.clientWidth){
   windowWidth = document.documentElement.clientWidth;
  }
  else {
   windowWidth = self.innerWidth;
  }
  windowHeight = self.innerHeight;
 }
 else if (document.documentElement && document.documentElement.clientHeight) {
  // Explorer 6 Strict Mode
  windowWidth = document.documentElement.clientWidth;
  windowHeight = document.documentElement.clientHeight;
 }
 else if (document.body) {
  // other Explorers
  windowWidth = document.body.clientWidth;
  windowHeight = document.body.clientHeight;
 }
 // for small pages with total height less then height of the viewport
 if(yScroll < windowHeight){
  pageHeight = windowHeight;
 }
 else {
  pageHeight = yScroll;
 }
 // for small pages with total width less then width of the viewport
 if(xScroll < windowWidth){
  pageWidth = xScroll;
 }
 else {
  pageWidth = windowWidth;
 }
  return [pageWidth,pageHeight];
}

function redirect(url){
	window.location= url;
}
