var b, v, browser1;
var minYear =1950;


// Determining if the browser supports window.open method
b = navigator.appName;
v = parseInt(navigator.appVersion);
browser1 = (b == "Netscape" && v >= 3 || b == "Microsoft Internet Explorer" && v >= 4);



//submits appropriate toolbar form
function submitToolbarForm(){

	if(document.toolbar.queryText.value.length ==0){
		alert(textField);
	}

	else{
			action= verityURL + "/" + verityCGI;
			document.toolbar.action= action ;
			document.toolbar.method= "post" ;
			document.toolbar.submit();
	}	
}


function englishOnly(){
	alert("The site is currenly available only in English");
}

function OpenWindow(tgt) 
{
     if (browser1) 
	 	{
                  newWin = window.open("", tgt, "width=640,height=450,toolbar=yes,menubar=yes,location=yes,directories=no,status=yes,resizable=yes,scrollbars=yes,copyhistory=yes");
                  newWin.focus();
        }
}

function OpenConsoleWindow(tgt) 
{
     if (browser1) 
	 	{
                  newWin = window.open("", tgt, "width=600,height=280,toolbar=no,menubar=no,location=no,directories=no,status=no,resizable=yes,scrollbars=yes,copyhistory=yes");
                  newWin.focus();
        }
}

function OpenTipWindow(tgt) 
{
     if (browser1) 
	 {
                  newWin = window.open("", tgt, "width=320,height=225,toolbar=no,menubar=no,location=no,directories=no,status=no,resizable=no,scrollbars=yes,copyhistory=yes");
                  newWin.focus();
     }
}


//submits  a form with a given index (if many forms in a page)
function setAction(formIndex)
{
	var submitOK = createRequest(formIndex);
	if(submitOK){
  	    document.forms[formIndex].submit();
	}
	else{
		alert(mandatory_field);
	}	
}


//creates the action for a form with a given index (index required if many forms in a page)
function createRequest(formIndex) 
{
        var re_action= guichetURL + "/" + guichetCGI;
	var mandatoryOK;
	
	mandatoryOK = validateMandatory(formIndex);

 	if(document.forms[formIndex].search.value == "Query" && mandatoryOK ){
		dates2guichetformat(formIndex);	
		//text2title(formIndex); //remove comment if want to search in title as well
	}

	if(mandatoryOK){
	    document.forms[formIndex].lg.value = document.forms["global"].lg.options[document.forms["global"].lg.selectedIndex].value;
	    document.forms[formIndex].nb_docs.value = document.forms["global"].nb_docs.options[document.forms["global"].nb_docs.selectedIndex].value;
	    document.forms[formIndex].action= re_action ;
	    document.forms[formIndex].method= "post" ;
	}
	return (mandatoryOK);
}

//validates if all mandatory fields are entered
//parameters that are pre-selected or hidden are not checked - for efficiency
function validateMandatory(formIndex)
{
	if(document.forms[formIndex].search.value == "Query"){
 	      //validate party but do not display alert - this is done by text field
		validateLength(document.forms[formIndex],document.forms[formIndex].party.value,document.forms[formIndex].party.name,0); 
		validateLength(document.forms[formIndex],document.forms[formIndex].text.value,document.forms[formIndex].text.name,1); 
		validateDate(document.forms[formIndex],document.forms[formIndex].to.value,document.forms[formIndex].to.name); 
		validateDate(document.forms[formIndex],document.forms[formIndex].from.value,document.forms[formIndex].from.name); 
		if( ((document.forms[formIndex].text.value || document.forms[formIndex].title.value) || (document.forms[formIndex].party.value))  ){
			return(true);
		}
	}
	if(document.forms[formIndex].search.value == "RefPub"){
		if(document.forms[formIndex].coll.value == "RC"){
  		    validateYear(document.forms[formIndex],document.forms[formIndex].year.value,document.forms[formIndex].year.name,1); 
  		    validateNumeric(document.forms[formIndex],document.forms[formIndex].page.value,document.forms[formIndex].page.name,1); 
			if(document.forms[formIndex].year.value){
				return(true);
			}
		}
		else{
  		    validateNumeric(document.forms[formIndex],document.forms[formIndex].nu_jo.value,document.forms[formIndex].nu_jo.name,1) ;
  		    validateYear(document.forms[formIndex],document.forms[formIndex].year.value,document.forms[formIndex].year.name,1); 
  		    validateMonth(document.forms[formIndex],document.forms[formIndex].month.value,document.forms[formIndex].month.name,1); 
  		    validateDay(document.forms[formIndex],document.forms[formIndex].day.value,document.forms[formIndex].day.name,1); 
  		    validateNumeric(document.forms[formIndex],document.forms[formIndex].page.value,document.forms[formIndex].page.name,1); 
			if(document.forms[formIndex].year.value && (document.forms[formIndex].nu_jo.value || document.forms[formIndex].month.value) ){
				return(true);
			}
		}
	}
	if(document.forms[formIndex].search.value == "DocNumber"){
		validateYear(document.forms[formIndex],document.forms[formIndex].an_doc.value,document.forms[formIndex].an_doc.name,1); 
		validateNumeric(document.forms[formIndex],document.forms[formIndex].nu_doc.value,document.forms[formIndex].nu_doc.name,1); 
		if(document.forms[formIndex].an_doc.value && document.forms[formIndex].nu_doc.value){
			return(true);
		}
	}

	return (false);
}

