Page_ValidationActive = false;

document.forms[0].onsubmit_VSA = document.forms[0].onsubmit;
document.forms[0].onsubmit = function()
{
	if(typeof(ValidatorOnSubmit)=='function'){
		ValidatorOnSubmit();
		if (Page_ValidationActive && !Page_IsValid) {
			SetFocusOnField(DiscoverFirstInvalidField_client());
		}else{
			if(document.forms[0].onsubmit_VSA) document.forms[0].onsubmit_VSA();
			DisableSecondSubmit();
		}
	}else{
		if(document.forms[0].onsubmit_VSA) document.forms[0].onsubmit_VSA();
		DisableSecondSubmit();
	}
}


function DisableSecondSubmit(){
	if (Page_ValidationActive && !Page_IsValid) 
		return;
	if(document.forms[0].enableSecondSubmit && document.forms[0].enableSecondSubmit=='true') 
		return;
	
	var i;
	var alertText = 'You have already submitted the form.';
	
	var inputs = document.getElementsByTagName('input');
	for(i=0; i<inputs.length; i++){
		// submit buttons and other widgets, any of which might have autopostback enabled
		inputs[i].onclick = function(){
			if(this.type == 'submit' || this.type == 'image') alert(alertText);
			return false;
		}
	}
	
	// tab flips, link buttons
	var links = document.getElementsByTagName('a');
	for(i=0; i<links.length; i++){
		if(links[i].href.indexOf('__doPostBack') > -1){
			links[i].onclick = function(){
				alert(alertText);
				return false;
			}
		}
	}
}



function OnServerSideValidation(field)
{
	if(typeof(ValidationSummaryOnSubmit) == 'function') {
		ValidationSummaryOnSubmit();
	}
	if(field != '') SetFocusOnField(document.getElementById(field));
}


function DiscoverFirstInvalidField_client()
{
	if(!Page_IsValid){
		if (typeof(Page_Validators) != 'undefined'){
			var i, val;
			for (i = 0; i < Page_Validators.length; i++) {
				val = Page_Validators[i];
				if (!val.isvalid) {
					if(val.controltovalidate != null && val.controltovalidate != ''){
						return document.getElementById(val.controltovalidate);
					}
					return null;
				}
			}
		}
	}
	return null;
}


function SetFocusOnField(field){
	if(field != null && field.focus){
		var x = document.body.scrollTop;
		field.focus();
		if(field.select) field.select();  // drop-downs do not have a select() method
		x -= document.body.scrollTop;
		if(x > 0){
			document.body.scrollTop -= 50;
		}
		if(x < 0){
			document.body.scrollTop += 50;
		}
	}
}


