function validate_form(form)
{
	var col = form.elements;
	var errors="";
	var elem;
	var atLeastOne = false;
	
	
	
	for (var i = 0; i < form.elements.length; i++)
	{
		if(col[i].name == "@have.eld" && col[i].checked)
			atLeastOne = true;
			
		if (((col[i].type == "text")||(col[i].type == "textarea")) && (trim(col[i].value).length > 0))
			atLeastOne = true;
		else if ((col[i].type == "text") && (trim(col[i].value).length > 0))
				errors += message1 + "\n";
		else if ((col[i].type == "select-one") && (col[i].selectedIndex > 0))
			atLeastOne = true;
	}
	
	if (null != form.langList){
		if(form.langList.value != "")
			atLeastOne = true;
	}
	
	if ((false == atLeastOne) && errors =="")
	{
		return checkLibrary();	// patikrina ar nepasirinktos bibliotekos
	}
	else if (errors!="") 
		{
	    $("#dialog").dialog("open");
        $("#dialogContent").html(errors);
			return false;
		}
		else return true;
}
function checkLibrary(){
	var libChkBox = document.getElementById("exeptLnb");
	var libs = document.getElementById("libraryList");
	
	if ((null != libChkBox) && (null != libs)){
		if(libChkBox.checked == true)
			return true;
		else if (libs.selectedIndex >= 0)
			return true;
		else{
		    $("#dialog").dialog("open");
	        $("#dialogContent").html(message2);
			return false;
		}
	}
	else{
	    $("#dialog").dialog("open");
        $("#dialogContent").html(message2);
		return false;
	}
}

function validate(form, noInput)
{
	var col = form.elements;
	var errors="";
	var atLeastOne = false;
	var elDocs = false;
	var no_inputs = noInput.split("@");
	for (var i = 0; i < form.elements.length; i++)
	{
		for (var j = 0; j < no_inputs.length; j++)
			if ((trim(col[i].value).substring(1) == no_inputs[j])&& (no_inputs[j] != ""))
				elDocs = true;
		if ((col[i].type == "text") && (trim(col[i].value).length > 0))
			atLeastOne = true;
		else
			 if ((col[i].type == "text") && (trim(col[i].value).length > 0)) 
				errors += message1 + "\n";
	}
	if (true == elDocs)
		return true;
	else if ((false == atLeastOne) && errors =="")
	{
	    $("#dialog").dialog("open");
        $("#dialogContent").html(message2);
		return false;
	}
	else if (errors!="") 
		{
	    $("#dialog").dialog("open");
        $("#dialogContent").html(errors);
			return false;
		}
		else return true;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}



function validate_AIform(form) {

	repl = /\*/g;
	errors="";

/*	if(form.id == "sourceSearch"){
		title = trim(form.title.value).replace(repl, '');	
		isbn = trim(form.isbn.value);
		issn = trim(form.issn.value);
		
		if ((title=="")&&(isbn=="")&&(issn==""))
		{
			alert(messageAI2);
			return false;
		}
		if ((title.length > 0)) 
			errors += messageAI1+"\n";
			
		if ((isbn.length > 0))
			errors += messageAI3+"\n";
			
		if ((issn.length > 0))
			errors += messageAI4+"\n";
			
		if (errors!="")
		{
			alert(errors);
			return false;
		}
		else
			return true;
	}
	
	else{
	
		entry = trim(form.title.value).replace(repl, '');
		
		
	
		if (entry=="") {
			alert(messageAI2);
			return false;
		}
	
		if (entry.length > 0) {
			errors += messageAI1+"\n";
		}
			
		if (errors!="") {
			alert(errors);
			return false;		
		}
	
		return true;
	}*/
	var pageDefinition = document.getElementById("hidPageDefinition").value;
	if(form.id != "sourceSearch" && pageDefinition != "aiSearchNews")
	{
		entry = trim(form.title.value);
		if (entry=="") 
		{
		    $("#dialog").dialog("open");
	        $("#dialogContent").html(message2);
			return false;
		}
		else return true;
	}
	
	if(pageDefinition == "aiSearchNews") 
	{
		var dateFrom = trim(form.dateFrom.value);
		var dateTo = trim(form.dateTo.value);
		if (dateFrom=="" && dateTo=="") {
		    $("#dialog").dialog("open");
	        $("#dialogContent").html(message3);
			return false;
		}
		else return true;
	}

}

/** 
 * Formos valymas
 */
function clearAllFields(root, location)  {

    // isvalo visus laukus isskyrus hidden, nes kai kurie hidden reikalingi
    $(':input', '#' + location)
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
    
    $('#langList').attr('selectedIndex', -1);
    $('#publCountryList').attr('selectedIndex', -1);


    $(":input[id^='idsDidFind']", '#' + location).val('');
    $(":input[id^='textsToFind']", '#' + location).val('');
    
    // straipsniu paieskos
    $(":input[id='source_id']", '#' + location).val('');
    $(":input[id='keyword_id']", '#' + location).val('');
    $(":input[id='sourceId']", '#' + location).val('');
    
	clearSession(root, location);
}

/** 
 * Sesijoje isvalomos reiksmes
 */
function clearSession(root, pageDefinition) {
	$.ajax({
	    type: "POST",
	    url: root + "/clearFieldsAction.do",
	    data: "pageDefinition=" + pageDefinition
	});
}

function clearAIFields(root, location, params)  
{
	document.getElementById("title").value = "";
}
