/**
 *
 * Smart controls
 * Copyright (c) J2B 2000.
 * All rights reserved.
 *
 * USE: 
 *   add prepareForms(Form frm) to body onLoad event.
 *   add validateForm(Form frm)  to form's onSubmit event.
 *
 * @version 1.0
*/

var __obj= new Object;

 function prepareForm(frm) {   
   if ( !frm || !frm.elements ) { 
     alert("Error in prepareForm function. Not a Form object"); 
     return false; 
   }

   var firstElem=null;
   for(i=0; i< frm.elements.length; i++ ) {
     str = frm.elements[i].onselect + "";
     if ( str == null ) continue;  
     if ( (p=str.indexOf("_")) != -1 ) {        
        __obj = frm.elements[i];
        eval("{" + str.substring(p));             
        if ( firstElem == null ) firstElem = frm.elements[i];
     }     
   }    
  if ( firstElem != null ) firstElem.focus(); 
  prepareFunction(frm);
  __obj= new Object;
 }

 function prepareFunction(frm) {   
   for(i=0; i< frm.elements.length; i++ ) {
       obj = frm.elements[i];   
     }     
 }
 
 function validateForm(frm) { 
   if ( !frm || !frm.elements ) {
     alert("Error in validateForm function. Not a Form object"); 
     return false;
   }
 
   for(i=0; i< frm.elements.length; i++ ) {
     obj = frm.elements[i];     

	if(obj.preCheck && !eval(obj.preCheck)) {
		continue;
	}

     if ( obj.notEmpty  || obj.mandatory) {
       if ( isEmpty(obj) ) {
         if ( obj.warning1 ) alert(obj.warning1);
         else alert("The field '"+obj.title+"' is mandatory.");
         obj.focus();
         return false;
       }
     }     

     if ( obj.maxLength && !isEmpty(obj) && obj.value.length > obj.maxLength) {
	 	alert("'" + obj.title + "' value too long (" + obj.maxLength + " simbols max).");
         obj.focus();
         return false;
     }     

     if ( obj.minLength && !isEmpty(obj) && obj.value.length < obj.minLength) {
	 	alert("'" + obj.title + "' value too short (" + obj.minLength + " simbols min).");
         obj.focus();
         return false;
     }     

     if ( obj.utype == 'int' ){
       if ( !checkInteger(obj, obj.minValue, obj.maxValue) ) { 
         addMsg="";
         if ( obj.minValue != null ) addMsg+="min=" + obj.minValue;
         if ( obj.maxValue != null ) addMsg+=" max=" + obj.maxValue;
         if ( addMsg!="" ) addMsg=" (" + addMsg + ") ";
         
         alert("Input integer value into the field '"+obj.title+"' "+addMsg+".");
         obj.focus();
         return false;
       }         
     }


     if ( obj.utype == 'float' ){
       if ( !checkFloat(obj, obj.minValue, obj.maxValue, obj.decimals) ) { 
         addMsg="";
         if ( obj.minValue != null ) addMsg+="min=" + obj.minValue;
         if ( obj.maxValue != null ) addMsg+=" max=" + obj.maxValue;
         if ( addMsg!="" ) addMsg=" (" + addMsg + ") ";
         
         alert("Input float value into the field '"+obj.title+"' "+addMsg+".");
         obj.focus();
         return false;
       }         
     }

     
    if ( obj.utype == 'email' && !checkEmail(obj.value) ){
        alert('You must enter correct e-mail address into the field ' + obj.title);
        obj.focus();
        return false;
    }

	if(obj.selectedIndexNonZero && obj.options && obj.selectedIndex == 0) {
		alert("'" + obj.title + "' field value must be specified.");
        obj.focus();
        return false;
	}     

   	/*if(obj.postCheck && !eval(obj.postCheck)) {
		alert("Wrong value for field '" + obj.title + "'.");
        obj.focus();
        return false;
	}*/
   }  

   return true;
 }
 
 /**
 *
 * Tools
 * Copyright (c) J2B 2000.
 * All rights reserved.
 *
 * @version 1.0
*/

function trim(s) {
	var i, j;
	for (i = 0; i < s.length && s.charAt(i) == " "; i++);
	for (j = s.length-1; j >= 0 && s.charAt(j) == " "; j--);

	if(i == s.length)
		return "";
	else
		return s.substring(i, j+1);
}

function isEmpty(obj) {
	return !obj || !obj.value || trim(obj.value) == "";
}

function isEmptyString(s) {
	return !s || trim(s) == "";
}

// checkEmail (STRING s)
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required

function checkEmail(s) {
   var reEmail = /^.+\@.+\..+$/
   if (isEmptyString(s)) return true;    
   return reEmail.test(s)
}

function checkInteger(obj, sign) {
	var s = obj ? obj.value.replace(',', '.') : null;
	var checkSign = sign ? true : false;
		
	if(!obj || isEmpty(obj)) return true;
	
	ret = !isNaN(s) && (s.indexOf('.') == -1) &&
		(checkSign ? (sign ? eval(s) >= 0 : eval(s) <= 0) : true);

	return ret;
}

