

function blank(field) {

	var trimmed_field = Trim(field);
	if ((field.length == 0) || (trimmed_field.length == 0)) return true;
	return false;
}

function Trim(trim_value) {
	
	if (trim_value.length < 1) {
		return "";
	}
	
	trim_value = rTrim(trim_value);
	trim_value = lTrim(trim_value);
	
	if (trim_value == "") {
		return "";
	}
	else {
		return trim_value;
	}
}

function rTrim(trim_value) {
	
	var w_space = String.fromCharCode(32);
	var v_length = trim_value.length;
	var strTemp = "";
	if(v_length < 0) {
		return "";
	}
	var iTemp = v_length -1;
	
	while (iTemp > -1) {
		if (trim_value.charAt(iTemp) == w_space) {
		}
		else {
			strTemp = trim_value.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;	
	}
	return strTemp;
}

function lTrim(trim_value) {
	
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return "";
	}
	var v_length = trim_value.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length) {
		if(trim_value.charAt(iTemp) == w_space) {
		}
		else {
			strTemp = trim_value.substring(iTemp,v_length);
		break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}
	
function isFloat(string) {
	return isNaN(parseFloat(string));
}

/**
 *
 */
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;
}

function compareDates(date1, date2) {

	if (date1 > date2) {
	    return false;
	}
	return true;
}

/**
 * Prepare date form fields printed with print_form_time3()
 */
function createTime(outfield, hour, minute, second) {
	outfield.value = '00:00:00';

	// accept no date at all
	if (isEmpty(hour.value) && isEmpty(minute.value) && isEmpty(second.value)) return true;

	// alert(day.value + '-' + month.value + '-' + year.value);

	//if (verifyTime(day, month, year) == false) return false;

	var _hour = parseInt(hour.value, 10);
	var _minute = parseInt(minute.value, 10);
	var _second = parseInt(second.value, 10);

	var hzero = '';
	var mzero = '';
	var szero = '';

	if (_hour < 10) hzero = '0';
	if (_minute < 10) mzero = '0';
	if (_second < 10) szero = '0';

	outfield.value = hzero + _hour + ':' + mzero + _minute + ':' + szero + _second;

	return true;
}

function isEmail(s) {
	return isValidEmail(s);
}

/**
 * Validates the email address.
 */
function isValidEmail(s) {
	var v = Trim(s);
	var we_had_monkey = false, relief = true;
	var we_had_dot = false;
	var count = 0;
	
	// sta je dobra email adresa?
	// mora da ima jedno majmunce
	// ne sme da ima razmake, moze da ima slova, cifre, crtice
	for(i = 0; i < v.length; i++) {
		c = v.charAt(i);
		count++;
		
		// tacka ne sme da ide iza majmunceta i
		// dve tacke ne smeju da idu jedna za drugom
		if (we_had_monkey == true) {
			if (c == '.') {
				we_had_dot = true;
				if (relief == true) {
					relief = false;
					continue;
				} else {
					return false;
				}
			}
			if (relief == false) relief = true;
		} else {
			if (c == '.') continue;
			if (c == '@') {
				// ima li znakova ispred majmunceta?
				if (count == 1) {
					return false;
				}
				count = 0;
				we_had_monkey = true;
				relief = false;
				continue;
			}
		}
		
		// standardno, slova, cifre i crtice su dozvoljeni
		if ((c >= 'a')&&(c <= 'z')) continue;
		if ((c >= 'A')&&(c <= 'Z')) continue;
		if ((c >= '0')&&(c <= '9')) continue;
		if ((c == '-')||(c == '_')) continue;
		
		return false;
	}
	
	
	// vracamo netacno ako nismo imali majmunce
	if (we_had_monkey == false) return false;
	else {
		// vracamo netacno ako je majmunce poslednji znak
		if (c == '@') return false;
		// vracamo netacno i ako je tacka poslednji znak i pretposlednji znak
		if ((c == '.')||(v.charAt(i-2) == '.')) return false;
		// vracamo netacno i ako nismo imali tacku posle majmunceta
		if (we_had_dot == false) return false;
		// vracamo netacno i ako je pred
	}
	return true;
}



function checkForm() {
	
	var f = document.forms["f"];
	
	// Contact Details
	if (blank(f.fullname.value)) {
		alert('Please type in name!');
		f.fullname.focus();
		return;
	}
	if (blank(f.email.value) || !isEmail(f.email.value)) {
		alert('Please type in correct e-mail address!');
		f.email.focus();
		return;
	}

	
	
	f.submit();
}

function toggle(object) {
  if (document.getElementById) {
    if (document.getElementById(object).style.visibility == 'visible')
      document.getElementById(object).style.visibility = 'hidden';
    else
      document.getElementById(object).style.visibility = 'visible';
  }

  else if (document.layers && document.layers[object] != null) {
    if (document.layers[object].visibility == 'visible' ||
        document.layers[object].visibility == 'show' )
      document.layers[object].visibility = 'hidden';
    else
      document.layers[object].visibility = 'visible';
  }

  else if (document.all) {
    if (document.all[object].style.visibility == 'visible')
      document.all[object].style.visibility = 'hidden';
    else
      document.all[object].style.visibility = 'visible';
  }

  return false;
}