//reset a given form
function resetForm(formIndex)
{
	document.forms[formIndex].reset();	
}

//resets all forms of a page
function resetAllForms(){
	var formNo = document.forms.length;
	var i;
	for(i=0; i<formNo;i++){
		document.forms[i].reset();	
	}
}


//copies text to title for search by word
function text2title(formIndex)
{
	document.forms[formIndex].title.value = document.forms[formIndex].text.value;
}


//selectDomain - for by word and by number form in Home
function selectDomain(formIndex){
	for(var i=0; i < document.forms[formIndex].dom.length; i++){
		if(document.forms[formIndex].dom[i].checked == true){
			if(document.forms[formIndex].dom[i].value == "all"){
				document.forms[formIndex].domain.value= "";
				document.forms[formIndex].coll.value="";
			}		
			else{
				if(document.forms[formIndex].dom[i].value == "JO"){
					document.forms[formIndex].domain.value="";
					document.forms[formIndex].coll.value="JO";
				}		
				else{
					document.forms[formIndex].domain.value=document.forms[formIndex].dom[i].value;
					document.forms[formIndex].coll.value="";
				}
			}		
		}
 	 }
}


//pass dates from and to in corresponding parameters required for Guichet
function dates2guichetformat(formIndex)
{
	if(document.forms[formIndex].from.value){
		var from = document.forms[formIndex].from.value;
		document.forms[formIndex].day_from.value = from.substring(0, from.indexOf("/")) ;
		from = from.substring(from.indexOf("/") + 1, from.length); 
		document.forms[formIndex].month_from.value = from.substring(0, from.indexOf("/")) ;
		document.forms[formIndex].year_from.value = from.substring(from.indexOf("/") + 1, from.length); 
	}
	if(document.forms[formIndex].to.value){
		var to = document.forms[formIndex].to.value;
		document.forms[formIndex].day_to.value = to.substring(0, to.indexOf("/")) ;
		to = to.substring(to.indexOf("/") + 1, to.length); 
		document.forms[formIndex].month_to.value = to.substring(0, to.indexOf("/")) ;
		document.forms[formIndex].year_to.value = to.substring(to.indexOf("/") + 1, to.length); 
	}
}


//validates if value has length >= 3, can have alert ON or OFF
function validateLength(formName, textValue, textName, alertOn)
{
	if(textValue.length==0){
    	return (true);
	}
	if(textValue.length == 2 || textValue.length == 1){
		formName[textName].value= "";
		if(alertOn){
			alert(text_length);
		}
   	    return (false);
	}
 	return (true);

}

//validates if value if numeric - can have alert ON or OFF
function validateNumeric(formName,value, name, alertOn)
{
	var RefString="1234567890";
	if(value.length==0){
    	return (true);
	}
	if(value ==0){
		formName[name].value = "";
		return(true);
	}
	for (Count=0; Count < value.length; Count++)  {
    	var TempChar= value.substring (Count, Count+1);
        if (RefString.indexOf(TempChar, 0)==-1){  
			formName[name].value = "";
			if(alertOn){
				alert(numeric_field);
			}
			return (false);
        }
		else{
			formName[name].value = parseInt(value,10);
		}
	}
  	return (true);
}

//validates month to be 1 - 12
function validateMonth(formName, monthValue, monthName, alertOn)
{
	if(monthValue.length==0){
    	return (true);
	}

	var correct = validateNumeric(formName,monthValue,monthName,1);
	if(correct){
		if(parseInt(monthValue,10) < 1 || parseInt(monthValue,10) > 12){
			formName[monthName].value = "";
			if(alertOn){
				alert(month_field);
			}
			correct =false;
		}
		else{
			formName[monthName].value = parseInt(monthValue,10);
		}
  	}
	return (correct);
}

//validates month to be 1 - 12
function validateDay(formName, dayValue, dayName, alertOn)
{
	if(dayValue.length==0){
    	return (true);
	}

	var correct = validateNumeric(formName,dayValue,dayName,1);
	if(correct){
		if(parseInt(dayValue,10) < 1 || parseInt(dayValue,10) > 31){
			formName[dayName].value = "";
			if(alertOn){
				alert(day_field);
			}
			correct =false;
		}
		else{
			formName[dayName].value = parseInt(dayValue,10);
		}
  	}
	return (correct);
}


