// Check Date
function checkValidDate(fieldName) {
   var errorCode = 0;
   dateField = fieldName.value;

   if (dateField.length != 10)
      errorCode = 1;

   day      = dateField.substring(0, 2);
   point1   = dateField.substring(2, 3);
   month    = dateField.substring(3, 5);
   point2   = dateField.substring(5, 6);
   year     = dateField.substring(6, 10);

   if (isNaN(day))
      errorCode = 1;
   if (isNaN(month))
      errorCode = 1;
   if (isNaN(year))
      errorCode = 1;
   if ((day < 1) || (day > 31))
      errorCode = 2;
   if (point1 != '.')
      errorCode = 3;
   if ((month < 1) || (month > 12))
      errorCode = 4;
   if (point2 != '.')
      errorCode = 5;
   if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
      if (day == 31)
         errorCode = 7
   }

   if (month == 2) {
      var g = parseInt(year/4);
      if (isNaN(g))
         errorCode=8;
      if (day > 29)
         errorCode = 9;
      if ((day == 29) && ((year/4) != parseInt(year/4)))
         errorCode=10;
   }

   if (errorCode > 0) {
      return false;
   } else {
      return true;
   }
}


function GoToPage(hiddenName, hiddenValue, bookMark) {
   hiddenName.value = hiddenValue;
   if(bookMark.length > 0)
      document.itemform.action = document.itemform.action+"#"+bookMark;
   document.itemform.submit();
   return true;
}

function ShowWindow(winHTML,winName,w,h,l,t) {

	winWidth=w || 780
	winHeight=h || 560
	winLeft=0//l || window.screenLeft+10
	winTop=0//t || window.screenTop+10
	params="menubar=no; toolbar=no; resizable=1; scrollbars=1; width="+winWidth+"px; height="+winHeight+"px; left="+winLeft+"; top="+winTop
	ThisWindow=window.open(winHTML,winName,params) 
	ThisWindow.self.focus()
	return false;
}

/*
function checkedavail(checkObjects="",alertStr="") {
		if (!alertStr) alertStr="Потвърдете изтриването!"
		if (!checkObjects) frm=document.forms(0).elements
		else eval('frm=document.forms(0).'+checkObjects)
		ret=0
		if (frm.type=="checkbox") ret=frm.checked
		else for (i=0;(i<frm.length && !ret);i++) if (frm[i].type=="checkbox") ret=frm[i].checked
		if (ret) { 
			if (confirm(alertStr)) {
				//if (document.forms(0).delFlag) document.forms(0).delFlag.value=1
				return true
			}
		} else alert("Маркирайте нещо все пак!")
		return false
}
*/

function sel_authors(letter,action,field,item,table) {
	ShowWindow('sel_authors.php?item='+item+'&letter='+letter+'&action='+action+'&field='+field+'&table='+table,'sel_authors')
}

function expandcolapse(id) {
// използва се в addrem.php за линковете и в firm_admin.php(firm_fields) за фирите
// показва дървовидна структура на категориите с възможност за свиване и разгъване
if (!id) return true;
	ch='child'+id;
	id='main'+id;
	stat=event.srcElement.checked;
	allitems=document.all(id);
	if (allitems) {
		allch=document.all(ch);
		if (allitems.length>1) 
			for (ind=0;ind<allitems.length;ind++) 	{
				if (!stat) allch[ind].checked=false;
				allitems[ind].style.display=(stat) ? "block" : "none";
			}
		else {
			allitems.style.display=(stat) ? "block" : "none";
			if (!stat) allch.checked=false;
		}
	}
//statements
}

function FormValidation(frm) {
//allInput=document.all.inputform.elements;
allInput=frm.elements;
for (ind=0;ind<allInput.length;ind++) 
	if (allInput[ind].cond) {
		myCond=allInput[ind].cond;
		if (myCond.slice(-1)=='(') 
			eval("currCond=("+myCond+"allInput[ind].value))");
		else eval("currCond=(allInput[ind].value"+myCond+")");
			if (!currCond) {
				alert(allInput[ind].msg);
				return false;
		}
	}
return true
}

/* Menu Related */
startList = function() {
   if (document.all && document.getElementById) {
      initHovers("nav", "over");
   }
}

function initHovers(listName, cName) {
   navRoot = document.getElementById(listName);
   
   for (i = 0; i < navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      
      if (node.nodeName == "LI") {
         node.onmouseover = function() { this.className = cName; }
         node.onmouseout = function() { this.className = this.className.replace(cName, ""); }
      }
   }
}

var httpReq;

function createRequestObject() {
   var ro;

   if (window.XMLHttpRequest) {
      // If IE7, Mozilla, Safari, etc: Use native object
      ro = new XMLHttpRequest();
   } else {
      if (window.ActiveXObject) {
         // otherwise, use the ActiveX control for IE5.x and IE6
         ro = new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return ro;
}

function sendRequest(fValue, cf, cmsd) {
   currField = cf;
   currMsgDiv = cmsd;

   if(fValue > 0) {
      httpReq = createRequestObject();
      httpReq.open("POST", "check_box_number.php?n="+fValue, true);
      httpReq.onreadystatechange = handleResponse;
      httpReq.send(null);
   } else {
      document.getElementById(currMsgDiv).innerHTML = "<b>Please enter number!</b>";
   }
}

function checkReadyState() {
   switch(httpReq.readyState) {
      case 0:
         document.getElementById(currMsgDiv).innerHTML = "<b>Checking number...</b>";
         break;
      case 1:
         document.getElementById(currMsgDiv).innerHTML = "<b>Checking number...</b>";
         break;
      case 2:
         document.getElementById(currMsgDiv).innerHTML = "<b>Checking number...</b>";
         break;
      case 3:
         document.getElementById(currMsgDiv).innerHTML = "<b>Checking number...</b>";
         break;
      case 4:
         document.getElementById(currMsgDiv).innerHTML = "";
         return (httpReq.status == 200);
         break;
      default:
         document.getElementById(currMsgDiv).innerHTML = "<b>An unexpected error has occurred.</b>";
   }
}

function handleResponse() {
   if(checkReadyState()) {
      if(httpReq.responseText == 1)
         document.getElementById(currMsgDiv).innerHTML = "<b>The number is OK!</b>";
      else {
         document.getElementById(currMsgDiv).innerHTML = "<b>The number is already in use!</b>";
         document.getElementById(currField).value = "";
         document.getElementById(currField).focus();
      }
   }
}

function cordinate(xpic,ypic,xmarker,ymarker)
	{
	
		document.getElementById('xpic').value=xpic;
		document.getElementById('ypic').value=ypic;
		document.getElementById('xmarker').value=xmarker;
		document.getElementById('ymarker').value=ymarker;
	
	}	  