function checkInteger(obj, minValue, maxValue) {
	var s = obj ? obj.value.replace(',', '.') : null;
		
	if(!obj || isEmpty(obj)) return true;
	if(minValue == null) minValue = Number.NEGATIVE_INFINITY;
	if(maxValue == null) maxValue = Number.POSITIVE_INFINITY;

	ret = !isNaN(s) && s.indexOf('.') == -1 && eval(s) >= minValue && eval(s) <= maxValue;

	return ret;
}

function checkFloat(obj, sign) {
	var s = obj ? obj.value.replace(',', '.') : null;
	var checkSign = sign ? true : false;
	
	if(!obj || isEmpty(obj)) return true;
	
	ret = !isNaN(s) &&
		(checkSign ? (sign ? eval(s) >= 0 : eval(s) <= 0) : true);

	return ret;
}

function checkFloat(obj, minValue, maxValue, decimals) {
	var s = obj ? trim(obj.value).replace(',', '.') : null;

	if(!obj || isEmpty(obj)) return true;
	if(minValue == null) minValue = Number.NEGATIVE_INFINITY;
	if(maxValue == null) maxValue = Number.POSITIVE_INFINITY;
	
	ret = !isNaN(s) && eval(s) >= minValue && eval(s) <= maxValue &&
		(decimals ? s.lastIndexOf('.') == -1 ||
		s.length - s.lastIndexOf('.') - 1 <= decimals : true);

	return ret;
}

function checkDate(obj) {
	var s = obj ? obj.value : null;
	
	if(!obj || isEmpty(obj)) return true;
	
	return true;
}

function checkRGB(obj) {
	if(isEmpty(obj)) return true;
	
	idx = obj.value.indexOf("#");
	
	if(idx == 0)
		s = obj.value.substr(1);
	else
		s = obj.value;

	if(s.indexOf("#") != -1 || s.length > 6 || parseInt(s, 16) == "NaN")
		return false;
	else
		return true;
}

function isLeapYear(year) {
	return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}

function roundVal(val, scale) {
	scale = Math.pow(10, scale);
	val = Math.round(val * scale) / scale;

	return val;
}
	
/*
* Parse functions
*/
function getFloat(val) {
	if(!val || val == isNaN(val))
		ret = 0;
	else
		ret = eval(val.replace(',', '.'));
	
	return ret;
}

function getInt(val) {
	if(!val || val == isNaN(val))
		ret = 0;
	else
		ret = eval(val);
	
	return ret;
}

function parseDate(s, fmt) {
	var day = 0;
	var month = 0;
	var year = 0;
	
	day = eval(s.substring(0, 2));
	month = eval(s.substring(3, 5));
	year = eval(s.substring(6));

	return new Date(year, month - 1, day);
}

function parseDateTime(s, fmt) {
	var seconds = 0;
	var minutes = 0;
	var hours = 0;
	var pos = s.indexOf(' ');
	var date = parseDate(s.substring(0, pos));
	
	if(date != null) {
		var time = s.substring(pos + 1);
		
		hours = eval(time.substring(0, 2));
		minutes = eval(time.substring(3, 5));
		seconds = eval(time.substring(6));
		
		date.setHours(hours);
		date.setMinutes(minutes);
		date.setSeconds(seconds);
	}

	return date;
}

/*
* RADIO & CHECKBOX routines
*/
function toggleObjectState(obj, idx, checked) {
	if(obj.length) {
		if(idx) {
			if(idx < obj.length) obj[idx].checked = checked;
		} else {
			// change state of ALL object items
			for(var i = 0; i < obj.length; i++) {
				obj[i].checked = checked;
			}
		}
	} else {
		obj.checked = checked;
	}
}

function checkObject(obj, idx) {
	toggleObjectState(obj, idx, true);
}

function unCheckObject(obj, idx) {
	toggleObjectState(obj, idx, false);
}

function objectCheckedIndexes(obj) {
	var idx = new Array();
	var j = 0;

	if(!obj.length) {
		if(obj.checked) idx[j] = 0;
	} else {
		for(i = 0; i < obj.length; i++) {
			if(obj[i].checked) idx[j++] = i;
		}
	}
	
	return idx;
}

function objectCheckedValues(obj) {
	var idx = objectCheckedIndexes(obj);

	if(obj.length) {
		for(var i = 0; i < idx.length; i++) {
			idx[i] = obj[idx[i]].value;
		}
	} else if(idx.length > 0) {
		idx[0] = obj.value;
	}
	
	return idx;
}

function radioCheckedIndex(obj) {
	var idx = objectCheckedIndexes(obj);

	return idx.length > 0 ? idx[0] : -1;
}

function radioCheckedValue(obj) {
	var val = objectCheckedValues(obj);
	
	return val.length > 0 ? val[0] : null;
}

/*
* Navigator test functions
*/
function isIE() {
	return navigatorName().toLowerCase().indexOf("microsoft internet explorer") != -1;
}

function isNN() {
	return navigatorName().toLowerCase().indexOf("netscape") != -1;
}

function navigatorName() {
	return navigator.appName;
}

function navigatorVersion() {
	return parseFloat(navigator.appVersion);
}