// $Header:   \\rlrgnttecf2\ecm\natwest.com\archives\web\secure\applicationforms_dda\javascript\validation_utils.js-arc   1.5   16 Aug 2004 15:38:14   Franrca  $ 
function changeStyle(objId, className){
	document.getElementById(objId).className = className;
}

function getFieldValue(formElement, inputType) {

   var value="";
   //alert(formElement.value);
   //alert(inputType);
   
   //check for carriage returns as this will cause problems in textareas
   if(escape(formElement.value).indexOf("%0D%0A") > -1){ 
		//Windows encodes returns as \r\n hex
		formElement.value=escape(formElement.value).replace(/%0D%0A/g," ");
		formElement.value=unescape(formElement.value);
	}
   if((formElement.value)&&(inputType!="CheckboxPara")){ 
   		value=formElement.value 
   } 
   else { 

		if(inputType=="CheckboxPara"){
			if(formElement.checked){
				value+="checked"; 
			}
			else{
				value+=""; 
			}
		}
		else{
	   		for (var i=0; i<formElement.length; i++) {
				if ( formElement[i].checked ) { 
					value+=formElement[i].value;  
				} 
			}//for
		} //else
	}//else
  return value;
  
}

function doValidationCheck(formElement, inputType, strLabel, strValidateL, strValidateR, strCurError) {

	if (strCurError == "") {  //Only do this validation if we don't already have an error for this field
    // Call the routine after substituting in the field value -->
		var strResponse = "";
	  	var strFieldValue = getFieldValue(formElement);

		//check for carriage returns as this will cause problems in textareas
		if(escape(strFieldValue).indexOf("%0D%0A") > -1){ 
			//Windows encodes returns as \r\n hex
			strFieldValue=escape(strFieldValue).replace(/%0D%0A/g," ");
			strFieldValue=unescape(strFieldValue);
		}
		if(strFieldValue.indexOf("\\") > -1){ 
			strFieldValue=strFieldValue.replace(/\\/g, "/");
		}
		
		//having problems with apostrophes - hence the following lines replacing apostrophes in the eval statement to 
		//speech marks to allow unput of odd number of apostrophes which previously caused errors as the eval would 
		//prematurely be finished. Amended any input of " to * as speech marks will now cause problems in the same way - have
		//therefore replaced with * in this data check as it sits at the same level validation-wise i.e not allowed
		if(strFieldValue.indexOf("\"") > -1){ 
			strFieldValue=strFieldValue.replace(/\"/g, "*");
		} 

		if((strValidateL.length > 0) && (strValidateR.length > 0)){
			while(strValidateL.indexOf("'") > -1){ 
				strValidateL=strValidateL.replace(/'/g, "\"");
			}
			while(strValidateR.indexOf("'") > -1){ 
				strValidateR=strValidateR.replace(/'/g, "\"");
			}
			strResponse = eval(strValidateL + strFieldValue + strValidateR);
			//strResponse = strFieldValue;
	      	if (strResponse != "") { strError+= "\n'" + strLabel + "' " + strResponse; }
		}
      	strCurError="";
   	}
}

function doMandatoryCheck(formElement, inputType, strLabel, strCondType, objCondField, strCondValue, strValidate) {
var bValsEqual = false;
var bCondMand = false;
var bValueFound = false;

 strCurControlError = ""; // Initialize
   
   if (strCondType != "") {// Check for conditionally mandatory stuff
      bCondMand = true;
      // Does the value of the target field equal the value we're looking for
      if (strCondValue == getFieldValue( objCondField)) {bValsEqual = true;}
   }

   // Check to see if we have a value 
  if (getFieldValue(formElement) != "") bValueFound = true;
  
  if(inputType=="CheckboxPara"){
		bValueFound = false;
		
 		if(getFieldValue(formElement, inputType) != ""){
			bValueFound = true;
		}
 	}
 
  if (bCondMand) {
   // If no value found and the conditional mandatory situation is fulfilled then report error 
   if (bValueFound==false && bValsEqual == true) { strCurControlError+="\n'" + strLabel + "' is a required field based on your input in another field." ; }
  } else {
   //This is a full mandatory check so no value should generate an error
   		if(bValueFound==false){
			if(inputType=="CheckboxPara"){
				strCurControlError+="\nYou must check the following to proceed - ' " + strLabel; 
			}
			else{
				strCurControlError+="\n'" + strLabel + "' is a required field."; 
			}
		}   
  }
  strError+=strCurControlError;
}

function doPoundSubstitution(strIn) {
// For client-side validation. Finds instances of &pound; and substitutes £ sign for alert box
var out = "&pound;"; // replace this
var add = "\u00A3"; // with this
var temp = "" + strIn; // temporary holder
var pos;

while (temp.indexOf(out)>-1) {
   pos= temp.indexOf(out);
   temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
}

return temp;
}

function popWindow(url, type) {
	type+="";
	var popupWindow;
   this.window.name = "app";   
   if(type=="obb"){
       popupWindow=window.open(url, "_pop", "width="+400+",height="+430+",left=10,top=50,resizable=yes,scrollbars=no,menubar=yes,location=yes,toolbar=yes,status=yes");
   }
   else  {
	   popupWindow=window.open(url, "_pop", "width="+700+",height="+530+",left=10,top=50,resizable=yes,scrollbars=yes,menubar=yes,location=yes,toolbar=yes,status=yes");
    }
	popupWindow.focus();
    return false;
}
//this is to set a conditional mandatory on a field that follows a text field
// params are field (i.e '~', the type of validation required of this field and the id of the text field in question
function valCondMandString(inData, val, textField){
	inData+="";
	textField+="";
	val+="";
	strErrorMsg="";
	
	if(client){
		var objTextField = document.getElementById(textField);
		textFieldText = getFieldValue(objTextField);
	}else{
		textFieldText = request.form(textField) + "";	
	}
	
	switch(val){
		case 'k':
			strErrorMsg = valField(inData, 'k');
			break;
		case 'a':
			strErrorMsg = valField(inData, 'a');
			break;
		
		default:
			break;
	}
	
	if((inData=="")&&(textFieldText.length>0)){
		strErrorMsg = "This is a required field.";
	
	
	}
	return strErrorMsg;

}

