﻿/*
    Name            :       validation.js
    Purpose         :       Handles all of the form validation
    Author          :       JohnD (JJB)
    Date            :       3/10/2007
    Copyright       :       JJB © 2007
    Notes           :       Validation done with regular expression
                            http://regexlib.com
                            
                            Using Regular Expressions in Javascript
                            http://www.regular-expressions.info/javascriptexample.html
*/

/*
     Name        :   validateForm
     Purpose     :   Performs the validation on the form after the user has clicked on the Send button
     Author      :   JohnD (SIDW)
     Date        :   3/11/2007
*/
function validateForm() {
            var blnValid = true; //Assume everything is correctly entered by the end user
            var strErrorMsg = "The following values are invalid:\n";
            var strErrorBody = "";
            //Lender Informtion
            if (document.getElementById('Tabs_tplLender_txtOrderedbyLender').value.length == 0) {
                strErrorBody = "Ordered By\n";
                
            }
            if (document.getElementById('Tabs_tplLender_txtAddressLender').value.length == 0) {
                strErrorBody += "Address\n";
                
            }
            if (document.getElementById('Tabs_tplLender_txtCityLender').value.length == 0) {
                strErrorBody += "City\n";
                
            }
            if (document.getElementById('Tabs_tplLender_ddlStatesLender').selectedIndex == 0) {
                strErrorBody +=  "State\n";
                
            }
            /*
            if (document.getElementById('Tabs_tplLender_txtZipCodeLender').value.length == 0) {
                strErrorBody += "Zip Code\n";
            } else if (document.getElementById('Tabs_tplLender_txtZipCodeLender').value.length > 0) {
                if (!validateZip(document.getElementById('Tabs_tplLender_txtZipCodeLender'),-1,'Tabs_tplLender_txtZipCodeLender')) {
                    strErrorBody += "Zip Code\n";
                
                }
            }
            */
            if (document.getElementById('Tabs_tplLender_txtPhoneLender').value.length == 0) {
                    strErrorBody += "Invalid phone number, please include the area code and the 7 digit number.\n";
            } else if (document.getElementById('Tabs_tplLender_txtPhoneLender').value.length > 0) {
                if (!validatePhoneFax(document.getElementById('Tabs_tplLender_txtPhoneLender'),1,-1,'Tabs_tplLender_txtPhoneLender')) {
                    strErrorBody += "Invalid phone number, please include the area code and the 7 digit number.\n";;
                
                }
            }
            //Check to see if there were any errors in the Lender Information section.
            if (strErrorBody != "") {
                strErrorMsg +=  "\n----------------------------------------\n" + "Requester Information\n" + "----------------------------------------\n" +  strErrorBody;
                strErrorBody = "";
                blnValid=false;
            }
            
            //Property Section
            if (document.getElementById('Tabs_tplProperty_txtStreetAddrProperty').value.length == 0) {
                strErrorBody = "Street Address\n";
            }
            /*
            if (document.getElementById('Tabs_tplProperty_txtAddress2Property').value.length == 0) {
                strErrorBody += "Address (contd.)\n";
            }
            */
            if (document.getElementById('Tabs_tplProperty_txtCityProperty').value.length == 0) {
                strErrorBody += "City\n";
            }
            if (document.getElementById('Tabs_tplProperty_ddlStateProvinceProperty').selectedIndex == 0) {
                strErrorBody += "State\n";
            }
            /*
            if (document.getElementById('Tabs_tplProperty_txtZipPostalProperty').value.length == 0) {
                strErrorBody += "Zip Code\n";
            } else if (document.getElementById('Tabs_tplProperty_txtZipPostalProperty').value.length > 0) {
                if (!validateZip(document.getElementById('Tabs_tplProperty_txtZipPostalProperty'),-1,'Tabs_tplProperty_txtZipPostalProperty')) {
                    strErrorBody += "Zip Code\n";
                }
            }
            */
            if (document.getElementById('Tabs_tplProperty_ddlPropertyType').selectedIndex == 0) {
                strErrorBody += "Property Type\n";
            }

            //Check to see if there were any errors in the Property Information section.
            if (strErrorBody != "") {
               strErrorMsg +=  "\n----------------------------------------\n" + "Property Information\n" + "----------------------------------------\n" +  strErrorBody;
                strErrorBody = "";
                blnValid=false;
            }
            //Borrower Section
            /*
            if (document.getElementById('Tabs_tplBorrower_ddlBorrowerType1').selectedIndex ==0) {
                strErrorBody += "Contact Type\n";
            }*/
            if (document.getElementById('Tabs_tplBorrower_txtBorrowerName1').value.length == 0) {
                strErrorBody += "Name\n";
            } 
            if (document.getElementById('Tabs_tplBorrower_txtBorrowerWp1').value.length == 0) {
                strErrorBody += "Work Phone\n";
            } 
            
           
          /*  
            var intBorrowers = parseInt(document.getElementById('Tabs_tplBorrower_hidBorrowerNumber').value);
            
            if (intBorrowers >=2) {
            
                //Check to see if any of the name fields have values
                for (var intBorrower=2;intBorrower<=4;intBorrower++) {
                        if (document.getElementById('Tabs_tplBorrower_txtBorrowerName' + intBorrower).value.length > 0 || document.getElementById('Tabs_tplBorrower_ddlBorrowerType' + intBorrower).selectedIndex > 0) {
                            
                            if (document.getElementById('Tabs_tplBorrower_txtBorrowerWp' + intBorrower).value.length == 0 || document.getElementById('Tabs_tplBorrower_ddlBorrowerType' + intBorrower).selectedIndex == 0) {
                                strErrorBody += "You need to have work phone number, and also the contact type\n";
                                break;
                            }
                        }
                }
            }
            */
            
            //Check to see if there were any errors in the Borrower Information section.
            if (strErrorBody != "") {
               strErrorMsg +=  "\n----------------------------------------\n" + "Borrower Information\n" + "----------------------------------------\n" +  strErrorBody;
                strErrorBody = "";
                blnValid=false;
            }
            
            //Determine if we need to display the error message or not
            if (!blnValid) {
                alert(strErrorMsg);
                
            }
     //Return the state of the function to the calling button            
     return blnValid;
}




