///associative array that maps the names of the mandatory fields to
//the number of fields with the same name that have to be checked
//initialized by mandatoryFields(); in this version, all the names are mapped to a count of 1
//used by checkMandatory()

var mandFields;
var mandHiddenFields=new Array();

var manKeys = new Array();
var manValues = new Array();

manKeys[0]="EMAIL";
manValues[0]="E-MAIL";
manKeys[1]="LAST_NAME";
manValues[1]="LAST NAME";
manKeys[2]="FIRST_NAME";
manValues[2]="FIRST NAME";
manKeys[3]="ACTIVITY_NAME";
manValues[3]="ACTIVITY NAME";
manKeys[4]="ACTIVITY_DESC";
manValues[4]="ACTIVITY DESCRIPTION";
manKeys[5]="EVENT_NAME";
manValues[5]="EVENT NAME";
manKeys[6]="EVENT_START";
manValues[6]="EVENT BEGIN";
manKeys[7]="EVENT_END";
manValues[7]="EVENT END";
manKeys[8]="BOOKING_START";
manValues[8]="ALLOWED ARRIVAL DATE FOR BOOKING";
manKeys[9]="BOOKING_END";
manValues[9]="ALLOWED DEPARTURE DATE FOR BOOKING";
manKeys[10]="FORM_NAME";
manValues[10]="FORM NAME";
manKeys[11]="REPORT_NAME";
manValues[11]="REPORT NAME";
manKeys[12]="REPORT_TYPE";
manValues[12]="REPORT TYPE";
manKeys[13]="REPORT_DESC";
manValues[13]="REPORT DESCRIPTION";
manKeys[14]="HOTEL_NAME";
manValues[14]="HOTEL NAME";
manKeys[15]="ROOM_BLOCK_NAME";
manValues[15]="ROOM BLOCK NAME";
manKeys[16]="ARRIVAL_DATE";
manValues[16]="ARRIVAL DATE";
manKeys[17]="DEPARTURE_DATE";
manValues[17]="DEPARTURE DATE";
manKeys[18]="ALLOTMENT";
manValues[18]="ALLOTMENT";
manKeys[19]="GROUP_NAME";
manValues[19]="GROUP NAME";
manKeys[20]="AGENDA_NAME";
manValues[20]="AGENDA NAME";
manKeys[21]="BADGE_NAME";
manValues[21]="BADGE NAME";
manKeys[22]="BADGE_TYPE";
manValues[22]="BADGE TYPE";
manKeys[23]="BADGE_DESC";
manValues[23]="BADGE DESCRIPTION";
manKeys[24]="SALUTATION";
manValues[24]="TITLE";
manKeys[25]="'IS_HP";
manValues[25]="IS HP";
manKeys[26]="STATE";
manValues[26]="STATE";
manKeys[27]="PHONE_NR";
manValues[27]="PHONE NR";
manKeys[28]="LOCATION_CODE";
manValues[28]="LOCATION CODE";
manKeys[29]="COMPANY_NAME";
manValues[29]="COMPANY NAME";
manKeys[30]="COUNTRY_ISO_CD";
manValues[30]="COUNTRY";
manKeys[31]="MENU_BUTTON_NAME";
manValues[31]="MENU BUTTON NAME";
manKeys[32]="EMAIL_NAME";
manValues[32]="EMAIL NAME";
manKeys[33]="EMAIL_TYPE";
manValues[33]="EMAIL TYPE";
manKeys[34]="JSP_PAGE_NAME";
manValues[34]="FILE NAME";
manKeys[35]="FILE_NAME";
manValues[35]="FILE NAME";
manKeys[36]="FILE_TYPE";
manValues[36]="FILE TYPE";
manKeys[37]="UPLOAD_FILE";
manValues[37]="UPLOAD FILE";
manKeys[38]="FILE_CATEGORY_NAME";
manValues[38]="CATEGORY NAME";
manKeys[39]="FREE_TEXT_FIELD";
manValues[39]="";
manKeys[40]="LCD1";
manValues[40]="LOCATION CODE";
manKeys[41]="LCD2";
manValues[41]="LOCATION CODE";
manKeys[42]="LCD3";
manValues[42]="LOCATION CODE";
manKeys[43]="LCD4";
manValues[43]="LOCATION CODE";
manKeys[44]="LCD5";
manValues[44]="LOCATION CODE";
manKeys[45]="LCD6";
manValues[45]="LOCATION CODE";
manKeys[46]="HOTEL_NOT_NEEDED";
manValues[46]="Accomodation required";
manKeys[46]="MAIL_ADDRESS";
manValues[46]="ADDRESS";
manKeys[47]="PASSPORT_NUMBER";
manValues[47]="PASSPORT NUMBER";
manKeys[48]="PASSPORT_EXPIRATION";
manValues[48]="PASSPORT EXPIRATION";
manKeys[49]="PASSPORT_ISSUED_AT";
manValues[49]="PASSPORT ISSUED AT";
manKeys[50]="PASSPORT_ISO_CD";
manValues[50]="NATIONALITY";
manKeys[51]="DEPARTMENT";
manValues[51]="DEPARTMENT";
manKeys[52]="JOB_TITLE";
manValues[52]="JOB TITLE";
manKeys[53]="ZIP";
manValues[53]="ZIP CODE";
manKeys[54]="CITY";
manValues[54]="CITY";
manKeys[55]="ALT_NR";
manValues[55]="CELLULAR PHONE";
manKeys[56]="LOGIN_NAME";
manValues[56]="LOGIN NAME";
manKeys[57]="PASSWORD";
manValues[57]="PASSWORD";
manKeys[58]="PASSWORD2";
manValues[58]="PASSWORD CONFIRMATION";
manKeys[59]="PASSWORD";
manValues[59]="PASSWORD";
manKeys[60]="FAX_NR";
manValues[60]="FAX";
manKeys[61]="SCHOOL";
manValues[61]="SCHOOL";
manKeys[62]="SEX";
manValues[62]="GENDER";
manKeys[63]="STATE";
manValues[63]="STATE";
manKeys[64]="CURRENCY_ID";
manValues[64]="CURRENCY";
manKeys[65]="BADGE_CATEGORY";
manValues[65]="BADGE CATEGORY";
manKeys[66]="REGION";
manValues[66]="REGION";
manKeys[67]="ARRIVAL_TIME";
manValues[67]="ARRIVAL TIME";
manKeys[68]="ARRIVAL_REMARKS";
manValues[68]="ARRIVAL FLIGHT";
manKeys[69]="DEPARTURE_TIME";
manValues[69]="DEPARTURE TIME";
manKeys[70]="DEPARTURE_REMARKS";
manValues[70]="DEPARTURE FLIGHT";
manKeys[71]="ARRIVAL_FROM";
manValues[71]="ARRIVAL FROM";
manKeys[72]="DEPARTURE_TO";
manValues[72]="DEPARTURE TO";
manKeys[73]="BIRTH_DATE";
manValues[73]="BIRTH DATE";
manKeys[74]="PLACE_OF_BIRTH";
manValues[74]="PLACE OF BIRTH";
manKeys[75]="AUTHOR_INITIAL_0";
manValues[75]="AUTHOR INITIALS";
manKeys[76]="AUTHOR_0";
manValues[76]="AUTHOR NAME";
manKeys[77]="INST_IDX_0";
manValues[77]="INSTITUTION #";
manKeys[78]="INSTITUTION_0";
manValues[78]="INSTITUTION";
manKeys[79]="FILE_TYPE_ID";
manValues[79]="CATEGORY";
manKeys[80]="FILE_DESC";
manValues[80]="TITLE";
manKeys[81]="COMPANY_ID";
manValues[81]="COMPANY NAME";
manKeys[82]="PROFILE_GROUP_ID";
manValues[82]="CATEGORY/GROUP";
manKeys[83]="BUSINESS_UNIT";
manValues[83]="BUSINESS UNIT";
manKeys[84]="ORGANIZATION";
manValues[84]="ORGANIZATION";
manKeys[85]="FILE_CATEGORY_ID";
manValues[85]="CATEGORY";
manKeys[86]="CC_ACCOUNT";
manValues[86]="CC ACCOUNT";
manKeys[87]="ACCOUNT_NAME";
manValues[87]="REGIS ACCOUNT NAME";
manKeys[88]="CC_PROVICER";
manValues[88]="CC PROVIDER";
manKeys[89]="LATE_REG_PENALTY_DATE";
manValues[89]="LATE CANCELLATION DATE";
manKeys[90]="CANCEL_LAST_NAME";
manValues[90]="Last Name";
manKeys[91]="CANCEL_FIRST_NAME";
manValues[91]="First Name";
manKeys[92]="CANCELLATION_MOTIVE";
manValues[92]="Reason";
manKeys[93]="TRANSPORT_ARRIVAL_DATE";
manValues[93]="TRANSPORT ARRIVAL DATE";
manKeys[94]="TRANSPORT_DEPARTURE_DATE";
manValues[94]="TRANSPORT DEPARTURE DATE";
manKeys[95]="SELECTED_INSERT_PERSON";
manValues[95]="coordinator";

