/**
 *
 *  Name:       nbtd.js
 *  Company:    IBM BCS
 *  @author     Simon Hudson
 *  @version    $Revision:$ $Date:$
 **/
 
 
function alertNotAvailable()
{
    alert ("This function is not available in this demo.");
}

/*
 *  Function: validateForm
 *  Input: oForm - form to be validated
 *  Description:
 *  This function will provide a standard validation for all test drive forms.  The function
 *  will return true if all actual values meet the expected values defined in the page.
 *  The function will provide an alert message, and then return false after finding the first
 *  unmatching element.
 * 
 *  Note:
 *  Form inputs should be set up as follows
 *   <input type="xxxx" lang="[true|false]" id="[expected]" title="[description]">
 *   lang -> if lang is set to true then this input will be ignored, if false then it will be validated
 *   id -> if the user input does not match this field then the validation will fail
 *   title -> the field description to be used in alert messages
 */
function validateForm(oForm)
{
    for (var i=0; i< oForm.elements.length; i++)
	{
        var oInput = oForm.elements[i]; 
				
		if(oInput.tagName == 'INPUT' && (oInput.type == 'text' || oInput.type == 'password' )&& oInput.lang != "true")
		{
            if (
                (oInput.value.toUpperCase() != oInput.id.toUpperCase()) && 
                (oInput.value != (oInput.id + ".00")) && 
                ((oInput.value + ".00") != oInput.id)
               )
			{
			    alert("Please enter the text '" + oInput.id + "' into the '" + oInput.title + "' field.");
			    oInput.focus();
				return false;				
			}    
		}else if(oInput.tagName == 'TEXTAREA' && oInput.lang != "true")
		{
            if (oInput.value != oInput.id)
			{
			    alert("Please enter the text '" + oInput.id + "' into the '" + oInput.title + "' field.");
			    oInput.focus();
				return false;				
			}    
		}else if(oInput.tagName == 'INPUT' && oInput.type == 'radio' && oInput.lang != "true")
		{
		    if((oInput.checked + "") != oInput.id)
			{
			    if( oInput.id + "" == "true" )
				    alert("Please check the '" + oInput.title + "' field.");
				else
				    alert("Please uncheck the '" + oInput.title + "' field.");
	
			    oInput.focus();
				return false;			   
			}
		}else if(oInput.tagName == 'SELECT' && oInput.lang != "true")
		{
		    if (oInput.options[oInput.selectedIndex].text != oInput.id)
			{
			    alert("Please select the '" + oInput.id + "' option in the '" + oInput.title + "' dropdown.");
			    oInput.focus();
				return false;
			}
		}else if(oInput.tagName == 'INPUT' && oInput.type == 'checkbox' && oInput.lang != "true")
		{
		    if((oInput.checked + "") != oInput.id)
			{
			    if( oInput.id + "" == "true" )
				    alert("Please check the '" + oInput.title + "' field.");
				else
				    alert("Please uncheck the '" + oInput.title + "' field.");
	
			    oInput.focus();
				return false;			   
			}
		}
	}
	return true;
}

 
function windowRedirect(URL) {
    window.location = URL;
}


/* 
 * Function: FormValidateAndRedirect
 
 * Input: - formid (this is the id of the form to be validated)
          - URL (this is the URL to redirect to once the form is valid)
          
 * REQUIREMENTS: If an element is being validated it must contain a label with the same
   ID of its validation element and have a suffix of "_Label". e.g - if you required the
   textbox TXTName to be validated, create a label with the ID TXTName_Label.
   
 * Description: This function validates the specified form in a top-to-bottom fashion.
   It validates each field by comparing its value to the elements title. Each validation
   message is displayed in an alert. For required checkboxes it checks to see if it is checked
   if not then it alerts the corresponding alert message
 */

