 // This js contains all the the javascript functions for
 // Advanced Search Widget 
 // author  - Bala
// JIRA # : SDSLA-1484,Author : Anuradha, Date: 24th Apr 2009,Add (YYYY/MM/DD)in the error msg popup
	// Declaring valid date character, minimum year and maximum year
//JIRA # : SDSLA-1422  Changed the html write statements to work in different domains.	
//JIRA # : ECMSEARCH-727  Changed the checkbox functionality.
	var dtCh= "/";
	var minYear=1900; 
	var maxYear=2100;
	var weekend = [0,6];
	var weekendColor = "#e0e0e0";
	var fontface = "Verdana";
	var fontsize = 2;
	var currentdomain = "" ;
	//Added By Raja Rajan. For ECMSEARCH-616 for checkbox field on 14-July-2008
	var flag_checkbox = 0;
	var calendar_Name="Calendar";
	
	var gNow = new Date();
	var ggWinCal;
	isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
	isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
	//Added by Rajkumar for providing Sort option and multilingual calendar on 14-09-06 JIRA:ECMSEARCH-300
	var sortParam ="sortDesc";
	var sortFieldValue="";
	var lang;
	
	//Author:Rajkumar Date:18-Oct-07 Change: Added method to encode the request string in the target url JIRA:ECMSEARCH-462.
	function utf8AdvSrch(wide) {
  	var c, s;
  	var enc = "";
  	var i = 0;
  	while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  	}
  	return enc;
	}
	var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
	function encodeURIComponentNewAdvSrch(s) {
 		var s = utf8AdvSrch(s);
  		var c;
 		var enc = "";
 		for (var i= 0; i<s.length; i++) {
  			if (okURIchars.indexOf(s.charAt(i))==-1)
   			enc += "%"+toHex(s.charCodeAt(i));
 			else
  			enc += s.charAt(i);
		}
	return enc;
	}
	//End by Rajkumar ECMSEARCH-462
	function isNull(elem){
		//	alert("I am from isNull.yes Field's value is "+elem.value);	
	}

    function isBlur(elem){
		//alert("I am from isBlur. Field's value is "+elem.value);
	}
	
	// This fucntion used to show the calender popup
	//Changed by Rajkumar for providing Sort option and multilingual calendar on 14-09-06 JIRA:ECMSEARCH-300	
	function callCalendar(objName,langcode)
	{
		lang=langcode;
		show_calendar('AdvancedWidgetSearchForm.'+objName); 
	}
	
	// This fuction used for trimming the unnecessary space
	// found in the field values

	function Trim(TRIM_VALUE) { 

		if(TRIM_VALUE != null && TRIM_VALUE.length < 1) {
			return"";
		}

		TRIM_VALUE = RTrim(TRIM_VALUE);
		TRIM_VALUE = LTrim(TRIM_VALUE);


		if(TRIM_VALUE=="") {
			return "";
		} else{
			return TRIM_VALUE;
		}
	} //End Function
		
	function RTrim(VALUE) {
		var w_space = String.fromCharCode(32);
		var v_length = VALUE.length;
		var strTemp = "";
		if(v_length !=null && v_length < 0) {
			return"";
		}
		var iTemp = v_length -1;

		while(iTemp > -1) {
			if(VALUE.charAt(iTemp) != w_space) {
				strTemp = VALUE.substring(0,iTemp +1);
				break;
			}
			iTemp = iTemp-1;
		} //End While
		return strTemp;	  
	} //End Function 
		
	function LTrim(VALUE){

		var w_space = String.fromCharCode(32);

		if(v_length < 1) {
			return"";
		}

		var v_length = VALUE.length;
		var strTemp = "";
		
		var iTemp = 0;
		
		while(iTemp < v_length) {

			if(VALUE.charAt(iTemp) != w_space) {

				strTemp = VALUE.substring(iTemp,v_length);
				break;

			}

			iTemp = iTemp + 1;

		} //End While

		return strTemp;
	} //End Function

	// This function used to check the given date is in integer or not
	
	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 stripCharsInBag(s, bag){
		var i;
	    var returnString = "";
	    // Search through string's characters one by one.
	    // If character is not in bag, append to returnString.
	    for (i = 0; i < s.length; i++){   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) returnString += c;
	    }
	    return returnString;
	}

	function daysInFebruary (year){
		// February has 29 days in any year evenly divisible by four,
	    // EXCEPT for centurial years which are not also divisible by 400.
	    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}

	function DaysArray(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31;
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
			if (i==2) {this[i] = 29;}
	   } 
	   return this;
	}
 
	// This function will check the format of the Date entered
	function isDate(dtStr){

		//	2006/04/05	   yyyy/mm/dd
	
		var daysInMonth = DaysArray(12);
		var pos1=dtStr.indexOf(dtCh);
		var pos2=dtStr.indexOf(dtCh,pos1+1);
		
		
		var strYear=dtStr.substring(0,pos1);
		var strMonth=dtStr.substring(pos1+1,pos2);
		var strDay=dtStr.substring(pos2+1);	
		
		//alert("strYear"+strYear);
		//alert("strMonth"+strMonth);
		//alert("strDay"+strDay);
		
		//alert("pos1"+pos1);
		//alert("pos2"+pos2);
		
		
		//alert("checking");
		
		//return false;
		
		strYr=strYear;										  	
	
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
		}
	
		month=parseInt(strMonth);
		day=parseInt(strDay);
		year=parseInt(strYr);
		
		if (pos1==-1 || pos2==-1){
		
			//SDSLA-1484 changes done here 	
			alert("Please enter a valid day (YYYY/MM/DD)");
		    //SDSLA-1484 changes ends here
			return false;
			
		}
		
	
		if (strMonth.length<1 || month<1 || month>12){
			alert("Please enter a valid month");
			return false;
		}
		
	
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//SDSLA-1484 changes done here 	
			alert("Please enter a valid day (YYYY/MM/DD)");
		//SDSLA-1484 changes ends here	
			return false;
		}
	
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
			return false;
		}
	
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			alert("Please enter a valid date");
			return false;
		}
	
		return true;
	}
	
	// This function will return comma seperated values of 
	// selection of the PICKLIST
	
	function getSelectedValues_comma(component){	
		
		//alert("component "+component); 
		var totalOptions=component.options.length;		
		var selectedValues="";
		for(i=0;i<totalOptions;i++){
			if(component.options[i].selected){			
				if(selectedValues!="")
					selectedValues+="," ;
				selectedValues+=component.options[i].value;				
			}
		}
		
		return selectedValues;  
	}	 
	
	// This function checks the atleast one values are selected
	// in the advanced Search Form and return true if it is selected
	// otherwise false
	    
	function isValueExist(){ 
	
		//alert("isValueExist");	

		var doclength = document.AdvancedWidgetSearchForm.elements.length;
		var count = 0;

		for(var i=0; i<doclength; i++){ // start for loop doclength	

			var elemValue = document.AdvancedWidgetSearchForm.elements[i].value;
			var elemType = document.AdvancedWidgetSearchForm.elements[i].type;		
			var elemName = document.AdvancedWidgetSearchForm.elements[i].name;

			elemValue = Trim(elemValue);  
			elemName = Trim(elemName);
			elemType = Trim(elemType);
			
			//alert("elemName	 =="+elemName);
			//alert("elemValue =="+elemValue);
			//alert("elemType2	 == "+elemType);     			

			
			if( elemType != 'select-one'  && 
				elemType != 'hidden' &&	
				elemType != 'select-multiple' &&			
				elemType != 'radio' &&
				// Added by Raja Rajan For ECMSEARCH-616 To provide checkbox in the Advanced Search Form on 14-July-2008
				elemType != 'checkbox' &&
				elemType != 'single' &&
				elemType == 'text' &&	
				elemValue != null &&
				elemValue.length > 0) { 				
				
				//alert("inside text only");

					// If elemType text and fromDate value is 'N/A' or toDate value is 'N/A'
					// no need to increase the count
								
					if((elemName == 'fromDate'&& elemValue == 'N/A')
						||(elemName == 'toDate'&& elemValue == 'N/A') ){						
						count = count;
					}else{
						count = count + 1;
					}
				
			}
			//Added by Rajkumar Changes: Added New ype field : JIRA :ECMSEARCH-529
			else if(elemType =='single' && elemValue !=null && elemValue.length >0){
				count = count + 1;
			}
			else if(elemType == 'select-multiple'){ 			
				
				// If elemType is 'select-multiple' we need to get the comma seperated values
				// if value is there we need to increase the count otherwise is not required
			
				var selectedValues=getSelectedValues_comma(eval("document.AdvancedWidgetSearchForm."+elemName));
				
				//alert(elemName+" selectedValues "+selectedValues);
				
				if(selectedValues !=null && Trim(selectedValues).length > 0){				
					count = count + 1;
				}				
			}else if(elemType == 'radio'){// for radio button added by bala for ECMS-283 start
					count = count;
			}//end
			// Added by Raja Rajan For ECMSEARCH-616 To provide checkbox in the Advanced Search Form on 14-July-2008
			else if(elemType == 'checkbox'){
				
					count = count+1;
			} // End of Add for ECMSEARCH-616
				
		} // End for loop doclength	
		
		//alert("count final "+count);	
	
		if(count  == 0){
			return false;
		}else{
			 return true;
		}
	}
	
	// This function validate the date options
	// and it will give message according to the 
	// value selected in the date option
	
	function checkDateOptions(){
		//Added by Uday for ECMSEARCH-356 on 16-March-2007
		//IF Condition is to support validation when docType(Date field) in not there in Search Form. 
		if(document.AdvancedWidgetSearchForm.docType !=null){
		
		var datetype = document.AdvancedWidgetSearchForm.docType.options[document.AdvancedWidgetSearchForm.docType.selectedIndex].value;	
		var frdt=document.AdvancedWidgetSearchForm.fromDate;	
		var todt= document.AdvancedWidgetSearchForm.toDate;	 		
		var frdtValue=frdt.value;																				 
		var todtValue=todt.value;																				 
		
		frdtValue=Trim(frdtValue);		   		
		todtValue=Trim(todtValue);		   		
		
		//alert("datetype "+datetype);
	
	
		if (datetype == 0) { //From/To		
			
			if(frdtValue.length == 0 && todtValue.length > 0 ){		
				alert('When selecting "From/To" to query between two dates, please enter both dates.'); 			
				frdt.focus();
				return false;
			}
			
			if(frdtValue.length > 0 && todtValue.length == 0 ){		
				alert('When selecting "From/To" to query between two dates, please enter both dates.'); 			
				todt.focus();
				return false;
			}
			
			if(frdtValue.length == 0 && todtValue.length == 0 ){					
				return true;
			}
					
			if(frdtValue.length > 0 && todtValue.length > 0 ){
			
				if (isDate(frdtValue)==false) {				
					frdt.focus()
					return false;
				}
				if (todtValue != 'N/A' && isDate(todtValue)==false) {
					todt.focus()
					return false;
				}
				
				if (compareDates(frdtValue,todtValue)==false) {
					alert("To Date Should Be Greater Or Equal To From Date");
					todt.focus();
					return false;
				}	
				
				return true;
			}		
	
		}else if(datetype==1) { // Before//from date field is disabled and to date has been enabled
		
		  //   alert("Before1");
	
			if(frdtValue != 'N/A' && frdtValue.length > 0 ){		
				alert('When selecting "Before" to query for all items before some date, please enter that date in the first date field (and no date in the second field).'); 			
				todt.focus();
				return false;
			}			
			
			if(todtValue.length == 0 && frdtValue == 'N/A'){
				return true;			
			}
			
			if(todtValue.length > 0 && frdtValue == 'N/A'){
			
				if (isDate(todtValue)==false) {				
					todt.focus()
					return false;
				}
				return true;
			}	
			
		}else if(datetype==2){ //Exact Date
	
			if(todtValue != 'N/A' && todtValue.length > 0 ){		
				alert('When selecting "Exact Date" to query for all items matching some date, please enter that date in the first date field (and no date in the second field).'); 			
				todt.focus();
				return false;
			}	
			
			if(frdtValue.length == 0 && todtValue == 'N/A'){
				return true;			
			}			
			
			if(frdtValue.length > 0 && todtValue == 'N/A'){
			
				if (isDate(frdtValue)==false) {				
					frdt.focus()
					return false;
				}				
				
				return true;
			}	
			
		}else if(datetype==4){// After
	
			if(todtValue != 'N/A' &&  todtValue.length > 0 ){		
				alert('When selecting "After" to query for all items after some date, please enter that date in the first date field (and no date in the second field'); 			
				todt.focus();
				return false;
			}
			
			if(frdtValue.length == 0 && todtValue == 'N/A'){
				return true;			
			}
				
			
			if(frdtValue.length > 0 && todtValue == 'N/A' ){
			
				if (isDate(frdtValue)==false) {				
					frdt.focus()
					return false;
				}
								
				return true;
			}	
			
		} else if(datetype==3) { //select	
					
			if(frdtValue.length > 0 ||todtValue.length > 0 ){		
				alert('When querying by date, please select the range to search over.  Select "From/To" to query between two dates.  Select "After", to query for all after some date.  Select "Before", to query for all before some date.  Or select "Exact Date" to query for all matching some date'); 			
				document.AdvancedWidgetSearchForm.docType.focus();
				return false;
			}		
			if(frdtValue.length == 0 && todtValue.length == 0 ){
				return true;
			}
					
		}
	}
	return true;
	}
	
	// This function will dynamic Parameters for
	// the target URL this can be overrided by 
	// each site according to their requirement
	
	function replaceAll(str, replacements ) {
    for ( i = 0; i < replacements.length; i++ ) {
        var idx = str.indexOf( replacements[i] );

        while ( idx > -1 ) {
            str = str.replace( replacements[0],replacements[1] ); 
            idx = str.indexOf( replacements[0] );
        }

   	 }

    return str;
	}
	
	function getDynamicParameters(){
	    //alert("alert getDynamicParameters");
	    //Added by Rajkumar on 22-july-2006 change: giving diffrent param name for major and child doc type
	    var majorTemp;
		var majDocTY="";
		var childTY="";
		var elemNameM="";
		var elemNameC="";
		var dAttsValue="";
		//end here
		var parameters = "";
		var parent_child="";
		var parentvalue="";
		var childvalue="";
		var parentname="";
		var childname="";
		var parentTemp="";
		
		var doclength = document.AdvancedWidgetSearchForm.elements.length;
		for(var i=0; i<doclength; i++){ // start for loop doclength				
		
			var elemType = document.AdvancedWidgetSearchForm.elements[i].type;		
			var elemName = document.AdvancedWidgetSearchForm.elements[i].name;
			
			//Added by Rajkumar on 22-july-2006 change: giving diffrent param name for major and child doc type
			if(elemName =='MajMinDocTY'){
				var iMajMinDocTY =document.AdvancedWidgetSearchForm.MajMinDocTY.options.length;	
				for(var j=0;j<iMajMinDocTY;j++)
					{
					if(document.AdvancedWidgetSearchForm.MajMinDocTY.options[j].selected)
						{
							majorTemp=document.AdvancedWidgetSearchForm.MajMinDocTY.options[j].value;		
								if(majorTemp.substring(0,1)=="*")					
								{	
								elemNameM="majorDocTY";					
									majDocTY=majDocTY+majorTemp.substring(1,majorTemp.length)+",";						
								}
								else
								{
								elemNameC="docTY";	
									childTY=childTY+majorTemp+",";					
								}
						}
					}
					majDocTY=majDocTY.substring(0,(majDocTY.length-1));
					childTY=childTY.substring(0,(childTY.length-1));
			}
			
			if(elemName =='parent_child'){
			
				parent_child = document.AdvancedWidgetSearchForm.parent_child.value;
				var parent = parent_child.split("_")[0];
				var child = parent_child.split("_")[1];
				var parentlist = document.getElementById(parent);
				var iparent = parentlist.options.length;	
			
				for(var j=0;j<iparent;j++)
					{
					if(parentlist.options[j].selected)
						{
							parentTemp=parentlist.options[j].value;		
								if(parentTemp.substring(0,1)=="*")					
								{	
									parentname=parent;					
									parentvalue=parentvalue+parentTemp.substring(1,parentTemp.length)+",";						
								}
								else
								{
									childname=child;	
									childvalue=childvalue+parentTemp+",";					
								}
						}
					}
					
					parentvalue=parentvalue.substring(0,(parentvalue.length-1));
					childvalue=childvalue.substring(0,(childvalue.length-1));
					
					parameters+="&"+parentname+"="+parentvalue;
					parameters+="&"+childname+"="+childvalue;
				}
			//End here			
			//alert("elem Type outer >>>>>>>` "+elemType);
			
			//alert("elemType "+elemType+"elemName "+elemName);			
		//	alert("elemValue "+eval("document.AdvancedWidgetSearchForm."+elemName).value);
											
			//elemName = Trim(elemName);							
			// Added by Raja Rajan For ECMSEARCH-616 To provide checkbox in the Advanced Search Form on 14-July-2008
			if(elemType != 'select-multiple' && elemType != 'radio'  && elemType != 'checkbox' && eval("document.AdvancedWidgetSearchForm."+elemName) != null){			
			

			
				//If todate has value is N/A then no need to append any parameters and leave as it it
				if(elemName=='toDate' && Trim(eval("document.AdvancedWidgetSearchForm."+elemName).value) == 'N/A'){
					parameters = parameters;
				//If fromdate has value is N/A then no need to append any parameters and leave as it it	
				}else if(elemName=='fromDate' && Trim(eval("document.AdvancedWidgetSearchForm."+elemName).value) == 'N/A'){
					parameters = parameters;
				}
				//Added by Rajkumar for providing Sort option on 14-09-06 JIRA:ECMSEARCH-300  
				else if(elemName =='sortField'){
				sortFieldValue=document.AdvancedWidgetSearchForm.sortField.options[document.AdvancedWidgetSearchForm.sortField.selectedIndex].value;
					parameters+="&"+sortParam+"="+document.AdvancedWidgetSearchForm.sortField.options[document.AdvancedWidgetSearchForm.sortField.selectedIndex].value;
				}
				else if(elemName =='dAtts'){
				dAttsValue=callDAtts(document.AdvancedWidgetSearchForm.dAtts.value);
				parameters+="&"+elemName+"="+dAttsValue;
				}
				else if(elemName =='parent_child'){
					parameters = parameters;
				}	else{
					//Author:Rajkumar Date:18-Oct-07 Change: Added method to encode the request string in the target url JIRA:ECMSEARCH-462.

					//Start added by Sivagnanavelu Date:19-May-08 Change: Remove the query parameter in the advanced search form target url if there is no value for query. Jira : ECMSEARCH-506 -->.
					
					if(elemName == 'query' )
					{
						var tquery=Trim(eval("document.AdvancedWidgetSearchForm."+elemName).value);
						if (tquery.length > 0)
						{
							var query = replaceAll(tquery,new Array("(","{"));
							query = replaceAll(query,new Array(")","}"));	
											
							parameters+="&"+elemName+"="+encodeURIComponentNewAdvSrch(query)
						}	
					}
					else
					{
					  parameters+="&"+elemName+"="+encodeURIComponentNewAdvSrch(Trim(eval("document.AdvancedWidgetSearchForm."+elemName).value))
					
					}
					
				 //End added by Sivagnanavelu Date:19-May-08 Change: Remove the query parameter in the advanced search form target url if there is no value for query. Jira : ECMSEARCH-506 -->.	
					
				}	
				
			}else if(elemType == 'radio'){//for Radio Group List added by Bala on 26-6-2006 start			
										 // JIRA ECMS-283					
					var elemObj = eval("document.AdvancedWidgetSearchForm."+elemName);
					for(var k = 0; k < elemObj.length; k++) {
						if(elemObj[k].checked) {	
							parameters+="&"+elemName+"="+elemObj[k].value;										
						}
					} 	
						
			}//end
			// Added by Raja Rajan For ECMSEARCH-616 To provide checkbox in the Advanced Search Form on 14-July-2008
			//Added By Raja Rajan. For ECMSEARCH-616 for checkbox field on 14-July-2008
			else if(elemType == 'checkbox'){			
					flag_checkbox=flag_checkbox+1;					 
					var elemObj = eval("document.AdvancedWidgetSearchForm."+elemName);
					var count=0;
					if(flag_checkbox==1)
					{
					 parameters+="&"+elemName+"=";										
					 for(var k = 0; k < elemObj.length; k++) {
						if(elemObj[k].checked) {	
							count=count+1;
							if(count>1)
							{
							 parameters+=" OR "+elemObj[k].value;
							}
							else
							{
							 parameters+=elemObj[k].value;
							}

						}
					 } 	
					}	
			}//end of Add for checkbox field
			else if(elemName =='MajMinDocTY'){
				parameters+="&"+elemNameM+"="+majDocTY;
				parameters+="&"+elemNameC+"="+childTY;
			}else if(elemName ==parentname){
						parameters = parameters;
			}else if(elemName =='TopicLabel'){
			parameters+="&"+elemName+"="+getSelectedValues_comma(eval("document.AdvancedWidgetSearchForm."+elemName));	
				if (getSelectedValues_comma(eval("document.AdvancedWidgetSearchForm."+elemName)) !=''){
					parameters+="&topic="+getSelectedValues_comma(eval("document.AdvancedWidgetSearchForm."+elemName));
				}
			}	
			else{
				parameters+="&"+elemName+"="+getSelectedValues_comma(eval("document.AdvancedWidgetSearchForm."+elemName));	
			}
		}// End of loop doclength		
		return parameters;
	}		
	