//validates if year is correct (must be numeric,>1950,<todays year)
function validateYear(formName,yearValue, yearName, alertOn)
{
	today = new Date();
	thisYear = today.getFullYear();
	if(yearValue.length==0){
    	return (true);
	}
	var correct = validateNumeric(formName,yearValue,yearName,1);
	if(correct){
		if(yearValue.length !=4 || yearValue < minYear || yearValue > thisYear){
			formName[yearName].value = "";
			if(alertOn){
				alert(incorrect_year);
			}
			correct =false;
		}		
	}
  	return (correct);
}


//validates date from and to fields 
function validateDate(formName, datevalue, datename)
{ 
	if(datevalue.length ==0 || datevalue.length == ""){
	   return(true);
	}
	var months ;
	var days ;
	var years ;
	days = datevalue.substring(0, datevalue.indexOf("/")) ;
	datevalue = datevalue.substring(datevalue.indexOf("/") + 1, datevalue.length); 
	months = datevalue.substring(0, datevalue.indexOf("/")) ;
	years = datevalue.substring(datevalue.indexOf("/") + 1, datevalue.length); 

	var helpyear = parseInt(years,10);
    var helptext;
	if (helpyear < 50 ) {
		helptext = 2000 + helpyear ;
    }
	else
	{
		if ( (helpyear >= 50 ) && (helpyear < 100) ){
			helptext = 1900 + helpyear ;
		}
		else
			helptext = years ;
	}	
	years = helptext.toString();

	if (dateOK(formName,datename,days, months, years)) {
		formName[datename].value = parseInt(days,10) + "/" + parseInt(months,10) + "/" + parseInt(helptext,10) ;	
		if(FromLtTo(formName)){
			return true;
		}
		else{
//			formName["from"].value = "";
			formName["to"].value = "";
			alert(fromLtTo);
			return false;
		}
	}
	else{
		formName[datename].value = "";
		alert(incorrect_format);
		return (false);
	}
}

function dateOK(formName,datename,days, months, years)
{
	today = new Date();
	thisYear = today.getFullYear();

   	if ( days.length ==0 || months.length ==0 || years.length ==0)    
		return false;	
   	if ( !validateNumeric(formName,days,datename,0) )       
		return false;	
  	if ( !validateNumeric(formName,months,datename,0) )       
        return false;       
   	if ( !validateNumeric(formName,years,datename,0) )       
        return false;

   	var eDays = parseInt(days,10);      	   
   	if ( eDays > lastDayOfMonth(years, months) || eDays < 1) 
		return false; 
   	var eMonth = parseInt(months,10);
	if ( (eMonth < 1) || (12 < eMonth) )
		return false;
	var eYear = parseInt(years,10);
//	if( years.length != 4){
	if( !validateYear(formName,years,datename,0)){
		return false;	
	}

	return true;
}
function lastDayOfMonth(nYear, nMonth)
{
  if (nMonth==2)
    return (nYear%4!=0)?28:(nYear%100!=0)?29:(nYear%400!=0)?28:29;
  else if (nMonth<=7)
    return (nMonth%2==0)?30:31;
  else
    return (nMonth%2==0)?31:30;
}


//checks if from field is less than to field
function FromLtTo(formName){
	var to, year_to, month_to, day_to;
	var from, year_from, month_from, day_from;
	
	if(formName["from"].value && formName["to"].value ){
		from = formName["from"].value;
		day_from = parseInt(from.substring(0, from.indexOf("/")),10) ;
		from = from.substring(from.indexOf("/") + 1, from.length); 
		month_from = parseInt(from.substring(0, from.indexOf("/")),10) ;
		year_from = parseInt(from.substring(from.indexOf("/") + 1, from.length),10); 
		to = formName["to"].value;
		day_to = parseInt(to.substring(0, to.indexOf("/")),10) ;
		to = to.substring(to.indexOf("/") + 1, to.length); 
		month_to = parseInt(to.substring(0, to.indexOf("/")),10) ;
		year_to = parseInt(to.substring(to.indexOf("/") + 1, to.length),10); 
		if(year_to < year_from){
			return false;
		}
		if(year_to > year_from){
			return true;
		}
		if(year_to == year_from){
			if(month_to < month_from){
				return false;
			}
			if(month_to > month_from){
				return true;
			}
			if(month_to == month_from){
				if(day_to < day_from){
					return false;
				}
				if(day_to >= day_from){
					return true;
				}
			}
		}
	}
	else{
		return true;
	}	
}