var submitcount=0;
//var debugSumitcout=true;
var debugSumitcout=false;

//called to specify the list of field names that are mandatory when submitting a form
//it saves the list for later use by the checkMandatory() function
//usage example: <body onload="mandatoryFields('LAST_NAME', 'FIRST_NAME', 'SALUTATION')" language=JavaScript>
function mandatoryFields()
	{
	String.prototype.trim=trimSpaces;
	mandFields=new Array();
	var i;
	for(i=0; i<mandatoryFields.arguments.length; i++)
		mandFields[mandatoryFields.arguments[i]]=1;
	}

function mandatoryHiddenFields()
	{
	String.prototype.trim=trimSpaces;
	mandHiddenFields=new Array();
	var i;
	for(i=0; i<mandatoryHiddenFields.arguments.length; i++)
		mandHiddenFields[mandatoryHiddenFields.arguments[i]]=1;
	}

//pops up an error message if a mandatory field is empty
//and returns false to cancel the submit
//usage example: <form method=post action="test.jsp" onsubmit="return checkMandatory()" language=JavaScript>
//it checks the fields of the first form in the document
//it uses the list of mandatory field names passed previously to mandatoryFields()
//if a field contains only blanks, it is considered empty
//if there are several fields with the same name, only the first n are checked,
//where n is the number associated with the name in the mandFields array