// This function reset all the fields of form 
// Added by uday for ECMSEARCH-428 on 07-May-07
 
function resetForm()
{
	document.AdvancedWidgetSearchForm.reset();

//Added as per the requirements of JIRA ECMSEARCH-683	

//Added as per the requirements of JIRA ECMSEARCH-691 by Avinash

	var inputElements = document.AdvancedWidgetSearchForm.getElementsByTagName("input");
			
		var checboxcount = 0;
		 for (var i = 0; i < inputElements.length; i++)
		  {	
				if (inputElements[i].type == "text" || inputElements[i].type == "TEXT")
						inputElements[i].value = "";
						
				if (inputElements[i].type == "checkbox" || inputElements[i].type == "CHECKBOX")
				{ 
					checboxcount++;
					if(checboxcount == 1)				
						inputElements[i].checked = true;
					else
						inputElements[i].checked = false;
				}
		  }

	var selectBox = document.AdvancedWidgetSearchForm.getElementsByTagName("select");
		
	for (var i = 0; i < selectBox.length; i++)
	  {

		var optionlist = selectBox[i].getElementsByTagName("option");
	
		var optioncount = 0;
		 for (var j = 0; j < optionlist.length; j++)
		  {	
				optioncount ++;
				
				if(optioncount == 1)				
					optionlist[j].selected = true;
				else
					optionlist[j].selected = false;
		  }

	}
//End of ECMSEARCH-691 Add
//End of ECMSEARCH-683 Add
}

