function getByID() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') element = document.getElementById(element);
		if (arguments.length == 1) return element;
		elements.push(element);
	}
	return elements;
}

//Kurses
function calcItems(){
	var chks = document.getElementsByName('item[]');
	var hasChecked = false;
	var sum = 0;
	var cnt = 0;
	//alert(chks.length);
	
	for (var i = 0; i < chks.length; i++){
		if (chks[i].checked){
			cnt++;
			sum += parseInt(chks[i].value);
			if(cnt>=1){
				hasChecked = true;
			}
		}
	}
	if (!hasChecked){
		getByID('kursCustomSum').innerHTML = '---';
		alert("Выберите хотя бы 1 курс!");
		chks[0].focus();
	} else {
		getByID('kursCustomSum').innerHTML = sum;
	}
}

function uncheckItems(){
	var chks = document.getElementsByName('item[]');
	for (var i = 0; i < chks.length; i++){
		if (chks[i].checked) chks[i].checked=false;
	} 
}

function uncheckOne(){
	var chks = document.getElementById('items');
	chks.checked=false;
}

function checkItems(){
	var chks = document.getElementsByName('item[]');
	var itms = document.getElementsByName('itemid[]');
	var hasChecked = false;
	$cnt=0;
	for (var i = 0; i < chks.length; i++){
		if (chks[i].checked){
			$cnt++;
			if($cnt>=1){
				hasChecked = true;
				//itms[i].value = itms[i].value + '|' + chks[i].value;
				//break;
			}
		} else {}
	}
	if (!hasChecked){
		alert("Выберите хотя бы 1 курс!");
		chks[0].focus();
		return false;
	} else {
		for (var i = 0; i < chks.length; i++){
			if (chks[i].checked) itms[i].value = itms[i].value + '|' + chks[i].value;
			else itms[i].value = '';
		}
		//$('kursCustomSum').innerHTML = sum;
		getByID('form_kurses').submit();
		return true;
	}
}
function checkOne() {
	var chks = document.getElementById('items');
	if(chks.checked){
		getByID('form_kurses').submit();
		return true;
	} else {
		alert("Выберите начальный уровень!");
		chks.focus();
		return false;
	}
}
//End of Kurses

function checkPass(val1, val2){
	if($(val1).value==$(val2).value) return true;
	else{
		alert('Пароли не совпадают!');
		return false;
	}
}

function showWin(sourcefile, i, w, h) {
	window.open(sourcefile,i,"width="+w+",height="+h+",screenX=0,screenY=0,left=0,top=0,resizable=no,scrollbars=no");
}

function confirmDel(ref, msg) {
	if (window.confirm(msg)) {
		window.location = ref;
	}
}
// Validate functions
// you are free to change the values below to fit your style

var nnjsCommonTxt     = "Entered value is not valid";   // Generic error message
var nnjsIsNotEmptyTxt = "Заполните обязательные поля!";    // Field is empty
var nnjsIsDigitTxt    = "Numeric value expected";       // Field is not numeric
var nnjsIsDateTxt     = "Неправильный формат даты, вводите в виде: ДД-ММ-ГГГГ";    // Field is not a valid date
var nnjsIsEmailTxt    = "Введите корректный E-mail адрес!"; // Field is not a valid email address
var nnjsIsRegexTxt    = "Entered value did not match given restrictions"; // Field didn't match regexp

var nnjsIsDateFmt     = "DD-MM-YYYY"; // Default format for date value

// DO NOT CHANGE ANYTHING PAST THIS LINE!

function nnjsValidateFocus( item ){
// bring focus to the form item if it can have focus
	if( item.type == "text" ||
		item.type == "password" || 
		item.type == "checkbox" || 
		item.type == "select-one" ||
		item.type == "select-multiple" ||
		item.type == "button" ||
		item.type == "textarea"
	){
		item.focus();
		return true;
	} else{
		return false;
	}
}

function nnjsValidateAlert( msg1, msg2, msg3, msg4 ){
// alert user using appropriate message
	if( msg1 ){ alert( msg1 ); }
	else{
		if( msg2 ){ alert( msg2 ); }
		else{
			if( msg3 ){ alert( msg3 ); }
			else{
				if( msg4 ){ alert( msg4 ); }
			}
		}
	}
	return true;
}

function nnjsValidateDate(year, month, day){
// validate date
// the full year, for example, 1976 (and not 76 for 1976)
// the month as an integer between 0 and 11 (January to December)
// the date as an integer between 1 and 31
	if( day < 1 || day > 31 || month < 0 || month > 11 || year < 0 ) return false;
	if( day == 31 && ( month == 3 || month == 5 || month == 8 || month == 10 )) return false;
	if( day > 29 && month == 1 ) return false;
	if( day == 29 && month == 1 && (( year % 4 != 0 ) || ( year % 100 == 0 && year % 400 != 0 ))) return false;
	return true;
}

