//******************************************************************************
// convert date to MySql format
//******************************************************************************
function DateToMySql(x) {
  x = x.substr(6,4)+"-"+x.substr(0,2)+"-"+x.substr(3,2);
  return x;
}  
//******************************************************************************
// includes browserSpecific CSS files
//******************************************************************************
function includeCSS(css) {
	var browserType = getBrowserType();
	var fileName = "/css/" + css
	var baseFileName = fileName + ".css";
	switch (browserType) {
		case 'F' :
			fileName += "_firefox"; break;
		case 'I' :
			fileName += "_ie"; break;
		case 'S' :
			fileName += "_safari"; break;
	}
	fileName += ".css";
	document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\""+baseFileName+"\">");
	document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\""+fileName+"\">");
}

//******************************************************************************
// Check for different browsers
// Returns browserType e.g. F := Firefox, I := InternetExplorer, S := Safari 
//******************************************************************************
function getBrowserType() {
	if (navigator.userAgent.indexOf('Firefox')!=-1) {
		return 'F';
	} else if (document.all) {
		return 'I';
	} else {
		return 'S';
	}
}

//******************************************************************************
// Close certain window (use for closing form's window where browser couldn't
// close it correctly on submitting)
//******************************************************************************
function closeAfterForm(wn) {
	if (navigator.userAgent.indexOf('Firefox')!=-1 ||
//      navigator.userAgent.indexOf("3.0.3 Safari")!=-1) {
      navigator.userAgent.indexOf("Safari")!=-1) {
		 w_old=window.open('',wn);
		 w_old.close();
	}
}

//******************************************************************************
// Close window of the current form in browsers where it can be done correctly
// on submitting without breaking the submit
//******************************************************************************
function closeInForm() { 
		if (navigator.userAgent.indexOf('Firefox')==-1 && 
//        navigator.userAgent.indexOf("3.0.3 Safari")==-1) window.close();
        navigator.userAgent.indexOf("Safari")==-1) window.close();
}
		
//******************************************************************************
// Truncates spaces and tab signs from the string [x]
//******************************************************************************		
function truncSpaces(x) {
	var i, s, datatrim='';
	for (i=0; i<x.length; i++) {
			s=x.charCodeAt(i);
			if ((s!=32) && (s!=160)) { 
					 datatrim=datatrim+x.charAt(i);
			}				
	}
	return datatrim;
}										
//******************************************************************************
// Truncates spaces and tab signs from the beginning and end of the string [x]
//******************************************************************************		
function truncEdgeSpaces(x) {
	var i, s, datatrim='';
	for (i=0; i<x.length; i++) {
			s=x.charCodeAt(i);
			if ((s!=32) && (s!=160)) { 
					 datatrim=x.substr(i);
					 break;
			}				
	}
	for (i=datatrim.length-1; i>=0; i--) {
			s=datatrim.charCodeAt(i);
			if ((s!=32) && (s!=160)) { 
					 datatrim=datatrim.substr(0,i+1);
					 break;
			}				
	}
	return datatrim;
}										

//******************************************************************************
// Tests if the date [d] is correct (or empty)
//******************************************************************************
function testDateMySql(d,mess) { 
  var dd,mm,yy,xx,s,simb;

	if (d.length>0) {
			for (i=0; i<d.length; i++) {
					s = d.charCodeAt(i);
					simb = d.charAt(i);
					if ((s!=32) && (s!=160) && (simb!="-"))  {
					   if (s<47 || s>57) {
					 		  alert(mess); 
							  return false;
					   }
			    }
     }


		 xx = d.split('-');
		 if (xx.length!=3) {alert(mess); return false;}
		 else {
					l = 0;
					for (i=0; i<3; i++) {
							while (xx[i].substr(0,1)=="0" || xx[i].substr(0,1)==" ") xx[i]=xx[i].substr(1,xx[i].length);
							l = l+xx[i].length;
					}
					if (l>0) {
							mm = parseInt(xx[1]);
					 		if ((""+mm)=="NaN" || mm<1 || mm>12) {alert(mess); return false;}
					 		else {
									 yy = parseInt(xx[0]);
									 if ((""+yy)=="NaN" || (yy <1900)) {alert(mess); return false;}
							 		 else {
												dd = parseInt(xx[2]);
										 		if ( (""+dd=="NaN") ||  (dd<1) || (dd>31) ||  ((mm==4 || mm==6 || mm==9 || mm==11) && (dd==31)) ||
											 		 	 (mm==2 && dd>29) || (mm==2 && dd==29 && (yy-parseInt(yy/4)*4>0)) ) {
															 	 alert(mess); return false;
												}			 													
						   		 }
					 		}
					}	 
		 }
	 }
	 return true;
}

//******************************************************************************
// Checks if the date [x] is empty
//******************************************************************************
function isemptyDate(x, div)  {
  var xx,i,xxx;
// test if the data is empty or filled with zeros, x must be a valid data or zero data 
  if (x.length==0) return true;
  else {
      if (!div) div = '-'; 
 			xx = x.split(div);
			if (xx.length<3)  return true;
			for (i=0; i<xx.length; i++) {
					while (xx[i].length>0 && xx[i].substr(0,1)=='0') xx[i]=xx[i].substr(1);
					xxx = parseInt(xx[i]);
 					if ((""+xxx)=="NaN" || xxx==0)  return true;
			}
  }
	return false;
}