//Added By Raja Rajan. For ECMSEARCH-616 for checkbox field to uncheck Default when any other type is selected. on 14-July-2008
function uncheck_defaultType(id,name)
{

   var chk=document.getElementsByName(name);
   var flag=0;
   var count=0;
   	 	
	for (i = 1; i < chk.length; i++)
	{	
	 	  if(chk[i].checked ==true)
	 	  {
	 	  	count++; 
	   	    flag=1;
	 	  }
	 	  
	}//end loop
	
	if(flag==1)
	{
	  if((count == chk.length - 1) && (chk[0].checked == false))	
	  {
	  	 chk[0].checked = true ;
		  for (j = 1; j < chk.length; j++)
		  {
		      chk[j].checked = false ;
		  }
	  }
	  else
	  	chk[0].checked = false ;
	}
	else
        chk[0].checked = true ;
        
        if(id=="1")
		{
		  chk[0].checked = true ;
		  for (k = 1; k < chk.length; k++)
		  {
		      chk[k].checked = false ;
		  }
        }
   
}// End of Add new function for ECMSEARCH-616.
	
	
// This function validate checkDateOptions and isValueExist
// if both return true it will submit the
// form using the dynamic built URL.



function validateAdvancedSearchForm(target){  
	var parameters= "";	
    //alert(" Inside validateAdvancedSearchForm() ");
   // alert(" target "+target);
	if(checkDateOptions() == true){
	
		if (isValueExist()==false) {
			alert("Please enter at least one value"); 
			if(document.AdvancedWidgetSearchForm.query != null){
				document.AdvancedWidgetSearchForm.query.focus();
			}	
			return false;
			
		}else{			
		  // alert(" calling getDyamic Parameters ");
		//If valueexist is true we need to build URL for the target page and submit the form
		// using the built URL
		
		
			//alert("parameters "+parameters);
		
				
			// calling this function to get the dynamic parameters for target URL
		 	parameters = getDynamicParameters();
		 	
			var finalTarget = target+parameters;
			
			//alert("finalTarget "+finalTarget); 
			//Changes made for JIRA# ECMSEARCH-395 on 17-Feb-2007

			//Changes made for JIRA # ECMSEARCH-689
			if(document.form1 != null)
			{
				if(document.form1.viewOption != null)
				{
					var viewOption = document.form1.viewOption.value;
					document.location = finalTarget + "&selectedOption=" + viewOption;
				}
				else
				{
					document.location = finalTarget;
				}
			}
			else
			{
				document.location = finalTarget;
			}			
			//document.AdvancedWidgetSearchForm.method = 'post';
			//document.AdvancedWidgetSearchForm.submit();	 					
		}
		
	}	
	
	return false;
}	  

