var _errorNumber = 0;

function trapError() {
	//alert(errorNumber);
	
	if (errorNumber > 0) {
		return false;
	}
	//return true;
}



function openwin(winurl)
{
	if(winurl != '')
	{
		//mywindow = window.open('', 'popup', 'width=400,height=350,top=250,left=350,resizable=yes,scrollbars=yes,menubar=yes');
		mywindow = window.open('', '_blank', 'width=520,height=400, top=100, left=200, resizable=yes, scrollbars=yes,menubar=yes');
		mywindow.location.href= winurl;
		mywindow.focus();
	}
}



function openSection(sectionName,servletctx) {

    if (sectionName == "a") {
  	   window.location.href = servletctx + "/kb/kbsearch.jsp";
    }

    if (sectionName == "b") {
    	window.location.href = servletctx + "/kb/search_pg.jsp";
    }

}//end of openSection


function validDate(sd,id){
	var today = new Date();
    var month = today.getMonth() + 1;
    var day   = today.getDate();
    var year  = today.getFullYear();
    var s     = ".";
    var calDateFormat = "";
	month = makeTwoDigit(month);
    day = makeTwoDigit(day);
    calDateFormat = month + s + day + s + year;
	showErrorMsg("date is invalid ","Y");


showElement('error');
document.sln_frm.action= '' ;
validateKBInput(sd,id);
//alert("document.sln_frm.action--"+document.sln_frm.action);
return false;

}
function getFormatDate(value,bsgDateFormat)
{	
	
    var fout ='';
    var dYear, dMonth, dDay;
	if(value!='')
	{
		switch (bsgDateFormat)
		{
			case "local":
				fout = new Date(value);
				break;
			case "MM/dd/yyyy":
				dMonth = value.substring(0,2);
				dDay = value.substring(3,5);
				dYear = value.substring(6,10);
				break;
			case "dd.MM.yyyy":
				dDay = value.substring(0,2);
				dMonth = value.substring(3,5);
				dYear = value.substring(6,10);
				break;
			case "MM.dd.yy":
				dMonth = value.substring(0,2);
				dDay = value.substring(3,5);
				dYear = value.substring(6,8);
				if (dYear > 20)
					dYear = 19 + dYear;
				else
					dYear = 20 + dYear;
				break;
			case "yyyy.dd.MM":
				dYear = value.substring(0,4);
				dDay = value.substring(5,7);
				dMonth = value.substring(8,10);
				break;
			case "yyyy.MM.dd":
				dYear = value.substring(0,4);
				dMonth = value.substring(5,7);
				dDay = value.substring(8,10);
				break;
		}
		fout = dMonth+"/"+dDay+"/"+dYear;
	}	
    return fout;
}

function validateKBInput(startDate,endDate,serverDate,bsgDateFormat,action){
    

	var fm= document.sln_frm; 
	//document.sln_frm.enddate1.value=endDate;
	
    var release = fm.release.value;
   
    var today = new Date();
    var month = today.getMonth() + 1;
    var day   = today.getDate();
    var year  = today.getFullYear();
    var s     = "/";
    var calDateFormat = "";
	var dateFormatMsg= "Date format Incorrect. Please type in " + bsgDateFormat +" format.";
	var dateInvalidMsg= "Date range Invalid.";
	var dateCompare="Start Date cannot be greater than end date.";
    var serverDateCompare="Search Results cannot be greater than "+serverDate;
	
    // Return Two Digits for Month and Day
    month = makeTwoDigit(month);
    day = makeTwoDigit(day);
    calDateFormat = month + s + day + s + year;
	//showErrorMsg("date is invalid ","Y");
    
	if(startDate != '')
	   if (!validatedate(startDate,bsgDateFormat)){
           fm.startDate.focus();
		   showErrorMsg(dateFormatMsg,'Y');	
		   return false;
        }
     
     if (!validatedate(endDate,bsgDateFormat)){
           fm.endDate.focus();
		   showErrorMsg(dateFormatMsg,'Y');	
           return false;
        }
	
    startDate=	getFormatDate(startDate,bsgDateFormat);
	endDate=	getFormatDate(endDate,bsgDateFormat);
	serverDate= getFormatDate(serverDate,bsgDateFormat);
   
   
	 if (!comparedate(startDate,endDate)){
           fm.startDate.focus();
           showErrorMsg(dateCompare,'Y');	
		   return false;
        }
        
      if (!comparedate(endDate,serverDate)){
           
           fm.endDate.focus();
           showErrorMsg(dateInvalidMsg,'Y');	
		   return false;
        }
     //commented by kirma04     
	/*var sdate = endDate.split("/");	
	 if (Math.abs(sdate[2]) > sYear)
	   {
		  showErrorMsg(dateInvalidMsg,'Y');	
	      fm.endDate.focus();
	      return false;
	   }
	   if ((Math.abs(sdate[2]) == sYear) && (Math.abs(sdate[0]) > sMonth))
	   {
		  showErrorMsg(dateInvalidMsg,'Y');	
	      fm.endDate.focus();
	      return false;
	   }
	   if ((Math.abs(sdate[2]) == sYear) && (Math.abs(sdate[0]) == sMonth) && (Math.abs(sdate[1]) > sDay))
	   {
		  showErrorMsg(dateInvalidMsg,'Y');	
	      fm.endDate.focus();
	      return false;
	   }*/
      //commented by kirma04 
/*
    // If the month(s) option has been chosen for the Last Modified Date criteria, check if a number has
     // been selected.
     if (fm.lastModified[0].checked == true) {
        if (fm.months.selectedIndex <= 0) {
          alert("You need to select the number of months");
          fm.months.focus();
          return false;
        }
     }

     // If the days(s) option has been chosen for the Last Modified Date criteria, check if a number has
     // been selected.
     if (fm.lastModified[1].checked == true) {
        if (fm.days.selectedIndex <= 0) {
          alert("You need to select the number of days");
          fm.days.focus();
          return false;
        }
     }

     // If the date range option has been chosen for the Last Modified Date criteria, 
     // - default start date to three months prior if not entered
     // - default the end date to current date if not entered
     if (fm.lastModified[2].checked == true){

        // Validate the Start Date
        if (!validatedate(fm.startDate.value)){
           fm.startDate.focus();
           return false;
        }
             

        if (fm.startDate.value != "") {
           if (fm.endDate.value == "") {
              fm.endDate.value = calDateFormat;
           }
        }
        
        
        if (document.sln_frm.ip_text != null)
        document.sln_frm.ip_text=document.sln_frm.ip_text;
	 

        // Validate the End Date
        if (!validatedate(fm.endDate.value)){
           fm.endDate.focus();
           return false;
        }

        // Check if the Start Date > End Date
        if (!comparedate(fm.startDate.value,fm.endDate.value)){
           fm.startDate.focus();
           return false;
        }

        //Check if End Date > Current Date
	   var sdate = fm.endDate.value.split("/");
	   if (sdate.length != 3) sdate = fm.endDate.value.split("-");
	   if (sdate.length != 3) 
	   {
	   	   alert("Invalid date format");
	   	   fm.endDate.focus();
		   return false;
	   }
	   if (Math.abs(sdate[2]) > year)
	   {
	      alert("End date cannot be greater than today's date");
	      fm.endDate.focus();
	      return false;
	   }
	   if ((Math.abs(sdate[2]) == year) && (Math.abs(sdate[0]) > month))
	   {
	      alert("End date cannot be greater than today's date");
	      fm.endDate.focus();
	      return false;
	   }
	   if ((Math.abs(sdate[2]) == year) && (Math.abs(sdate[0]) == month) && (Math.abs(sdate[1]) > day))
	   {
	      alert("End date cannot be greater than today's date");
	      fm.endDate.focus();
	      return false;
	   }
     }
     
     if (fm.lastModified(3).checked){
     	//alert("clear values");
     	fm.startDate.value="";
     	fm.endDate.value="";
     }  */    		 
	 document.sln_frm.action=action;
	 
    return true; 
}


