// This file contains all validation functions

function set_form_action(arg)
{
	document.form.form_action.value = arg;	
	document.form.submit();	
}

function survey_set_form_action(arg, allow_send_to_library)
{	
	var ok = true;
	with(document.form)
	{
		if(is_empty(title,"Please insert a Survey Title !"))
			return false;

//		if(ok && is_empty(description,"Please insert a Survey Description !"))
//			return false;

		if (!allow_send_to_library) // will force to select a program  
		if (!valid_dropdown(program_id, 'Please select program for this survey!'))
			return false;

		form_action.value = arg;
		submit();	

	}
}

function submit_settings()
{
	var ok = true;
	with(document.form)
	{	
		if(is_empty(username,'Please type your username !'))
			ok = false;
			
		if(ok && is_empty(email,'Please type your email !'))
			ok = false;
					
		if(ok && !valid_email(email,'Your email is not valid !'))
			ok = false;
			
		if(current_password.value == "" && (new_password.value != "" || new_password1.value != ""))	
		{
			alert('Please type your current password !');
			ok = false;
			new_password1.focus();
		}
		
		if( ok && (new_password.value != new_password1.value))
		{
			alert('Your new password and retyped new password are different !');
			ok = false;
			new_password1.focus();
		}

		if(ok) submit();
	}
}

function valid_email(entered, text)
{
	with (entered)
	{
		apos=value.indexOf("@"); 
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
		{
			if (text!="") alert(text);
			focus();
			return false;
		}	
		else {return true;}
	}
}

function is_empty(entered, text)
{

	with (entered)
	{
	if (value==null || value=="")
		{
			if (text!="") alert(text);
			focus();
			return true;
		}
	else 
		return false;
	}	
}

function warn_on_delete(form_link,text)
{
	if(confirm(text)) document.location=form_link;
}

function valid_date(day,month,year,message)
{
	if(!valid_dropdown(day,message) || !valid_dropdown(month,message) || !valid_dropdown(year,message))
		return false;
	return true;	
}

function valid_dropdown(ddown,message)
{
	if (ddown.selectedIndex == 0) 
	{
		alert(message);
		return false;
	}
return true;	
}

function valid_radio(name,message)
{
	myOption = -1;
	for (i=name.length-1; i > -1; i--) 
		if (name[i].checked) 
			myOption = i;

	if (myOption == -1) 
	{
		alert(message);
		return false;
	}
	return true;
}

function is_checked(name)
{
	return name.checked;
}

function goto_page(golink)
{
	document.location=golink;
}

function remove_pb(name)
{
	//alert(name.type);
	if(name.type == "button") name.type = "hidden";
	//else name.type = 'button';
}

//-----------------------------------------------------------------------
function popup_window(link) {
	var width = 0.5;
	var height = 0.5;

	var w = width * screen.width;
	var h = height * screen.height;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
			
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl
	myWin = window.open(link,'picker','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'+winprops);
	myWin.focus();

	//window.open (link,"myWindow","mywindow","menu=0,location=0,status=0,scrollbars=1,width=200,height=400"); 
}

//-----------------------------------------------------------------------
function popup_window2(link) {
	var width = 0.5;
	var height = 0.5;

	var w = width * screen.width;
	var h = height * screen.height;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
			
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl
	myWin = window.open(link,'picker','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'+winprops);
	myWin.focus();

	//window.open (link,"myWindow","mywindow","menu=0,location=0,status=0,scrollbars=1,width=200,height=400"); 
}