//Added by Rajkumar for providing Sort option on 14-09-06 JIRA:ECMSEARCH-300  
function callDAtts(TempdAttsValue){
var DATTS=sortFieldValue;;
	var cTemp;
	var str;
	if (TempdAttsValue != null) {
		var DattsArr = TempdAttsValue.split(",");	
		
		for(var i=0; i<DattsArr.length; i++){
			if (DattsArr[i]!=sortFieldValue){
			DATTS=DATTS+","+DattsArr[i];
			}
   		}
	}
 return DATTS;
}
function validateSort(){
var sortType = document.AdvancedWidgetSearchForm.sortType.options[document.AdvancedWidgetSearchForm.sortType.selectedIndex].value;
	if (sortType == 1){
	sortParam ="sorting";
	}
	else {
	sortParam ="sortDesc";
	}
}
//End here
 
// This function will be used to display 'N/A' and
// enable or disable date field text box according
// to the options selected in the date combo box

function validateDate() {    

	var date = document.AdvancedWidgetSearchForm.docType.options[document.AdvancedWidgetSearchForm.docType.selectedIndex].value;
	var toValue = document.AdvancedWidgetSearchForm.toDate.value;
	var fromValue = document.AdvancedWidgetSearchForm.fromDate.value;
	// enable both the text boxes.and set value as null
	//document.AdvancedWidgetSearchForm.toDate.disabled = false;
	//document.AdvancedWidgetSearchForm.fromDate.disabled = false;	
	//document.AdvancedWidgetSearchForm.fromDate.value = "";
	//document.AdvancedWidgetSearchForm.toDate.value = "";	
	
	if (date == 0) {		

		document.AdvancedWidgetSearchForm.toDate.disabled = false;
		document.AdvancedWidgetSearchForm.fromDate.disabled = false;	
		document.AdvancedWidgetSearchForm.fromDateImage.disabled = false;		   	    
		document.AdvancedWidgetSearchForm.toDateImage.disabled = false;		   	     
		
		if( document.AdvancedWidgetSearchForm.toDate.value !=null &&
			Trim(document.AdvancedWidgetSearchForm.toDate.value).length > 0 &&
			Trim(document.AdvancedWidgetSearchForm.toDate.value)== 'N/A'){			
			document.AdvancedWidgetSearchForm.toDate.value = "";		
		}else if( document.AdvancedWidgetSearchForm.fromDate.value !=null &&
			Trim(document.AdvancedWidgetSearchForm.fromDate.value).length > 0 &&
			Trim(document.AdvancedWidgetSearchForm.fromDate.value)== 'N/A'){			
			document.AdvancedWidgetSearchForm.fromDate.value = "";		
		}	 
		

	}else if(date == 3){		

		document.AdvancedWidgetSearchForm.toDate.disabled = false;
		document.AdvancedWidgetSearchForm.fromDate.disabled = false;		
		document.AdvancedWidgetSearchForm.fromDateImage.disabled = false;		   	    
		document.AdvancedWidgetSearchForm.toDateImage.disabled = false;		   	     
		
		//alert("todate value "+document.AdvancedWidgetSearchForm.toDate.value);
		//alert("todate value lenght "+(document.AdvancedWidgetSearchForm.toDate.value).length);
		
		if( document.AdvancedWidgetSearchForm.toDate.value !=null &&
			Trim(document.AdvancedWidgetSearchForm.toDate.value).length > 0 &&
			Trim(document.AdvancedWidgetSearchForm.toDate.value) == 'N/A'){			
			document.AdvancedWidgetSearchForm.toDate.value = "";		
		}else if( document.AdvancedWidgetSearchForm.fromDate.value !=null &&
			Trim(document.AdvancedWidgetSearchForm.fromDate.value).length > 0 &&
			Trim(document.AdvancedWidgetSearchForm.fromDate.value)== 'N/A'){			
			document.AdvancedWidgetSearchForm.fromDate.value = "";		
		}	
		

	}else if( (date ==2) || (date == 4)){	
	
	    //alert("comes here");
		
		document.AdvancedWidgetSearchForm.toDate.disabled = true;
		document.AdvancedWidgetSearchForm.toDate.value='N/A';
		document.AdvancedWidgetSearchForm.toDateImage.disabled = true;		   	     
		document.AdvancedWidgetSearchForm.fromDate.disabled = false;			
		document.AdvancedWidgetSearchForm.fromDateImage.disabled = false;		   	    
		
		if( document.AdvancedWidgetSearchForm.fromDate.value !=null &&
			Trim(document.AdvancedWidgetSearchForm.fromDate.value).length > 0 &&
			Trim(document.AdvancedWidgetSearchForm.fromDate.value)== 'N/A'){			
			document.AdvancedWidgetSearchForm.fromDate.value = "";		
		}			

	}else if(date == 1){// If date is before, from date has been disabled and todate had been enabled 
	
		document.AdvancedWidgetSearchForm.fromDate.disabled = true;
		document.AdvancedWidgetSearchForm.fromDate.value='N/A';
		document.AdvancedWidgetSearchForm.fromDateImage.disabled = true;		   	     
		document.AdvancedWidgetSearchForm.toDate.disabled = false;			
		document.AdvancedWidgetSearchForm.toDateImage.disabled = false;	
		if( document.AdvancedWidgetSearchForm.toDate.value !=null &&
			Trim(document.AdvancedWidgetSearchForm.toDate.value).length > 0 &&
			Trim(document.AdvancedWidgetSearchForm.toDate.value)== 'N/A'){			
			document.AdvancedWidgetSearchForm.toDate.value = "";		
		}		
			   	    
	}								  		
	return false;
}

