// various configuration parameters

var conf_date_format = 'y-m-d';

var _whitespace = ' \n\r\t';
var _digits = '0123456789';
var _hexDigits = '0123456789ABCDEF';
var _smallChars = 'abcdefghijklmnopqrstuvwxyz';
var _capChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var _chars = _smallChars + _capChars;
var _protocols = new Array ('http://', 'file://', 'https://', 'ftp://');
var _image_extensions = new Array ('.gif', '.jpg', '.jpeg');


/**
 *	Common Functions
 */
 
function isEmpty(s) {
	if (s.length == 0) return true;
	if (trim(s).length == 0) return true;
	return false;
}

 

/**
 *	Returns the length of the chars that are in chars.
 */
function getValidCharsLength (t, chars, startAt) {
	for(var i = startAt; i < t.length; i++)
		if (chars.indexOf(t.charAt(i)) == -1)
			break;
	return (i - startAt);
}

function isEmail (t) {
	t = trim(t);
	i = getValidCharsLength (t, _smallChars + _capChars + _digits + '._-', 0);
	if (i < 1) return false;
	if (getValidCharsLength (t, '@', i) != 1) return false;
	dots = -1;
	do {
		len = getValidCharsLength (t, _smallChars + _capChars + _digits + '_-', ++i);
		if ((len < 2) || (len > 64)) return false;
		i += len;
		dots++;
	} while (getValidCharsLength (t, '.', i) == 1)
	
	return ((len < 6) && (len > 1) && (i >= t.length) && (dots > 0));
}

function isPassword(s) {
	return true;
}
	
function trim(s) {
	var i = 0;
	while(isspace(s.charAt(i))) i++;
	if (i == s.length) return new String('');
		
	var j = s.length - 1;
	while(isspace(s.charAt(j))) j--;
		
	return s.substring(i, j + 1);
}
	
function isspace(c) {
	if (c == ' ') return true;
	if (c == '\n') return true;
	return false;
}
	
function isFloat(string) {
	return isNaN(parseFloat(string));
}

/**
 * Checks whether parameter is a number.
 */
function isNumber(s) {
	var v = trim(s);
	var i, c, we_had_dot = false;
	
	for(i = 0; i < v.length; i++) {
		c = v.charAt(i);
		if (i == 0) if ((c == '-')||(c == '+')) continue;
		if ((c == '.')&&(we_had_dot == false)) {
			we_had_dot = true;
			continue;
		}
		if ((c < '0') || (c > '9')) return false;
	}
	
	return true;
}

/**
 *
 */
function verifyDate(day, month, year) {
	if ( isEmpty(day.value) || isEmpty(month.value) || isEmpty(year.value) )
		return false;
	
	var _day = parseInt(day.value, 10);
	var _month = parseInt(month.value, 10);
	var _year = parseInt(year.value, 10);
	
	// alert('[verifyDate checkpoint 1] ' + _day + '-' + _month + '-' + _year);
	
	if (isNaN(_day) || isNaN(_month) || isNaN(_year))
		return false;
	
	if ((_month < 1) || (_month > 12)) return false;
	
	var max_day_count;
	switch(_month) {
		case  1 :
		case  3 :
		case  5 :
		case  7 :
		case  8 :
		case 10 :
		case 12 :
			max_day_count = 31;
			break;
			
		case  2 :
			if ((_year % 4) == 0) max_day_count = 29;
			else max_day_count = 28;
			break;
			
		default :
			max_day_count = 30;
	}
	
	if ((_day < 1) || (_day > max_day_count)) return false;
	return true;
}

/**
 *
 */
function verifyDate2(_date) {
	if (isEmpty(_date))
		return false;
	
	var _day = parseInt(_date.substring(8, 10), 10);
	var _month = parseInt(_date.substring(5, 7), 10);
	var _year = parseInt(_date.substring(0, 4), 10);
	
	// alert('[verifyDate checkpoint 1] ' + _day + '-' + _month + '-' + _year);
	
	if (isNaN(_day) || isNaN(_month) || isNaN(_year))
		return false;
	
	if ((_month < 1) || (_month > 12)) return false;
	
	var max_day_count;
	switch(_month) {
		case  1 :
		case  3 :
		case  5 :
		case  7 :
		case  8 :
		case 10 :
		case 12 :
			max_day_count = 31;
			break;
			
		case  2 :
			if ((_year % 4) == 0) max_day_count = 29;
			else max_day_count = 28;
			break;
			
		default :
			max_day_count = 30;
	}
	
	if ((_day < 1) || (_day > max_day_count)) return false;
	return true;
}


/**
 * Prepare date form fields printed with print_form_date1()
 */