function getAlertMessage(tmpName){

	var theFieldMsg="The answer to ";
	var isEmptyMsg="is empty.";
	var fillInMsg="Please answer all questions.";
		
	comment ="\n"+theFieldMsg +" \""+ tmpName  +"\" "+isEmptyMsg ;
	return fillInMsg + comment;
}

function checkMandatory(){
    var formElems=document.forms[0].elements;
    var tmpArray=new Array();
    var i;
    //all the strange code that follows is necessary due to the limitations in the Netscape Navigator DOM
    //i.e. there is no item() method, for getting only one of several fields with the same name
    
    for(i in mandFields){
	tmpArray[i]=mandFields[i];
    }
    var someEmpty=false;
    var fullComment="";
    for(i=0; i < formElems.length; i++){
	var tmpName=formElems[i].name;
	var tmpId=formElems[i].id;
	var tmpCount=tmpArray[tmpName];
	
	
	if(tmpCount!=null && tmpCount>0){
	    
	    tmpArray[tmpName]--;
	    
	    //the test below is necessary because in Navigator the value of select elements is null at this time
	    // Add on 13th may by JR (the javascript guru), also test if the field is hidden
	    // to not make a bug when field is hidden to participant 
	    //alert(i+" "+ formElems[i].value+" "+formElems[i].type+" "+formElems[i].name+" "+formElems[i].checked );
	    if(formElems[i].value!=null && formElems[i].value.trim().length==0 && (formElems[i].type!="hidden" || mandHiddenFields[tmpName] != null)){
		//alert("value="+formElems[i].value+"\ntype="+formElems[i].type+"\n");
		if(debugSumitcout) alert("reset submitcount!");
		someEmpty=true;
		var gotTranslation=false;
		var indefini;
		var comment = "";
		var theFieldMsg="The field";
		var isEmptyMsg="is empty.";
		var fillInMsg="Please fill in all required fields.";
		
		//alert ('theFieldMsg -->'+theFieldMsg);

		// translated alert messages, if they were not set for any reason, I give them default values
		// we will not put translation in the "create form" or "create admin" so the default wil be used, 
		// but if we wannt a fully translated regis, we could :)

		if (typeof theFieldMsgLng!="undefined"){
		    theFieldMsg=theFieldMsgLng;
		    //alert ('theFieldMsgLng is defined :) ->'+theFieldMsgLng);
		}
		if (typeof isEmptyMsgLng!="undefined"){
		    isEmptyMsg =isEmptyMsgLng;
		}
		if (typeof fillInMsgLng!="undefined"){
		    fillInMsg=fillInMsgLng;
		}
		

		// try to get a alert message in the static array
		for( k=0; k < manKeys.length; k++){
		    if( manKeys[k] == tmpName ){
			if( manValues[k] != "" && manValues[k] != "null" ){
			    comment = "\n"+theFieldMsg +" \""+ manValues[k] +"\" "+isEmptyMsg ;
			}
			break;
		    }
		}
		
		// this should be the best, get the text in "id" witch is the same as the translated label
		
		
		if ( tmpId!=null && tmpId.trim().length!=0 && tmpId!="from"){
		    comment = "\n"+theFieldMsg +" \""+ tmpId +"\" "+isEmptyMsg ;
		    gotTranslation=true;
		}

		// nothing found, so use the input name
		if (!gotTranslation && tmpName!=null && tmpName!=""){
		    comment ="\n"+theFieldMsg +" \""+ tmpName  +"\" "+isEmptyMsg ;
		}
		
		if (comment.indexOf("_") >= 0){
		    comment = comment.replace(/_/gi,' ');
		}
		//formElems[i].style.background="#ff8989";
		//if ((formElems[i].type)!='undefined' &&(formElems[i].type)!='textarea') {
		    formElems[i].className="inputRed";
		   
		    //}
	
		fullComment+=comment;
		

		
	    }
	    else {
		formElems[i].className="regInput";
		//formElems[i].style.background="white";
	    }
	}
    }
    if (someEmpty){
	alert(fillInMsg+ fullComment);
	scroll(0,0);
	submitcount=0;	//inserted=0;	
	return false;
	
    }
    
    if (submitcount == 0){
	submitcount++;
	if(debugSumitcout) alert("INFO: submitcount="+submitcount+"( was 0 )");	
	return true;
    }
    else{
	submitcount++;
	if(debugSumitcout) alert("Do not double click. you have pressed " + submitcount +" times the button! checmMandatory return false, submit blocked!");
	return false;
    }
}