// This function compare the dates
// and it will return the value

 function compareDates (value1, value2)  {

   var date1, date2;
   var month1, month2;											   
   var year1, year2;

   year1  = value1.substring (0, value1.indexOf ("/"));
   month1 = value1.substring (value1.indexOf ("/")+1, value1.lastIndexOf ("/"));
   date1 = value1.substring (value1.lastIndexOf ("/")+1, value1.length);     

   year2  = value2.substring (0, value2.indexOf ("/"));
   month2 = value2.substring (value2.indexOf ("/")+1, value2.lastIndexOf ("/"));
   date2 = value2.substring (value2.lastIndexOf ("/")+1, value2.length);      

   if (year1 > year2) return false;
   else if (year1 < year2) return true;
   else if (month1 > month2) return false;
   else if (month1 < month2) return true;
   else if (date1 > date2) return false;
   else if (date1 < date2) return true;
   else return true;
} 

// This function is used to show the calender popup

function show_calendar() {

	/* 
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		p_item	: Return Item.
	*/

	p_item = arguments[0];
	if (arguments[1] == null)
		p_month = new String(gNow.getMonth());
	else
		p_month = arguments[1];
	if (arguments[2] == "" || arguments[2] == null)
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[2];
	if (arguments[3] == null)
		p_format = "YYYY/MM/DD";
	else
		p_format="YYYY/MM/DD";
	
	currentdomain = document.domain;
		
	if(lang=="RUS")
	{
		if(currentdomain == "worldbank.org")
			vWinCal = window.open("/shared/SiteResources/scripts/Calendar_Domain.htm", "Calendar","width=620,height=250,status=no,resizable=no,top=200,left=200");
		else
			vWinCal = window.open("", "Calendar","width=620,height=250,status=no,resizable=no,top=200,left=200");	
	}
	else
	{
		if(currentdomain == "worldbank.org")
			vWinCal = window.open("/shared/SiteResources/scripts/Calendar_Domain.htm", "Calendar","width=280,height=250,status=no,resizable=no,top=200,left=200");
		else
			vWinCal = window.open("", "Calendar","width=280,height=250,status=no,resizable=no,top=200,left=200");
	}
	vWinCal.opener = self;
	ggWinCal = vWinCal;

	setTimeout("Build('" + p_item + "','" + p_month + "','" + p_year + "','" + p_format + "')",1000);

}

