//      general.js
//      
//      Copyright 2009 aleXoid <webmaster@resetters.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
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;
}
var ns4=document.layers;
var ie4=document.all;
var ns6=document.getElementById&&!document.all;
var dragswitch=0;
var nsx;
var nsy;
var nstemp;
	function drag_dropns(name)
		{
		if (!ns4) return;
		temp=eval(name);
		temp.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
		temp.onmousedown=gons;
		temp.onmousemove=dragns;
		temp.onmouseup=stopns;
		}
	function gons(e)
		{
		temp.captureEvents(Event.MOUSEMOVE);
		nsx=e.x;
		nsy=e.y;
		}
	function dragns(e)
		{
		if (dragswitch==1)
			{
			temp.moveBy(e.x-nsx,e.y-nsy);
			return false;
			}
		}
	function stopns()
		{
		temp.releaseEvents(Event.MOUSEMOVE)
		}
	function drag_drop(e)
		{
		if (ie4&&dragapproved)
			{
			crossobj.style.left=tempx+event.clientX-offsetx;
			crossobj.style.top=tempy+event.clientY-offsety;
			return false
			}
		else if (ns6&&dragapproved)
			{
			;crossobj.style.left=tempx+e.clientX-offsetx+"px";
			crossobj.style.top=tempy+e.clientY-offsety+"px";
			return false
			}
		}
function initializedrag(e)
	{
	crossobj=ns6? document.getElementById("popup_window_top") : eval("document.all.popup_window_top");
	var firedobj=ns6? e.target : event.srcElement;
	var topelement=ns6? "html" : document.compatMode && document.compatMode!="BackCompat"? "documentElement" : "body";
	while (firedobj.tagName!=topelement.toUpperCase() && firedobj.id!="popup_window")
		{
		firedobj=ns6? firedobj.parentNode : firedobj.parentElement
		}
	if (firedobj.id=="popup_window")
		{
		offsetx=ie4? event.clientX : e.clientX;offsety=ie4? event.clientY : e.clientY;
		tempx=parseInt(crossobj.style.left);
		tempy=parseInt(crossobj.style.top);
		dragapproved=true;
		document.onmousemove=drag_drop
		}
	}
	;document.onmouseup=new Function("dragapproved=false")
function hidebox()
	{
	crossobj=ns6? document.getElementById("popup_window_top") : eval("document.all.popup_window_top");
	if (ie4||ns6)crossobj.style.visibility="hidden";
	else if (ns4)eval("document.popup_window_top.visibility=\'hide\'")
	}
function viewbox()
	{
	popup_window_top.style.top=document.body.scrollTop+50;
	crossobj=ns6? document.getElementById("popup_window_top") : eval("document.all.popup_window_top"); 
	if (ie4||ns6)crossobj.style.visibility="";
	else if (ns4)eval("document.popup_window_top.visibility=''")
	}
function TopWindowMap(map,alt)
	{ 
	document['Big_Photo'].src='images/map/'+map;
	document['Big_Photo'].alt=alt;
	document['Big_Photo'].title=alt;
	document.getElementById("popup_window_title").innerHTML=alt;
    }
function AjaxLoad(url, tag, ld, data) 
	{
	var xmlHttp = false;
    if(!ld)
		{
		ld="<img src='images/design/AjaxLoad.gif' width=8 height=8 border=0>";
		}
    try 
		{
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
	catch (e)
		{
        try 
			{
			 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
		catch (e2)
			{
			xmlHttp = false;
			}
		}
     if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
		{
		xmlHttp = new XMLHttpRequest();
		}

     var include=document.getElementById (tag);
     include.innerHTML = ld;
     xmlHttp.onreadystatechange = function() {AjaxDone(tag,xmlHttp);};
     xmlHttp.open("POST", url, true);
     xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=windows-1251");
     xmlHttp.send(escape(data));
     }
function AjaxDone(tag,xmlHttp){if (xmlHttp.readyState == 4) 
	{
    if (xmlHttp.status == 200) 
		{
		var include=document.getElementById (tag);
        include.innerHTML = xmlHttp.responseText;
        }
     }
    }
if (document.images)
 {
 SearchOn=new Image();SearchOn.src="images/design/SearchOn.gif";
 SearchOff=new Image();SearchOff.src="images/design/SearchOff.gif";
 PlusOn  =new Image();PlusOn.src  ="images/design/plusOn.gif";
 PlusOff =new Image();PlusOff.src ="images/design/plusOff.gif";
 MinusOn =new Image();MinusOn.src ="images/design/minusOn.gif";
 MinusOff=new Image();MinusOff.src="images/design/minusOff.gif";
 }
	function CatOn(id)
		{
		CI=document.getElementById('catim_'+id);
        if(CI.src==PlusOff.src)
			{
			CI.src=PlusOn.src;
			}
		else 
			{
			CI.src=MinusOn.src;
			}
		}
	function CatOff (id)
		{
		CI=document.getElementById('catim_'+id);
        if(CI.src==MinusOn.src)
			{
			CI.src=MinusOff.src;
			} 
		else 
			{
			CI.src=PlusOff.src;
			}
		}
	function OpenCat(id,inc)
		{
		var Serv;
		El=document.getElementById("Cat_"+id);
		if (El.style.display=='none')
			{
			El.style.display='block';
			}
		else 
			{
			El.style.display='none';                                                          
			}
		}

	function openHorWin() 
		{
		var features, w = 355, h = 294;
		var top = (screen.height - h)/2, left = (screen.width - w)/2-15;
		if(top < 0) top = 0;
		if(left < 0) left = 0;
		features = 'top=' + top + ',left=' +left;
		features += ',height=' + h + ',width=' + w + ',resizable=no, scrollbars=no';
		myWin = open('cam/index.html', 'displayWindow', features);
	}
	function Printer(prin_it)
		{
			pr = document.getElementById(prin_it).innerHTML;  
			newWin=window.open('','printWindow','Toolbar=0,Location=0,Directories=0,Status=0,Menubar=0,Scrollbars=0,Resizable=0'); 
			newWin.document.open(); 
			newWin.document.write(pr); 
			newWin.print();
			newWin.document.close();
		}