//***************************************************************************************
// Add [x] to the string [f] with divider ';' only if the string hasn't x in its content 
//***************************************************************************************
function addNameToList(f,x) {
// f - field value in the form (with full name, as string , i.e. "document.forms.a.b.value"), x - value to add
    x = truncSpaces(x);
		if (x.length>0) {
			 array1 = eval(f+".split(';')");
			 if (array1.length>0) {
			     for (i=0; i<array1.length; i++) {
			 		 		 if (truncSpaces(array1[i])==x) return;
					 }
			 }
			 stroka = truncSpaces(eval(f));

			 if (stroka.length>0) {
			     x1 = stroka.substr(stroka.length-1,1); 
				   if (x1==',' || x1=='.') eval(f+" = stroka.substr(0,stroka.length-1)+'; '");	
					 else if (x1!=';') eval(f+" = "+f+"+'; '");
			 }
			 eval(f+" = "+f+" + x + '; '");
		}		 
		return;
}

//******************************************************************************
// Returns date [x] as 10-digits (with all leading zeros) XXXX-XX-XX
//******************************************************************************
function fullDate(x) {
	x1 = x.split("-");
	if (x1[0].length<4) {
			x1[0]="0000"+x1[0]; x1[0] = x1[0].substr(x1[0].length-4,4);
	}
	if (x1[1].length<2) {
		 	x1[1]="00"+x1[1]; x1[1] = x1[1].substr(x1[1].length-2,2);
	}
	if (x1[2].length<2) {
			x1[2]="00"+x1[2]; x1[2] = x1[2].substr(x1[2].length-2,2);
	}
	return (x1[0]+"-"+x1[1]+"-"+x1[2]);
}

//******************************************************************************
// Returns short notation for browser name
//******************************************************************************
function defBrowserName() {
		if (navigator.userAgent.indexOf('Firefox')!=-1) return 'F';
		else if (navigator.userAgent.indexOf('Safari')!=-1) return 'S';
		else return 'U'; // Unknown
}

//******************************************************************************
// Opens new window with certain name, width, heights, left/top coordinates
//******************************************************************************
function openW(wname,wWidth,wHeight,leftCoord,topCoord) {
	var wnew,str_param;
	if (!leftCoord) leftCoord = 50;
	if (!topCoord) topCoord = 50;
	str_param="toolbar=0,menubar=1,location=0,scrollbars=1,width="+wWidth+",height="+wHeight+",resizable=1,left="+leftCoord+",top="+topCoord;
	wnew=window.open("",wname,str_param);
	wnew.focus();
	return wnew;
}

//******************************************************************************
// Opens new window with certain name, width, heights, left/top coordinates
//******************************************************************************
function openUrl(e, url, name, width, height, leftOff, topOff) {
	// prevent from bubbling up
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	var wnew,str_param;
	if (!leftOff) leftCoord = 50;
	if (!topOff) topCoord = 50;
	str_param="toolbar=0,menubar=1,location=0,scrollbars=1,width="+width+",height="+height+",resizable=1,left="+leftOff+",top="+topOff;			 
	wnew=window.open(url,name,str_param);
	wnew.focus();
	return false;
}

function openWindow(e, url) {
	// prevent from bubbling up
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	wnew=window.open(url);
	wnew.focus();
	return false;
}

function followUrl(e, url) {
	// prevent from bubbling up
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	window.location=url;
	return false;
}

// Prevent Bubbling to higher elements
function preventBubbling(e) {
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler) {
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else {
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
	
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}

function handleEvent(event) {
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers) {
		if (!Object.prototype[i]) {
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}
	if (this.$$handler) this.$$handler = null;
	return returnValue;
}

function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function() {
	this.returnValue = false;
}
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener) {
	document.onreadystatechange = function() {
		if (window.onload && window.onload != handleEvent) {
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}

// needed to detect the screen size
addEvent(window, 'load', setScreenClass);
addEvent(window, 'resize', setScreenClass);
    //window.onload = setScreenClass; 
	//window.onresize = setScreenClass;

	//  Following transition classes will be declared:
	//
	//	classname		  screenwidth
	//	------------------------------------------
	//	pda_v			  240px			
	//	pda_h			  320px			
	//	ultralow		  320px -  640px	
	//	screen_lo		  640px -  800px	
	//	screen_med		  800px - 1024px	
	//	screen_hi		 1024px - 1280px	
	//	screen_wide				> 1280px			

function setScreenClass(){
	var fmt = document.documentElement.clientWidth;
	var cls = (fmt<=240)?'pda_ver':(fmt>240&&fmt<=320)?'pda_hor':(fmt>320&&fmt<=640)?'screen_ultralow':(fmt>640&&fmt<=800)?'screen_low':(fmt>800&&fmt<=1024)?'screen_med':(fmt>1024&&fmt<=1280)?'screen_high':'screen_wide';
	//document.getElementById('count').innerHTML=fmt+'px -> '+cls;
	document.body.className=cls;
};