function Build(p_item, p_month, p_year, p_format) {
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year, p_format);

	// Customize your Calendar here..
	gCal.gBGColor="white";
	gCal.gLinkColor="black";
	gCal.gTextColor="black";
	gCal.gHeaderColor="darkgreen";

	// Choose appropriate show function
	if (gCal.gYearly)	
		gCal.showY();
	else
		gCal.show();
}

//Added by Rajkumar for providing Multilingual calendar on 14-09-06 JIRA:ECMSEARCH-300
Calendar.Months = [ "January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December" ];
//Calendar.Months_FRE = ["Janvier", "F&#233;vrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Ao&#251;t", "Septembre", "Octobre", "Novembre", "D&#233;cembre" ];
//Calendar.Months_SPA = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" ];

// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(p_item, p_WinCal, p_month, p_year, p_format) {
	if ((p_month == null) && (p_year == null))	return;

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;
	
	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
	this.gBGColor = "white";
	this.gFGColor = "black";
	this.gTextColor = "black";
	this.gHeaderColor = "black";
	this.gReturnItem = p_item;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;
Calendar.print = Calendar_print;

//Changed by Rajkumar for providing  Multilingual calendar on 14-09-06 JIRA:ECMSEARCH-300
//Modified by uday to support Multilingual calendar for chinece 26-02-07 JIRA: ECMSAERCH-388
function Calendar_get_month(monthNo) {
   if(lang=="FRE"){
   return Calendar_FRE[monthNo];
   }
   else if(lang=="SPA"){
   return Calendar_SPA[monthNo];
   }
   else if(lang=="RUS"){
   return Calendar_RUS[monthNo];
   }
   else if(lang=="ARA"){
   return Calendar_ARA[monthNo];
   }
   else if(lang=="CHI"){
   return Calendar_CHI[monthNo];
   }
   else{
	return Calendar.Months[monthNo];
	}
}

function Calendar_get_daysofmonth(monthNo, p_year) {
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];
	
		return Calendar.lDOMonth[monthNo];
	} else
		return Calendar.DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

function Calendar_print() {
	ggWinCal.print();
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	
	// Begin Table Drawing code here..
	vCode = vCode + "<TABLE BORDER=1 BGCOLOR=\"" + this.gBGColor + "\">";
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	
	vCode = vCode + "</TABLE>";
	
	return vCode;
}

