import java.lang.*;
import java.io.*;
import java.util.*;

//all "Add" prefix functions write code on output writer
//all "gen" prefix functions returns String with required code
class JScriptGen
{

	 JScriptGen()
	 {}


	 //Adds Script Header
	 public void  addJScriptHeader(PrintWriter out)
	 {
		  out.println("<SCRIPT LANGUAGE=JavaScript>\n");		  
	 }

	 //adds JScript footer
	 public void addJScriptFooter(PrintWriter out)
	 {		  
		  out.println("</script>\n");		  
	 }
	 

	 //Creates and adds required code for valedate() function
	 //It uses other "gen" and "add" functions for generating and writing validate() fn.
	 public void addValidateFn(PrintWriter out, Vector fname, Vector ftype)
	 {
		  out.println("function validate(){\n");
		  
		  addNullFieldChecker(out, fname,ftype);
		  addIntValueChecker(out, fname, ftype);
		  addDateCheck(out,"CLOSINGDATE", "STARTDATE" );
		  
		  out.println("document.forms[0].submit()");
		  out.println("return true;\n");
		  out.println("\n}\n");
		  out.println(genIntCheckFunct());		
		  out.println(genDateCheckerFn());
		  out.println("\n");
	 }

	 
	 //Adds the code required for checking nullity of the fields specified  in the vector.
	 public void addNullFieldChecker(PrintWriter out, Vector fieldName, Vector ftype)
	 {
		  String check = "";
		  int n = fieldName.size();
		  String fn =""; 

		  for (int i=0; i<n; i++)
		  {
			   if(!((String)ftype.elementAt(i)).equalsIgnoreCase("date"))
			   {
					fn = (String)fieldName.elementAt(i);
					check += "if(document.forms[0]."+ fn  + ".value==\"\") \n";
					check += "{ alert('Please enter " + fn  + ".');\n" ; 
					check +=  "document.forms[0]." + fn + ".focus();\n" ;
					check +=	 "return false; \n }\n " ;
			   }
		  }
		  System.out.println(check);
		  out.println(check);		  
	 }


	 // Generates the function which takes one argument and alerts if the arg is not an integer.
	 // This function returns the String contaning that function
	 public String genIntCheckFunct()
	 {
		  String fn = "";
		  fn += "function isValidInteger(num, msg){ \n";
		  fn += " if(isNaN(parseInt(num)))\n ";
		  fn += "{ alert('Please Enter Correct Number For ' + msg );\n" ;
		  fn += "return false;\n }\n";
		  fn += "}\n";
		  return fn;
	 }


	 //Adds the code required for checking all the integer fields in the vector for integer value I/P.
	 public void addIntValueChecker(PrintWriter out, Vector fieldName, Vector fieldType)
	 {
		  String check = "";
		  int n = fieldName.size();
		  String fn =""; 
		  
		  for (int i=0; i<n; i++)
		  {
			   if (((String)fieldType.elementAt(i)).equalsIgnoreCase("int") ||  ((String)fieldType.elementAt(i)).equalsIgnoreCase("number") ||  ((String)fieldType.elementAt(i)).equalsIgnoreCase("integer"))
			   {	
					fn = (String)fieldName.elementAt(i);
					check += "n = document.forms[0]."+ fn  + ".value;\n";
					check += "if(isValidInteger(n,\"" + fn + "\") == false) \n";
					check += "{\nreturn false;\n}\n ";
			   }
		  }
		  System.out.println(check);
		  out.println(check);
	 }

	 public void addDateCheck(PrintWriter out, String field1,String field2)
	 {
		  String gd="document.forms[0]."+field1+"day.options[document.forms[0]."+field1+"day.selectedIndex].value";
		  String gm="document.forms[0]."+field1+"month.options[document.forms[0]."+field1+"month.selectedIndex].value";
		  String gy="document.forms[0]."+field1+"year.options[document.forms[0]."+field1+"year.selectedIndex].value";
		  String sd="document.forms[0]."+field2+"day.options[document.forms[0]."+field2+"day.selectedIndex].value";
		  String sm="document.forms[0]."+field2+"month.options[document.forms[0]."+field2+"month.selectedIndex].value";
		  String sy="document.forms[0]."+field2+"year.options[document.forms[0]."+field2+"year.selectedIndex].value";

		  String toWrite = "if(dateComp(parseInt("+gd+"), parseInt("+gm+"),parseInt("+gy+"),parseInt("+sd+"),parseInt("+sm+"),parseInt("+sy+")) == false)\n";
		  toWrite += "{alert('"+ field1 + " is earlier than " + field2 + "'); return false;}\n";
		  out.println(toWrite);
	 }

	 public String genDateCheckerFn()
	 {
		  String s = "";
		  s += "function dateComp(day1, mon1, yr1, day2, mon2, yr2)\n";
		  s += "{\n";
		  s += "if(yr1 < yr2)\n";
		  s += "{\n";
		  s += "     return false;\n";
		  s += "}\n";
		  s += "else if(yr1 > yr2)\n";
		  s += "{\n";
		  s += "     return true;\n";
		  s += "}\n";
		  s += "else \n";
		  s += "{\n";
		  s += "     if(mon1 < mon2)\n";
		  s += "     {\n";
		  s += "  		return false;\n";
		  s += "     }\n";
		  s += "     else if(mon1 > mon2)\n";
		  s += "     {\n";
		  s += "  		return true;\n";
		  s += "     }\n";
		  s += "     else\n";
		  s += "     {\n";
		  s += "  		if(day1 > day2)\n";
		  s += "  		{\n";
		  s += "  			 return true;\n";
		  s += "  		}\n";
		  s += "  		else \n";
		  s += "  		{\n";
		  s += "  			 return false;\n";
		  s += "  		}\n";
		  s += "     }\n";
		  s += "}\n}\n";


		  /*					  s += "	if(yr1 < yr2)\n{\nreturn false;\n}\n";
					  s += "else if(mon1 < mon2)\n{\nreturn false;\n}\n";
					  s += "else if(day1 < day2)\n{\nreturn false;\n}\n}\n";
		   */

		  return s;
	 }

}