//Regular Expressions pulled from regexlib.com unless stated
// Validation - Regular Expressions
/*
    Name            :       validateDateFormat
    Purpose         :       Validates the date format entered by the user.
    Author          :       JohnD (JJB)
    Date            :       3/10/2007
*/
function validateDateFormat(objfld,intMode,strControlId) {
    //Local variables
    var blnValid = true;
    //Check to see if any value was entered or not
    if (objfld.value.length >0) {
        //Validate the contents of the field
        var objRegExpr = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{4}$/);
        blnValid = objfld.value.match(objRegExpr)
        //Check to see if any message needs to be returned or no
        if (!blnValid) {
            //Invalid date entered
            if (intMode==1) {
                alert("Invalid date entered, please try again.");
                
                document.getElementById(strControlId).focus();
                document.getElementById(strControlId).select();
            }
            blnValid=false;
        } else {
            blnValid=true;
        }
        
    }
    return blnValid;
}

/*
    Name            :       validateEmail
    Purpose         :       Validates the email entered by the user
    Author          :       JohnD (JJB)
    Date            :       3/10/2007
*/
function validateEmail(objfld,intMode,strControlId) {
//Local variables
    var blnValid = true;
    
    //Check to see if any value was entered or not
    if (objfld.value.length >0) {
        //Validate the contents of the field
        var objRegExpr = new RegExp(/^.+@[^\.].*\.[a-z]{2,}$/);
        blnValid = objfld.value.match(objRegExpr);
        //Check to see if you display the error message or not    
        if (!blnValid) {
            //Invalid email address
            if (intMode==1) {
                alert("Invalid email address entered, please try again.");
                
                document.getElementById(strControlId).focus();
                document.getElementById(strControlId).select();
            }
            blnValid=false;
        } else {
            blnValid=true;
        }
    }
    return blnValid;
}
/*
    Name            :       validatePhoneFax
    Purpose         :       Validates the phone/fax entered by the user
    Author          :       JohnD (JJB)
    Date            :       3/10/2007
*/
function validatePhoneFax(objfld,intPhnFax,intMode,strControlId) {
    //Local variables
    var blnValid = true;
    //Check to see if any value was entered or not
    if (objfld.value.length >0) {
        //Validate the contents of the field
        var objRegExpr = new RegExp(/^(\(\d{3}\) \d{3}-\d{4}|\d{3}-\d{3}-\d{4}|\d{10})$/);
        blnValid = objfld.value.match(objRegExpr);
        //alert(strPhoneRegExpr + blnValid);
        //Check to see if you display the error message or not        
        if (blnValid == null) {
          //Invalid Phone/Fax
           if (intMode==1) {
                var strError = "";
                if (intPhnFax==1) {
                    strError = "Invalid phone number, please include the area code and the 7 digit number.";
                } else {
                    strError = "Invalid phone number, please include the area code and the 7 digit number.";
                }
                alert(strError);
                
                document.getElementById(strControlId).focus();
                document.getElementById(strControlId).select();
            }
            blnValid=false;
        } else {
            blnValid=true;
        }
    }
    return blnValid;
}