Calendar.prototype.show = function() {
		var vCode = "";
	
//	this.gWinCal.document.open();
    var htmlCode = "";
	// Setup the page...
	htmlCode +="<html>";
	htmlCode +="<head>";
	htmlCode +="<meta  encoding=UTF-8/>";
	htmlCode +="<title>";
	htmlCode +=calendar_Name;
	htmlCode +="</title>";
	
	if(currentdomain == "worldbank.org")
	{
		htmlCode +="<script>";
		htmlCode +="document.domain=\"worldbank.org\"";
		htmlCode +="</script>";
	}
	
	htmlCode +="</head>";

	htmlCode +="<body " +"link=\"" + this.gLinkColor + "\" " + "vlink=\"" + this.gLinkColor + "\" " +"alink=\"" + this.gLinkColor + "\" " +"text=\"" + this.gTextColor + "\">";
	htmlCode +="<FONT FACE='" + fontface + "' SIZE=2><B>";
	htmlCode +=this.gMonthName + " " + this.gYear;
	htmlCode +="</B><BR>"; 

	// Show navigation buttons
	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	
	htmlCode +="<TABLE WIDTH='100%' BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR='#e0e0e0'><TR><TD ALIGN=center>";
	htmlCode +="[<A HREF=\"" +	"javascript:window.opener.Build(" + "'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1) + "', '" + this.gFormat + "'" +");" +	"\"><<<\/A>]</TD><TD ALIGN=center>";
	htmlCode +="[<A HREF=\"" +"javascript:window.opener.Build(" + "'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +	");" +"\"><<\/A>]</TD><TD ALIGN=center>";
	
	htmlCode +="[<A HREF=\"" +"javascript:window.opener.Build(" + "'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +	");" +"\">><\/A>]</TD><TD ALIGN=center>";
	htmlCode +="[<A HREF=\"" +"javascript:window.opener.Build(" + "'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1) + "', '" + this.gFormat + "'" +");" +"\">>><\/A>]</TD></TR></TABLE><BR>";

	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	htmlCode +=vCode;

	htmlCode +="</font></body></html>";
	
	this.gWinCal.document.write(htmlCode);
	this.gWinCal.document.close();
	}

Calendar.prototype.showY = function() {
	var vCode = "";
	var i;
	var vr, vc, vx, vy;		// Row, Column, X-coord, Y-coord
	var vxf = 285;			// X-Factor
	var vyf = 200;			// Y-Factor
	var vxm = 10;			// X-margin
	var vym;				// Y-margin
	if (isIE)	vym = 75;
	else if (isNav)	vym = 25;
	
//	this.gWinCal.document.open();
	var htmlCode="";
	htmlCode +="<html>";
	htmlCode +="<head><title>Calendar</title>";
	
	if(currentdomain == "worldbank.org")
	{
		htmlCode +="<script>";
		htmlCode +="document.domain=\"worldbank.org\"";
		htmlCode +="</script>";
	}
	
	htmlCode +="<style type='text/css'>\n<!--";
	for (i=0; i<12; i++) {
		vc = i % 3;
		if (i>=0 && i<= 2)	vr = 0;
		if (i>=3 && i<= 5)	vr = 1;
		if (i>=6 && i<= 8)	vr = 2;
		if (i>=9 && i<= 11)	vr = 3;
		
		vx = parseInt(vxf * vc) + vxm;
		vy = parseInt(vyf * vr) + vym;

		htmlCode +=".lclass" + i + " {position:absolute;top:" + vy + ";left:" + vx + ";}";
	}
	htmlCode +="-->\n</style>";
	htmlCode +="</head>";

	htmlCode +="<body " + "link=\"" + this.gLinkColor + "\" " + "vlink=\"" + this.gLinkColor + "\" " +	"alink=\"" + this.gLinkColor + "\" " +"text=\"" + this.gTextColor + "\">";
	htmlCode +="<FONT FACE='" + fontface + "' SIZE=2><B>";
	htmlCode +="Year : " + this.gYear;
	htmlCode +="</B><BR>";

	// Show navigation buttons
	var prevYYYY = parseInt(this.gYear) - 1;
	var nextYYYY = parseInt(this.gYear) + 1;
	
	htmlCode +="<TABLE WIDTH='100%' BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR='#e0e0e0'><TR><TD ALIGN=center>";
	htmlCode +="[<A HREF=\"" +"javascript:window.opener.Build(" + "'" + this.gReturnItem + "', null, '" + prevYYYY + "', '" + this.gFormat + "'" +");" +"\" alt='Prev Year'><<<\/A>]</TD><TD ALIGN=center>";
	htmlCode +="[<A HREF=\"javascript:window.print();\">Print</A>]</TD><TD ALIGN=center>";
	htmlCode +="[<A HREF=\"" +"javascript:window.opener.Build(" + "'" + this.gReturnItem + "', null, '" + nextYYYY + "', '" + this.gFormat + "'" +	");" +"\">>><\/A>]</TD></TR></TABLE><BR>";

	// Get the complete calendar code for each month..
	var j;
	for (i=11; i>=0; i--) {
		if (isIE)
			htmlCode +="<DIV ID=\"layer" + i + "\" CLASS=\"lclass" + i + "\">";
		else if (isNav)
			htmlCode +="<LAYER ID=\"layer" + i + "\" CLASS=\"lclass" + i + "\">";

		this.gMonth = i;
		this.gMonthName = Calendar.get_month(this.gMonth);
		vCode = this.getMonthlyCalendarCode();
		htmlCode +=this.gMonthName + "/" + this.gYear + "<BR>";
		htmlCode +=vCode;

		if (isIE)
			htmlCode +="</DIV>";
		else if (isNav)
			htmlCode +="</LAYER>";
	}

	htmlCode +="</font><BR></body></html>";
	this.gWinCal.document.write(htmlCode);
	this.gWinCal.document.close();
	
}

Calendar.prototype.wwrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}

Calendar.prototype.wwriteA = function(wtext) {
	this.gWinCal.document.write(wtext);
}