function FormValidateAndRedirect(formid,URL) {

var v = new Validator();

// Validator object
function Validator() {
    this.IsNotTitle = IsNotTitle;
}

// Validator methods
function IsNotTitle(val, title) {
    if(!isNaN(title)) {
        var titleNumber = parseFloat(title);
    }
    if(val != title && val != titleNumber){
        return true;
    }
    return false;
}

var formToValidate = document.getElementById(formid);
var elementClasses = new Array();
var elementType;


var result;

for(i=0;i<formToValidate.elements.length;i++) {

    elementClasses[i] = formToValidate.elements[i]['className'].split(" ");

}

    //loops through each form elements classes [each elementClasses array item]
    for(element=0;element<elementClasses.length;element++) {
        
        //loops through each class of the current element in the loop [each SUB-ARRAY item of the elementClasses array]
        for(cssClass=0;cssClass<elementClasses[element].length;cssClass++) {

            //if the element has a class of requiredField do the following
            if(elementClasses[element][cssClass] == "requiredField" && formToValidate.elements[element]['type'] != "checkbox") {  
                    result = v.IsNotTitle(formToValidate.elements[element].value.toLowerCase(),formToValidate.elements[element].title.toLowerCase());
                    elementType = formToValidate.elements[element];             
            }//end if(elementClasses[element][cssClass] == "requiredField")
            
            if(elementClasses[element][cssClass] == "requiredField" && formToValidate.elements[element]['type'] == "checkbox") {  
                    if(!formToValidate.elements[element].checked) {
                        result = true;
                    }             
            }//end if(elementClasses[element][cssClass] == "requiredField")
            
            //CSSClass specific to IMT Currency validation
            if(elementClasses[element][cssClass] == "requiredFieldValidateCurrency"){
                result = IMT_PaymentDetailsValidateCurrencyValues();
                
                if(result==false)
                {
                    return true;
                }
                
                result = false;
            }
            
            //CSSClass specific to IMT Benificiary's BIC validation
            if(elementClasses[element][cssClass] == "requiredFieldValidateBIC"){
            
                if(!IMT_IsBankSearchComplete){
                    alert("Please enter 'GENODEF1S11' in the BIC/SWIFT field and click on 'Search for this bank'.");
                    return false;
                }
            
                result = IMT_SearchForThisBank();
                
                if(result==false)
                {
                    return true;
                }
                
                result = false;
            }
            
            if(result == false) {
                continue;
            }
            
            if(result == true && formToValidate.elements[element].tagName == "SELECT") {
                var myLabel = document.getElementById(formToValidate.elements[element].id + "_Label").innerHTML;
                alert("Please select the '" + formToValidate.elements[element].title + "' option in the '" + myLabel + "' dropdown.");
                formToValidate.elements[element].focus();
                result = false;
                return false;
            }
            
            if(result == true && formToValidate.elements[element].tagName == "INPUT" && formToValidate.elements[element]['type'] == "text" || formToValidate.elements[element]['type'] == "password") {
                var myLabel = document.getElementById(formToValidate.elements[element].id + "_Label").innerHTML;
                alert("Please enter the text '" + formToValidate.elements[element].title + "' into the '" + myLabel + "' field.");
                formToValidate.elements[element].focus();
                result = false;
                return false;
            }
            
            if(result == true && formToValidate.elements[element].tagName == "TEXTAREA") {
                var myLabel = document.getElementById(formToValidate.elements[element].id + "_Label").innerHTML;
                alert("Please enter the text '" + formToValidate.elements[element].title + "' into the '" + myLabel + "' field.")
                formToValidate.elements[element].focus();
                result = false;
                return false;
            }
            
            if(result == true && formToValidate.elements[element].tagName == "INPUT" && formToValidate.elements[element]['type'] == "checkbox") {
                var myLabel = document.getElementById(formToValidate.elements[element].id + "_Label").innerHTML;
                alert("Please check the '" + formToValidate.elements[element].title + "' field.");
                formToValidate.elements[element].focus();
                result = false;
                return false;
            }
            
            
        }//end for(cssClass=0)
        
       

    }//end for(element=0)
    window.location = URL;
    return false;
    
}


/* 
 * Function: IMT_PaymentDetailsValidateCurrencyValues
    
 * Description: This function compares the values of 2 inputs against the 2 values input into the function (i.e - value1, value2)
 */

function IMT_PaymentDetailsValidateCurrencyValues(BtnClicked) {

    var input1 = document.getElementById("TXTAmount1");
    var input1Label = document.getElementById("TXTAmount1_Label");
    
    var input2 = document.getElementById("TXTAmount2");
    var input2Label = document.getElementById("TXTAmount2_Label");
    
    var toggleElement = document.getElementById("amountToggle");
    
    var userInput1 = new String(input1.value);
    userInput1 = userInput1.split(",");
    userInput1 = userInput1.join("");
    userInput1 = parseFloat(userInput1);
    var userInput2 = new String(input2.value);
    userInput2 = userInput2.split(",");
    userInput2 = userInput2.join("");
    userInput2 = parseFloat(userInput2);

    if(userInput1 != "590" && userInput2 != "1000.00") {
        alert("Please enter either '590.00' in the '" + input1Label.innerHTML + "' field or '1000.00' in the '" + input2Label.innerHTML + "' field and click on 'Calculate'.");
        toggleElement.innerHTML = "";
        return false;
    } 
    if(BtnClicked != "BTNCalculate") {
        if(userInput1 == "590" && userInput2 != "1000" || userInput1 != "590" && userInput2 == "1000" ) {
            alert("Please click on 'Calculate'");
            return false;
        }
    }
    input2.value = "1000.00";
    input1.value = "590.00";
    toggleElement.innerHTML = "1,022.00";
    toggleElement.style.display = "block";
    return true;
}

var IMT_IsBankSearchComplete = false;

function IMT_SearchForThisBank(BtnClicked) {
    var input = document.getElementById("TXTBIC");
    var toggleElement = document.getElementById("toggleBankDetails");
    
    if(BtnClicked == "BTNSearch" && input.value != input.title) {
        alert("Please enter 'GENODEF1S11' in the 'BIC/SWIFT' field.");
        input.focus();
        toggleElement.style.display = "none";
        return false;
    }
    if(input.value != input.title) {
        alert("Please enter 'GENODEF1S11' in the BIC/SWIFT field and click on 'Search for this bank'. ");
        input.focus();
        toggleElement.style.display = "none";
        return false;
    }    
    else {
        toggleElement.style.display = "block";
        IMT_IsBankSearchComplete = true;
        return true;
    }
}


/* 
 * Function: clearForm
    
 * Description: This function clears all textbox elements within a form
 */
 
 function clearForm(elementToClear,optionalElement) {
    var elementToClear = document.getElementById(elementToClear);
    var optionalElement = document.getElementById(optionalElement);
    var inputElements = elementToClear.getElementsByTagName("input");
    var textBoxes = new Array();
    var count=0;
    
    for(var x=0;x<inputElements.length;x++) {
        if(inputElements[x]['type'] == "text") {
            textBoxes[count] = inputElements[x];
            count += 1;
        }
    }
    
    if(elementToClear) {
        for(var i=0;i<textBoxes.length;i++) {
            textBoxes[i].value = "";
        }
    }
    optionalElement.innerHTML = "";
 
 }
 
 

