// #########################################################
// Form saving handlers
// #########################################################

var arr_handlers = new Array();
var do_validate = true;

function validateForm( form )
{
	if( do_validate == false ) return true;

	handler_call = new Array();
	for( i=0; i<arr_handlers.length; i++ )
	{
		//alert(arr_handlers[i]);
		handler_call[i] = new Function(arr_handlers[i])
		var rez = handler_call[i]();
		if( rez == false ) { return false; }
	}
	
	please_save = false;
	return true;
}

function CheckNumber2(id)
{
	
	Reg = /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;
	//Reg = /^[0-9]*\.[0-9]+$/;
	//Reg = /^\d*$/;
	msg = "Please enter number.";
	obj = document.getElementById(id);

	return CheckValid(obj, Reg, msg);
}

function CheckEmail2(id)
{
	Reg = /^[a-z0-9]([a-z0-9]|([\w\-]+[a-z0-9]))*(\.([a-z0-9]|[a-z0-9][\w\-]+[a-z0-9]))*@[a-z0-9][\w\-]*[a-z0-9]\.([a-z0-9][\w\-]*[a-z0-9]\.)*[a-z]{2,4}$/;
	//Reg = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z0-9._%-]{2,4}$/;
	//Reg = /^(([A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+)*@[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+)*)?)$/;
	msg = "Please enter correct e-mail.";
	obj = document.getElementById(id);
	
	return CheckValid(obj, Reg, msg);
}

function CheckEmptyInputText(id)
{
	msg = "Please complete mandatory field.";
	obj = document.getElementById(id);
	s = stripInitialWhitespace(obj.value);
    s = stripClosingWhitespace(obj.value);
    if( s == null || s.length == 0 )
	{
		alert(msg);
		//setTimeout( fieldFocus(obj) , 10 );
		fieldFocus(obj);
		return false;
	}
	return true;
}

function CheckDateVal2(id)
{
	Reg = /^(0[1-9]|1[012])[\.\/-](0[1-9]|[12][0-9]|3[01])[\.\/-]\d\d\d\d$/;
	msg = "Please use date format MM/DD/YYYY.";
	msg2 = "Entered date is invalid";
	obj = document.getElementById(id);
	if( CheckValid(obj, Reg, msg) == true )
	{
		if( obj.value == '' ) { return true; }
		
		//now check if date valid
		var parts = obj.value.split('.');
		if( !parts[2] ) { parts = obj.value.split('/'); }
		if( !parts[2] ) { parts = obj.value.split('-'); }
		
		yy = parts[2];
		mm = parts[0];
		dd = parts[1];
		
		var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
		if( parseFloat(dd) != dt.getDate()    )
		{
			alert(msg2);
			//setTimeout( fieldFocus(obj) , 10 );
			fieldFocus(obj);
			return false;
		}
		if( parseFloat(mm)-1 != dt.getMonth() )
		{
			alert(msg2);
			//setTimeout( fieldFocus(obj) , 10 );
			fieldFocus(obj);
			return false;
		}
		return true;
	}
	return false;
}

function CheckURL2(id)
{
	Reg = /^(http|https|ftp):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(:(\d+))?(\/)?/;
	//Reg = /^(http|https|ftp):\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(:(\d+))?(\/[a-zA-Z0-9._-~?&#=]*)*/;
	msg = "Please enter correct HTML link 'http://name.dot/path' \n (supports also https and ftp sites)";
	obj = document.getElementById(id);
	return CheckValid(obj, Reg, msg);
}

function CheckLongText2(id, MaxLength)
{
	obj = document.getElementById(id);

	z_value = obj.value;
	//MaxLength = 1000;
	if( z_value.length > MaxLength )
	{
		if (confirm("Maximum size of field is " + MaxLength + " symbols."))
		{
			obj.value = z_value.substring(0,MaxLength);
			return true;	
		}
		else
		{		
			return false;
		}
	}
	return true;
}