function nnjsValidateForm( fm ){
// main validation routine
	for(i = 0; i < fm.elements.length; i++ ){
		var item = fm.elements.item(i);
		if( item && !item.disabled ){

			// check for NotEmpty
			if(item.getAttribute('IsNotEmpty') != null){
				// check if item is filled/selected
				if((item.value == "") || ( item.type == "checkbox" && !item.checked )){
					nnjsValidateFocus(item);
					nnjsValidateAlert(
						item.IsNotEmptyTxt,
						item.CommonTxt,
						nnjsIsNotEmptyTxt,
						nnjsCommonTxt
					);
					return false;
				}
			}

			// check if item is numeric
			if( item.getAttribute('IsDigit') != null ){
				var rex = /^-?\d+(\.\d+)?$/;
				if( item.IsDigit != "" ){
					if( item.IsDigit.search( /^-?([=z]?\d)|(z?\*)(\.([=z]?\d)|(z?\*))$/ ) == 0 ){
					//replacing rex with appropriate regexp
					}
				}
				if( item.value != "" && item.value.search( rex ) == -1 ){
					nnjsValidateFocus( item );
					nnjsValidateAlert(
						item.IsDigitTxt,
						item.CommonTxt,
						nnjsIsDigitTxt,
						nnjsCommonTxt
					);
					return false;
				}
			}

			// check if item is date
			if( item.getAttribute('IsDate') != null ){
				var fmt = item.getAttribute('IsDate');
				if( fmt == "" ) fmt = nnjsIsDateFmt;

				// determine order of date fields
				var mpos = fmt.search( /MM/ );
				var dpos = fmt.search( /DD/ );
				var ypos = fmt.search( /YYYY/ );
				var midx = 1, didx = 1, yidx = 1;
				if( mpos > dpos ) midx++;
				if( mpos > ypos ) midx++;
				if( dpos > mpos ) didx++;
				if( dpos > ypos ) didx++;
				if( ypos > mpos ) yidx++;
				if( ypos > dpos ) yidx++;

				// build regular expression according to date format
				fmt = fmt.replace( /\\/g, "\\\\" );
				fmt = fmt.replace( /MM/g, "(\\d{1,2})" );
				fmt = fmt.replace( /DD/g, "(\\d{1,2})" );
				fmt = fmt.replace( /YYYY/g, "(\\d{4})" );
				var rex = new RegExp( '^' + fmt + '$' );
				var sep = item.value.match( rex );

				if( item.value != "" &&
					( sep == null || !nnjsValidateDate( sep[yidx], sep[midx]-1, sep[didx] ))){
					nnjsValidateFocus( item );
					nnjsValidateAlert(
						item.IsDateTxt,
						item.CommonTxt,
						nnjsIsDateTxt,
						nnjsCommonTxt
					);
					return false;
				}
			}

			// check if item is valid email address
			if( item.getAttribute('IsEmail') != null ){
				
				if( item.value != "" && item.value.search( /^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i ) == -1 ){
				//if( item.value != "" && item.value.search( /^[\w\.-_]+@[\w-_]+\.[\w\.-_]+$/ ) == -1 ){
					nnjsValidateFocus( item );
					nnjsValidateAlert(
						item.IsEmailTxt,
						item.CommonTxt,
						nnjsIsEmailTxt,
						nnjsCommonTxt
					);
					return false;
				}
			}

			// check if item matches the regular expression
			if( item.getAttribute('IsRegex') != null ){
				var rex = new RegExp( item.IsRegex );
				if( item.value != "" && item.value.search( rex ) == -1 ){
					nnjsValidateFocus( item );
					nnjsValidateAlert(
						item.IsRegexTxt,
						item.CommonTxt,
						nnjsIsRegexTxt,
						nnjsCommonTxt
					);
					return false;
				}
			}

		} // if
	} // for

	// all checks passed - form data is ok.
	return true;
} // nnjsValidateForm

/* nnjsValidateForm.js - Universal HTML Form Validator JScript
*
* Copyright (C) 2002 by N-Networks. All Rights Reserved.
* http://www.dpsp-yes.com
*
* Modification history:
*
* Feb 21, 2002  v1.0.0   V.Bulatov     - initially created
* Mar 05, 2002  v1.0.1   V.Zakharychev - cleaned up some names and texts
* Aug 26, 2002  v1.1.0   V.Bulatov     - IsDate attribute added
*
* Sample usage: 
* <form .... OnSubmit="return nnjsValidateForm( this );">
*   or 
* <form .... OnSubmit="return nnjsValidateForm( this ) && SomeUsersFunction(....);">
* ....
* <input type=text IsRegex="^[abc]$"  .... IsRegexTxt="bla bla" >
* <input type=text IsNotEmpty IsDigit .... IsNotEmptyTxt="bla1" IsDigitTxt="bla2">
* <input type=text IsNotEmpty IsEmail .... CommonTxt="bla bla">
* ....
* </form>
*
* See nnjsValidateForm.html for more details
*/