//added as a method to the String class
//returns the string without any blank at the beginning or end

function trimSpaces(){
    var size=this.length;
    if(size==0) return this;
    var i;
    var j;
    for(i=0; i<size; i++)
	if(this.charAt(i)!=" ") break;
    for(j=size-1; j>i; j--)
	if(this.charAt(j)!=" ") break;
    if(i>j) return "";
    return this.substring(i, j+1);
}

function checkAnswerMandatory(answerField, alertText){
    var formElems=document.forms[0].elements;
    var el=formElems[answerField];
    
      if( el!=null){
	var v=el.value;
	if( v != null && v=='' && alertText!=null && alertText.length>0 ){
	    alert(getAlertMessage(alertText));
	    submitcount=0;
	    return false;
	}
    }
    
    return true;
}
function checkAnswerMandatoryChexkBox(answerField, alertText){
    var formElems=document.forms[0].elements;
    var el=formElems[answerField];
    
    if( el!=null){
	if( !el.checked ){
	    if( alertText!=null && alertText.length>0 ){
		alert(getAlertMessage(alertText));
		submitcount=0;
		return false;
	    }
	}
    }
    return true;
}

// Generic Method to check input groups such as radio buttons, checkboxes
// drop menu and so on, used in reg form for the folders

function checkFolderMandatory(activityGroup,alertText, type){
    if (activityGroup){
	
	// type == 0 > do nothing, return true
	// type == 1 > mandatory selection
	// type == -1 > refuse submit if selection
	
	// alert text is the message who will be prompted to the user

	// activityGroup is the name of the option group to check, 
	// must give the full object reference ->  document.myform.ACTIVITY_FOLDER_2123   
	// warning, do not put quotes, it's an object , not a string !!
	
	if (type==0 || type=='undefined'){
	    return true;
	}
	else if (type==1 || type== -1){
	    // alert("Check for "+activityGroup+"\ntype is "+type );
	    var mySize = activityGroup.length ;
	    var myType=activityGroup.constructor ;
	    var myType2=activityGroup.type ;
	    var myName=name;
	    var isRadioGroup=false;
	    var isSingleRadio=false;
	    var isMenu=false;
	    if (myType=='[NodeList]'){
		//   alert("mySize ="+mySize+"\n it's a radio  or check box group\nmyName="+myName+"\nmytype2="+myType2);
		isRadioGroup=true;
	    }
	    if (myType=='[HTMLInputElement]'){
		//	alert("mySize ="+mySize+"\n it's a check box\nmyName="+myName+"\nmytype2="+myType2);
		isSingleRadio=true;
	    }
	    if (myType=='[HTMLSelectElement]' ||myType2=='select-one' ){
		// alert("mySize ="+mySize+"\n it's a select menu\nmyName="+myName+"\nmytype2="+myType2+"\nmytype="+myType);
		isMenu=true;
	    }
	    //else alert("mySize ="+mySize+"\n mytype="+myType2);
	    var oneChoiceSelected=false;
	    var isSelected ;
	    if (isMenu){
		if (activityGroup.selectedIndex==0)oneChoiceSelected=false;
		else oneChoiceSelected=true;	
	    }
	    else{
		//alert ("my size is "+mySize);
		if (mySize>0){
		    for (i=0; i<mySize;i++){
			isSelected = activityGroup[i].checked  ;
			if (isSelected) oneChoiceSelected=true;
		    }
		}
		else {
		    if (activityGroup.checked) oneChoiceSelected=true; 
				//mySize="single radio";
	    }
	    }
	    // logic inversion for mandatory or refused submition
	    if (type ==1){
		if (oneChoiceSelected){
				//alert("someting is checked in folder "+myName+", it's ok then\n");
		    return true;
		}
		else {
		    alert(alertText);
		    submitcount=0;
		    return false;
		}
	    }
	    else if (type ==-1){
		if (!oneChoiceSelected){
				//alert("someting is checked in folder "+myName+", it's ok then\n");
		    return true;
		}
		else {
		    alert(alertText);
		    submitcount=0;
		    return false;
		}
	    }
	}
    }
    else {
	return true;
    }
}