// todo: in development
function createDate1(outfield, infield, required) {
// todo: for print_form_date1()
	outfield.value = '9999-01-01';
	
	_date = infield.value;
	
	// todo: parse for incomplete date
	var _day = parseInt(_date.substring(0, 2), 10);
	var _month = parseInt(_date.substring(3, 2), 10);
	var _year = parseInt(_date.substring(6, 4), 10);
	
	if ((required != 'no') || (isEmpty(_date) != false))
		if (verifyDate(_day, _month, _year) == false) return false;
	
	return createDate(outfield, _day, _month, _year);
}

/**
 * Prepare date form fields printed with print_form_date2/3()
 */
function createDate(outfield, day, month, year) {
	outfield.value = '9999-01-01 00:00:00';
	
	// accept no date at all
	if (isEmpty(day.value) && isEmpty(month.value) && isEmpty(year.value)) return true;
	
	// alert(day.value + '-' + month.value + '-' + year.value);
	
	if (verifyDate(day, month, year) == false) return false;
	
	var _day = parseInt(day.value, 10);
	var _month = parseInt(month.value, 10);
	var _year = parseInt(year.value, 10);
	
	var mzero = '';
	var dzero = '';
	
	if (_month < 10) mzero = '0';
	if (_day < 10) dzero = '0';
	
	outfield.value = '' + _year + '-' + mzero + _month + '-' + dzero + _day;
	
	return true;
}


/**
 * TODO : VERIFY TIME
 */
 function verifyTime(minutes, hours) {
 	return true;
 }
 
/**
 * Formats both date and time values
 */
function createDateTime(outfield, minute, hour, day, month, year) {
	outfield.value = '9999-01-01 00:00:00';
	// accept no date at all
	if (isEmpty(day.value) && isEmpty(month.value) && isEmpty(year.value) && isEmpty(hour.value) && isEmpty(minute.value)) return true;
	
	//alert(day.value + '-' + month.value + '-' + year.value);
	//alert(hour.value + '-' + minute.value);
	
	if (verifyDate(day, month, year) == false) return false;
	
	if (verifyTime(hour, minute) == false) return false;
	
	var _day = parseInt(day.value, 10);
	var _month = parseInt(month.value, 10);
	var _year = parseInt(year.value, 10);
	
	var _hour = parseInt(hour.value, 10);
	var _minute = parseInt(minute.value, 10);
	
	var mzero = '';
	var dzero = '';
	
	var hzero = '';
	var minzero = '';
	
	if (_month < 10) mzero = '0';
	if (_day < 10) dzero = '0';
	
	if (_hour < 10) hzero = '0';
	if (_minute < 10) minzero = '0';
	
	outfield.value = '' + _year + '-' + mzero + _month + '-' + dzero + _day + ' ' + hzero + _hour + ':' + minzero + _minute;
	//alert(outfield.value);
	
	return true;
}

function getFlash(file_name, width, height, url, align) {
	var text = new String();
	text = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ZINDEX:10 width="'+width+'" height="'+height+'" align="'+align+'">';
  	text += '<param name="movie" value="g/images/'+file_name+'">';
	text += '<param name="quality" value="high">';
	if (window.navigator.mimeTypes && window.navigator.mimeTypes["application/x-shockwave-flash"]){ 
		text = '<embed src="g/images/'+file_name+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" align="'+align+'"></embed>';
	} else {
		file_name = file_name.replace('.swf','.jpg')
	
		if (url != '') {
			text+= '<a href="'+url+'">';
		}
		text += '<img  src="g/images/'+file_name+'" width="'+width+'"  height="'+height+'" border="0" vspace="0" hspace="0" align="'+align+'">';
		if (url != '') {
			text+= '</a>';
		}
		
	}
	text += '</object>';
	document.write(text);
}

function getFlashWithParam(file_name, width, height, url, align, param) {
	var text = new String();
	text = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" ZINDEX:10 width="'+width+'" height="'+height+'" align="'+align+'">';
  	text += '<param name="movie" value="g/images/'+file_name+'?mycontent='+ param +'">';
	text += '<param name="quality" value="high">';
	if (window.navigator.mimeTypes && window.navigator.mimeTypes["application/x-shockwave-flash"]){ 
		text = '<embed src="g/images/'+file_name+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" align="'+align+'"></embed>';
	} else {
		file_name = file_name.replace('.swf','.jpg')
	
		if (url != '') {
			text+= '<a href="'+url+'">';
		}
		text += '<img  src="g/images/'+file_name+'" width="'+width+'"  height="'+height+'" border="0" vspace="0" hspace="0" align="'+align+'">';
		if (url != '') {
			text+= '</a>';
		}
		
	}
	text += '</object>';
	document.write(text);
}

   function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }


   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }
   
 

    
    function hiLite(imgDocID,imgObjName) {
    if (parseInt(navigator.appVersion) >= 3)
    {
    document.images[imgDocID].src = eval(imgObjName + ".src")
    }}		