/*
    Name            :       validateZip
    Purpose         :       Validates the zip entered by the user
    Author          :       JohnD (JJB)
    Date            :       3/10/2007
*/
function validateZip(objfld,intMode,strControlId) {
//Local variables
    var blnValid = true;
    //Check to see if any value was entered or not
    if (objfld.value.length >0) {
        //Validate the contents of the field
        var objRegExpr = new RegExp(/^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$/);
        blnValid = objfld.value.match(objRegExpr);
        //check to see if you display the error message or not        
        if (!blnValid) {
            if (intMode==1) {
                alert("Invalid zip/postal code, please try again.");
                document.getElementById(strControlId).focus();
                document.getElementById(strControlId).select();
            }
            blnValid=false;
        } else {
            blnValid=true;
        }
    }
    return blnValid;
}


  function activeTabClient(sender, e) {
  
  
        var strCurrentTabText = sender.get_activeTab().get_headerText();
        
        
        
        if (strCurrentTabText.indexOf("Lender") > -1) {
            document.getElementById('Tabs_tplLender_txtOrderedbyLender').focus();
        } else if (strCurrentTabText.indexOf("Property") > -1) {
            document.getElementById('Tabs_tplProperty_txtStreetAddrProperty').focus();
        } else if (strCurrentTabText.indexOf("Borrower") > -1) {
            document.getElementById('Tabs_tplBorrower_txtBorrowerName1').focus();
        }  else if (strCurrentTabText.indexOf("Officer/Contact") > -1) {
            document.getElementById('Tabs_tplLoanOfficer_txtLoanOfficer').focus();
        } else if (strCurrentTabText.indexOf("Loan") > -1) {
            document.getElementById('Tabs_tplLoan_txtLoanNum').focus();
        }  else if (strCurrentTabText.indexOf("Financ") > -1) {
            document.getElementById('Tabs_tplFinance_txtSalesPrice').focus();
        } else if (strCurrentTabText.indexOf("Realtor") > -1) {
            document.getElementById('Tabs_tplRealtor_txtRIName').focus();
            
        }

  displayBorrowerContact();
     
}   

function displayBorrowerContact() {
if (document.getElementById('btnAddBorrower') != null) {
var intBorrowerContactNum = parseInt(document.getElementById('Tabs_tplBorrower_hidBorrowerNumber').value);

    if (intBorrowerContactNum == 4) {
        document.getElementById('btnAddBorrower').style.display = "none";
    }
    
   
   
    for (i=1;i<=intBorrowerContactNum;i++) {
        if (i <5) {
        document.getElementById('Tabs_tplBorrower_trBorrower' + i).style.display = "inline";
        }
    }
 }
}


function NextTab(sender,e) {
/*
alert("jrd");


    
    if (document.getElementById('Tabs_tplLender').style.display=="none") {
        
        document.getElementById('Tabs_tplLender').style.display=="block"
    }
    
    return false;
    */
    
}
        
 // JavaScript Functions Written by:
     //    Scott Mitchell
     //    mitchell@4guysfromrolla.com
     //    http://www.4GuysFromRolla.com

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

function FormatPercent(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.										
 
	RETVAL:
		The formatted number!		
 **********************************************************************/
{
   if (num.indexOf("NaN") > -1) {
        num = num.replace("NaN","");
        
    }
       if (num.indexOf("%") > -1) {
        num = num.replace("%","");
        
    }
	var tmpStr = new String(FormatNumber(num*100,decimalNum,bolLeadingZero,bolParens,bolCommas));

	if (tmpStr.indexOf(")") != -1) {
		// We know we have a negative number, so place '%' inside of ')'
		tmpStr = tmpStr.substring(0,tmpStr.length - 1) + "%)";
		return tmpStr;
	}
	else
		return tmpStr + "%";			// Return formatted string!
}

function FormatCurrency(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.										
 
	RETVAL:
		The formatted number!		
 **********************************************************************/
{

    if (num.indexOf("$") > -1) {
        num = num.replace("$","");
    }
    if (num.indexOf("NaN") > -1) {
        num = num.replace("NaN","");
        
    }

	var tmpStr = new String(FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas));

	if (tmpStr.indexOf("(") != -1 || tmpStr.indexOf("-") != -1) {
		// We know we have a negative number, so place '$' inside of '(' / after '-'
		if (tmpStr.charAt(0) == "(")
			tmpStr = "($"  + tmpStr.substring(1,tmpStr.length);
		else if (tmpStr.charAt(0) == "-")
			tmpStr = "-$" + tmpStr.substring(1,tmpStr.length);
			
		return tmpStr;
	}
	else
	
		return "$" + tmpStr;		// Return formatted string!
}

/*
        Name            :           addBorrowerContact
        Purpose         :           Adds a new row to the borrower/contact table
        Author          :           JohnD (JJB)
        Date            :           5/26/2007
    */
    function addBorrowerContact() {
        //get the current number of rows displayed
        var intBorrowerContactNumber = parseInt(document.getElementById('Tabs_tplBorrower_hidBorrowerNumber').value) + 1;
        
  
        //Check to see if you are greater than the available rows
        if (intBorrowerContactNumber < 5) {
            
            //Display the next available row
            document.getElementById('Tabs_tplBorrower_trBorrower' + intBorrowerContactNumber).style.display = "inline";
            document.getElementById('Tabs_tplBorrower_hidBorrowerNumber').value = intBorrowerContactNumber;
            //Check to see if you are on the last available row
            if (intBorrowerContactNumber == 4) {
                //Hide the add button
                document.getElementById('btnAddBorrower').style.display = "none";
 
            }
        }
        
        
        
    }        