// Ensure the value is two digits in length
function makeTwoDigit(inValue) {

    var numVal = parseInt(inValue, 10);
    // Value is less than 10 digits in length
    if (numVal < 10) {
        // add a leading zero to the value
        return("0" + numVal);
    }
    else {
        return numVal;
    }
}


function validatedate(value,bsgDateFormat) {
    var i = 0;	
    var datein = value;
	var bsgforamt =bsgDateFormat.split(".");
	var s=".";
	if(bsgforamt.length != 3)
	{
		s="/";
	}
    var sdate = datein.split(s);
	//alert(s);
	var flag =true;
    if(sdate.length != 3)
	  return false;
    
	if(value.length != bsgDateFormat.length){
      return false;
    }

	switch (bsgDateFormat)
	{
		case "local":
			fout = new Date(value);
			break;
		case "MM/dd/yyyy":
			flag=checkTwodigit(sdate[0]);
			flag=checkTwodigit(sdate[1]);
			flag=checkFourdigit(sdate[2]);
			break;
		case "dd.MM.yyyy":
			flag=checkTwodigit(sdate[0]);
			flag=checkTwodigit(sdate[1]);
			flag=checkFourdigit(sdate[2]);
		break;
		case "MM.dd.yy":
			flag=checkTwodigit(sdate[0]);
			flag=checkTwodigit(sdate[1]);
			flag=checkTwodigit(sdate[2]);
			break;
		case "yyyy.dd.MM":
			flag=checkFourdigit(sdate[0]);
			flag=checkTwodigit(sdate[1]);
			flag=checkTwodigit(sdate[2]);
			break;
		case "yyyy.MM.dd":
			flag=checkFourdigit(sdate[0]);
			flag=checkTwodigit(sdate[1]);
			flag=checkTwodigit(sdate[2]);
			break;
	}
    

    

   /* var chkDate=new Date(Date.parse(datein));
    var cmpDate=(chkDate.getMonth()+1)+"."+(chkDate.getDate())+"."+(chkDate.getFullYear());
    var indate2=(Math.abs(sdate[0]))+"."+(Math.abs(sdate[1]))+"."+(Math.abs(sdate[2]));

    if (indate2!=cmpDate){
      alert("You've entered an invalid date or date format.  Please use the MM/DD/YYYY format.");
      return false;
    }
    else {
      if (cmpDate=="NaN/NaN/NaN"){
          alert("You've entered an invalid date or date format.  Please use the MM/DD/YYYY format.");
	  return false;
      }
    }*/

    return flag;
}
function checkTwodigit(sdate){
	  if(sdate.length != 2) {
		  return false;     
    }
	return true;
}    
function checkFourdigit(sdate){
	  if(sdate.length != 4){
	  return false;
       }
	return true;
}

function comparedate(startDate,endDate){
    var sDate= startDate.split("/");
    var eDate= endDate.split("/");
    
    if(sDate[2] > eDate[2]){        
        return false;
    }
	if((sDate[2] == eDate[2])&& (sDate[0] > eDate[0])){
        return false;
    }
	if((sDate[2] == eDate[2])&& (sDate[0] == eDate[0]) && (sDate[1] > eDate[1])){
        return false;
    }
   
    return true;
}

function isLeapYear(year) {

    if ( ((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0) )
       return true;
    else
       return false;

}

function cleanSelect() {
    var fm = document.sln_frm; 
    var y2 = fm.startDate;
    var y3 = fm.endDate;   
}

