//  OCelO.js
//  site specific JavaScript for the O-Cel-O EWCD site.
//alert("Found site specific JS file");

// custom quickform code follows

/*
 * qform.js: This file contains the script required at the render survey
 * Version: 	1.0 		20-APR-2004
 * @author: 	VenkataRajesh Juturu(Wipro Technologies)
 * Copyright (c) 1995-2004 3M Worldwide.
 * All Rights Reserved.
 */

function disableEnterKey() 
{ 
    if (window.event.keyCode == 13) {

     window.event.keyCode = 0; 
     }
} 


function checkOnSubmit(capture) {
	var toNullEle = 0;
	var count = 0;
	var emailFlag = true;
	var captureServlet = capture;
	
	
	if(document.qform.action == contextPath+"/UpdateSurvey") return true;
	
	
	
	var totalEmailQues = eval(document.qform.elements[0].value);

	if(totalEmailQues != 0){
		emailFlag =isEmailOkay(document.qform.elements[2].value);
		toNullEle = eval(document.qform.elements[0].value);
		
	}

	if(emailFlag != false){
			
			for(i=0; i<document.qform.elements.length; i++) {
       				var elm = document.qform.elements[i];
					if(elm.type == 'text' || elm.type == 'select-one'){
						if(elm.value == '' || elm.value == 'Select') toNullEle = toNullEle + 1;
					}
					
					if(elm.type == 'checkbox' || elm.type == 'radio')
					 	 if( elm.checked == false ) toNullEle = toNullEle + 1;
				 
					 
					 if(elm.type == 'hidden')  toNullEle = toNullEle + 1;
							
					
       		}
       		if(toNullEle != document.qform.elements.length){
       		
       				alert('you need to answer at least one question to submit');
       				document.qform.action = "";
       				return false;
       		}
       		//code added by UpendarRao mikkilineni on 12/08/2004 for forward to CaptureServlet
       		//Begin of enhancements 
       		if(captureServlet=='captureServlet'){
       			document.qform.action = contextPath+"/captureServlet";
       		//End of enhancements 	
       		}else {
       			document.qform.action = contextPath+"/qform/captureResponse";
       		}
       		
	// ****************************************************
	// CustomValidateFunction call added 12/30/04 by SM. 
	// To use: include lines like these in the PAGE_MSG.entry.header (enclosed in script tags):
	//	function myCustomValidate() {
	//		if(document.qform.F5.value == '') { 
	//			alert("Please enter a product type.");
	//			return false;
	//		}
	//	}	
	//	var customValidateFunction = "myCustomValidate()";
	// ****************************************************
	  	if(window.customValidateFunction) {
			emailFlag = eval(customValidateFunction);
		}
       		
       } 
       
      return emailFlag;
}

/* This script will be called by the xslt files for 
 * updation of the xml
 */
function change(hascondopt) {
	
	
	if(hascondopt == true ){
	//Sets the servlet name to which the form has to be submitted
	document.qform.action = contextPath+"/XMLUpdate";
	//Submit the form
	document.qform.submit();
	}
	
}//End of change()

/* Script to check the validation of the email ID
 * @param questionID ---- contains the questionid of email
 */
function checkEmail(questionID) {

	
	document.qform.email.value = questionID.value;
	questionID.value = 	removeSpace(questionID.value);
	if(isEmailOkay(questionID.value) == false){//Call the script to check email 
	    questionID.value = "";
	 	questionID.focus();
	 	return false; 
	 }//End of check email
	
	 //Changes the servlet which to which the form has to be submitted	
	 if(document.qform.updatable.value == "true"){
	 document.qform.action = contextPath+"/UpdateSurvey";
	 document.qform.submit();
	 }//End of if to submit the form
	
	 return true;
	  
}//End of checkEmail()



/* Script to remove the spaces in the string 
 * @param src ---string in which the spaces are to be removed
 */
function removeSpace(src) {
	var result="";
	var inPtr=0;
	//Loop to check the white spaces
	for(inPtr=0; inPtr < src.length; inPtr++) {
		if(src.charAt(inPtr) != ' ') {//check if the character is white space
			result+=src.charAt(inPtr);
		}//end of check for the character is white space
	}//End of loop
	
	return result;
}//End of removeSpace()

/* Script to check the bad characters in the mail
 * @param string ----contains the mail string in which bad charaters 
                     are to be checked
 */  
function emailBadChars(string) {

   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()&$#%";

   //Loop to check the string
   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1) { //condition to check for bad chars
         return true;
      }//End of condition
   }//End of loop
   return false;
}//End of emailBadChars()                 



/* Script to check whether the the email is ok
 */
function isEmailOkay(valueCheck) {
	
	var result = true;
    valueCheck = removeSpace(valueCheck)
    if (emailBadChars(valueCheck) == true) {//Check for badcharacters
    	   alert("We're sorry, but the email address you entered contains invalid characters.\r\n"+
			"Please re-enter your email address.");
        result = false;
    }//end of check for bad characters
    else {//Else for not having bad characeters
		if(valueCheck.length < 7) {//check length of email
				alert("We're sorry, but the email address you entered does not contain enough characters to be valid.\r\n"+
				"Please re-enter your email address.");
				result = false;
		}//end of check length
		else {
		  if(      valueCheck.indexOf('@') < 1
			|| valueCheck.lastIndexOf('.') < valueCheck.indexOf('@')+2
			|| valueCheck.lastIndexOf('.') == valueCheck.length - 1
			|| valueCheck.lastIndexOf('@') == valueCheck.length - 1)  { //Check for  @, . in the email
				alert("We're sorry, but the email address you entered does not follow the pattern x@x.x\r\n"+
				"Please re-enter your email address.");
				result = false;
		  }//End of check for @ . in email
		}
    }//End of else for not having bad characters
    return result;
}//End of isEmailOkay()


//End of qform.js