Calendar.prototype.cal_header = function() {
	var vCode = "";
	//Added by Rajkumar for providing  Multilingual calendar on 14-09-06 JIRA:ECMSEARCH-300
	if(lang=="FRE")
	{	
		vCode ="<TR>";
		for(var i=0;i<6;i++)
		{		
			vCode +="<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_FRE[i]+"</B></FONT></TD>";	    
		}
		vCode +="<TD WIDTH='16%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_FRE[i]+"</B></FONT></TD></TR>";			
	}	
	else if(lang=="SPA")
	{	
		vCode ="<TR>";
		for(var i=0;i<6;i++)
		{
		vCode +="<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_SPA[i]+"</B></FONT></TD>";
		}
		vCode +="<TD WIDTH='16%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_SPA[i]+"</B></FONT></TD></TR>";			
	}
	else if(lang=="RUS")
	{		
		vCode ="<TR>";
		for(var i=0;i<6;i++)
		{
		vCode +="<TD><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_RUS[i]+"</B></FONT></TD>";
		}
		vCode +="<TD><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_RUS[i]+"</B></FONT></TD></TR>";			
	}	
	else if(lang=="ARA"){	
	vCode ="<TR>";
	for(var i=0;i<6;i++)
	{
	vCode +="<TD><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_ARA[i]+"</B></FONT></TD>";
	}
	vCode +="<TD><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_ARA[i]+"</B></FONT></TD></TR>";

	/*
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>???????</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>????????</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>????????</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>??????</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>??????</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>????? </B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='16%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>????? </B></FONT></TD>";
	vCode = vCode + "</TR>";
	*/
	}
	//Added by Uday for Chinece Calendar on 26-02-07 JIRA:ECMSEARCH-388
	else if(lang=="CHI")
	{		
		vCode ="<TR>";
		for(var i=0;i<6;i++)
		{
		vCode +="<TD><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_CHI[i]+"</B></FONT></TD>";
		}
		vCode +="<TD><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>"+Days_CHI[i]+"</B></FONT></TD></TR>";			
	}
	else{
	vCode = vCode + "<TR>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Sun</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Mon</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Tue</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Wed</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Thu</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='14%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Fri</B></FONT></TD>";
	vCode = vCode + "<TD WIDTH='16%'><FONT SIZE='2' FACE='" + fontface + "' COLOR='" + this.gHeaderColor + "'><B>Sat</B></FONT></TD>";
	vCode = vCode + "</TR>";
	}
	
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/

	vCode = vCode + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(i) + "><FONT SIZE='2' FACE='" + fontface + "'> </FONT></TD>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j) + "><FONT SIZE='2' FACE='" + fontface + "'>" + 
			"<A HREF='#' " + "onClick=\"self.opener.document." + this.gReturnItem + ".value='" + this.format_data(vDay) + "';self.opener.document." + this.gReturnItem + ".focus();self.opener.document." 
				+ this.gReturnItem + ".blur();window.close();\">" + this.format_day(vDay) + "</A>" + "</FONT></TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=0; j<7; j++) {
			vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j) + "><FONT SIZE='2' FACE='" + fontface + "'>" + 
				"<A HREF='#' " + "onClick=\"self.opener.document." + this.gReturnItem + ".value='" + this.format_data(vDay) + "';self.opener.document." + this.gReturnItem + ".focus();self.opener.document." +	this.gReturnItem + ".blur();window.close();\">" +this.format_day(vDay) +"</A>" +"</FONT></TD>";vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++) {
		if (this.gYearly)
			vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j+m) + 
			"><FONT SIZE='2' FACE='" + fontface + "' COLOR='gray'> </FONT></TD>";
		else
			vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j+m) + 
			"><FONT SIZE='2' FACE='" + fontface + "' COLOR='gray'>" + m + "</FONT></TD>";
	}
	
	return vCode;
}

Calendar.prototype.format_day = function(vday) {
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();

	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		return ("<FONT COLOR=\"RED\"><B>" + vday + "</B></FONT>");
	else
		return (vday);
}

Calendar.prototype.write_weekend_string = function(vday) {
	var i;

	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i])
			return (" BGCOLOR=\"" + weekendColor + "\"");
	}
	
	return "";
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (this.gFormat) {
		case "MM\/DD\/YYYY" :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
			break;
		case "MM\/DD\/YY" :
			vData = vMonth + "\/" + vDD + "\/" + vY2;
			break;
		case "MM-DD-YYYY" :
			vData = vMonth + "-" + vDD + "-" + vY4;
			break;
		case "MM-DD-YY" :
			vData = vMonth + "-" + vDD + "-" + vY2;
			break;

		case "DD\/MON\/YYYY" :
			vData = vDD + "\/" + vMon + "\/" + vY4;
			break;
		case "DD\/MON\/YY" :
			vData = vDD + "\/" + vMon + "\/" + vY2;
			break;
		case "DD-MON-YYYY" :
			vData = vDD + "-" + vMon + "-" + vY4;
			break;
		case "DD-MON-YY" :
			vData = vDD + "-" + vMon + "-" + vY2;
			break;

		case "DD\/MONTH\/YYYY" :
			vData = vDD + "\/" + vFMon + "\/" + vY4;
			break;
		case "DD\/MONTH\/YY" :
			vData = vDD + "\/" + vFMon + "\/" + vY2;
			break;
		case "DD-MONTH-YYYY" :
			vData = vDD + "-" + vFMon + "-" + vY4;
			break;
		case "DD-MONTH-YY" :
			vData = vDD + "-" + vFMon + "-" + vY2;
			break;

		case "DD\/MM\/YYYY" :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
			break;
		case "DD\/MM\/YY" :
			vData = vDD + "\/" + vMonth + "\/" + vY2;
			break;
		case "DD-MM-YYYY" :
			vData = vDD + "-" + vMonth + "-" + vY4;
			break;
		case "DD-MM-YY" :
			vData = vDD + "-" + vMonth + "-" + vY2;
			break;
		case "YYYY\/MM\/DD":
			vData = vY4 + "\/" + vMonth + "\/" + vDD;
			break;
		default :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
	}
	return vData;
}