function CounterLongText2(id, MaxLength)
{
	obj = document.getElementById(id);

	z_value = MaxLength-obj.value.length;

	if( z_value < 0 )
		document.getElementById('cl'+id).style.color='red';
	else
		document.getElementById('cl'+id).style.color='';
		
	document.getElementById('cl'+id).innerHTML = z_value;
	return true;
}

function CheckLongHtml(id, MaxLength)
{
	obj = document.getElementById(id);

	contents = obj.value;
	contents = contents.replace(/(\n|\r)/g, '');//Don't count HTML tags
	contents = contents.replace(/<head>(.*)<body>/m, '');//Don't count HTML tags
	contents = contents.replace(/<([^>]+)>/g, '');//Don't count HTML tags
	contents = contents.replace(/&(.{2,4});/g, ' ');//Count nbsp; as one keystroke
	while (contents.substring(contents.length-1, contents.length) == ' ')
	{
		contents = contents.substring(0,contents.length-1);
	}
	z_value = MaxLength-contents.length;
	if( z_value < 0 )
	{
		if (confirm("Maximum size of field is " + MaxLength + " symbols."))
		{
			//"Maximal size of field is " + MaxLength + " symbols.\nDo You want to cut enty to " + MaxLength + " symbols?"
			//obj.value = obj.value.substring(0,MaxLength); 
			var newMaxLength = parseInt(MaxLength);
			var index = 0; 
			 while (index != -1) 
			 { 
		 		if (index == 0) 
		 		{
		 			index = obj.value.indexOf('<');
		 		}
		 		else 
		 		{
		 			index = obj.value.indexOf('<', index + 1);
		 		}
		 		if (index >= newMaxLength){index = -1;}
		 		if(index != -1)  
			 	{
			 		val = index
			 		index = obj.value.indexOf('>', index + 1);
			 		if (index != -1) 
			 		{
			 			newMaxLength = (newMaxLength+(index-val+1));
			 		}
			 	}
			 }
				obj.value = obj.value.substring(0,newMaxLength);
			return true;	
		}
		else
		{		
			return false;
		}
	}

	return true;
}

function CounterLongHtml(id, MaxLength)
{
	obj = document.getElementById(id);

	contents = obj.value;
	contents = contents.replace(/(\n|\r)/g, '');//Don't count HTML tags
	contents = contents.replace(/<head>(.*)<body>/m, '');//Don't count HTML tags
	contents = contents.replace(/<([^>]+)>/g, '');//Don't count HTML tags
	contents = contents.replace(/&(.{2,4});/g, ' ');//Count nbsp; as one keystroke
	z_value = MaxLength-contents.length;
	if( z_value < 0 )
		document.getElementById('cl'+id).style.color='red';
	else
		document.getElementById('cl'+id).style.color='';
		
	document.getElementById('cl'+id).innerHTML = z_value;
	return true;
}

// ##############################################################################
// Functions
// ##############################################################################

function CheckValid(obj, Reg, msg )
{
	if( obj.value == '' )
		return true;

	if(obj.value.search(Reg)==-1)
	{
		alert(msg);
		//setTimeout( fieldFocus(obj) , 10 );
		fieldFocus(obj);
		return false;
	}
	return true;
	
}

function fieldFocus(obj)
{
	obj.focus();
}

function getkey(e)
{
	if( window.event )
		return window.event.keyCode;
	else if(e)
		return e.which;
	else
		return null;
}

// function for filtering specific characters
// used on input fields where only numbers alowed
// with event onKeyPress="return goodchars(event,'0123456789')"
function goodchars(e, goods)
{
	var key, keychar;
	key = getkey(e);
	if( key == null ) return true;

	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();

	// check goodkeys
	if (goods.indexOf(keychar) != -1)
		return true;
	
	// control keys
	if( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;

	// else return false
	return false;
}
