﻿
var FBCurrentSiteName;
var FBResultLableId;
var FBErrorLabel;
var FBFNameId;
var FBLNameId;
var FBAddressId;
var FBCityId;
var FBStateId;
var FBZipId;
var FBPhoneId;
var FBEmailId;
var FBCommentsId;
var FBCommentsTypeId;
var FBServiceId;
var FBFeelTypeId;
var FBRefereScaleId;
var FBErrorMessage='';
var FBDayPhoneAreaId;
var FBDayPhoneExchId;
var FBDayPhoneNumId;
var sTempMessage='';

function validateFeedback(){
    FBErrorMessage='';
    var ControlToFocus;
    var ErrorCtlList='';
    document.getElementById(FBErrorLabel).innterText=FBErrorMessage;
    if(Trim(document.getElementById(FBCommentsId).value)==''){
        FBErrorMessage+=' - Comments<br/>';
        ControlToFocus=document.getElementById(FBCommentsId);
        ErrorCtlList+=FBCommentsId + "|";
    }
    if(Trim(document.getElementById(FBFNameId).value)==''){
        FBErrorMessage+=' - First Name<br/>';
        ControlToFocus=document.getElementById(FBFNameId);
        ErrorCtlList+=FBFNameId + "|";
    }
    if(Trim(document.getElementById(FBLNameId).value)==''){
        FBErrorMessage+=' - Last Name<br/>';
        ControlToFocus=document.getElementById(FBLNameId);
        ErrorCtlList+=FBLNameId + "|";
    }
    if(Trim(document.getElementById(FBAddressId).value)==''){
        FBErrorMessage+=' - Address<br/>';
        ControlToFocus=document.getElementById(FBAddressId);
        ErrorCtlList+=FBAddressId + "|";
    }
    if(Trim(document.getElementById(FBCityId).value)==''){
        FBErrorMessage+=' - City<br/>';
        ControlToFocus=document.getElementById(FBCityId);
        ErrorCtlList+=FBCityId + "|";
    }
    if(Trim(document.getElementById(FBStateId).value)==''){
        FBErrorMessage+=' - State<br/>';
        ControlToFocus=document.getElementById(FBStateId);
        ErrorCtlList+=FBStateId + "|";
    }
    if(Trim(document.getElementById(FBZipId).value)==''){
        FBErrorMessage+=' - ZIP<br/>';
        ControlToFocus=document.getElementById(FBZipId);
        ErrorCtlList+=FBZipId + "|";
    }
    if(Trim(document.getElementById(FBDayPhoneAreaId).value)==''){
        FBErrorMessage+=' - Day Phone Area Code<br/>';
        ControlToFocus=document.getElementById(FBDayPhoneAreaId);
        ErrorCtlList+=FBDayPhoneAreaId + "|";
    }
    if(Trim(document.getElementById(FBDayPhoneExchId).value)==''){
        FBErrorMessage+=' - Day Phone Exchange<br/>';
        ControlToFocus=document.getElementById(FBDayPhoneExchId);
        ErrorCtlList+=FBDayPhoneExchId + "|";
    }
    if(Trim(document.getElementById(FBDayPhoneNumId).value)==''){
        FBErrorMessage+=' - Day Phone Suffix<br/>';
        ControlToFocus=document.getElementById(FBDayPhoneNumId);
        ErrorCtlList+=FBDayPhoneNumId + "|";
    }
    if (document.getElementById(FBDayPhoneAreaId).value != "" && document.getElementById(FBDayPhoneExchId).value != "" && document.getElementById(FBDayPhoneNumId).value != ""){
		if(!IsPhoneNumberGood(document.getElementById(FBDayPhoneAreaId),3)){
		    FBErrorMessage+=' - Day Phone Area Code must be 3 digits<br/>';
            ControlToFocus=document.getElementById(FBDayPhoneAreaId);
            ErrorCtlList+=FBDayPhoneAreaId + "|";
		}
		if(!IsPhoneNumberGood(document.getElementById(FBDayPhoneExchId),3)){
		    FBErrorMessage+=' - Day Phone Exchange must be 3 digits<br/>';
            ControlToFocus=document.getElementById(FBDayPhoneExchId);
            ErrorCtlList+=FBDayPhoneExchId + "|";
		}
		if(!IsPhoneNumberGood(document.getElementById(FBDayPhoneNumId),4)){
		    FBErrorMessage+=' - Day Phone Suffix must be 4 digits<br/>';
            ControlToFocus=document.getElementById(FBDayPhoneNumId);
            ErrorCtlList+=FBDayPhoneNumId + "|";
		}
	}
    if(Trim(document.getElementById(FBEmailId).value)==''){
        FBErrorMessage+=' - E-mail<br/>';
        ControlToFocus=document.getElementById(FBEmailId);
        ErrorCtlList+=FBEmailId + "|";
    }
    if(!IsQuestionAnswerd(FBCommentsTypeId)){
        FBErrorMessage+=' - Compliment/Complaint/Other<br/>';
        ControlToFocus=document.getElementById(FBCommentsTypeId);
        ErrorCtlList+=FBCommentsTypeId + "|";
    }
    if(!IsQuestionAnswerd(FBServiceId)){
        FBErrorMessage+=' - Service received<br/>';
        ControlToFocus=document.getElementById(FBServiceId);
        ErrorCtlList+=FBServiceId + "|";
    }
    if(!IsQuestionAnswerd(FBFeelTypeId)){
        FBErrorMessage+=' - How do you feel about your service<br/>';
        ControlToFocus=document.getElementById(FBFeelTypeId);
        ErrorCtlList+=FBFeelTypeId + "|";
    }
    if(!IsRecommentQuestionAnswerd(FBRefereScaleId)){
        FBErrorMessage+=' - On a scale of 1-10 how likely would you be to recommend us to a friend<br/>';
        ControlToFocus=document.getElementById(FBRefereScaleId);
        ErrorCtlList+=FBServiceId + "|";
    }
    if(FBErrorMessage==''){
        if(!IsZipCode(document.getElementById(FBZipId))){
            FBErrorMessage+=sTempMessage + '<br/>';
            ControlToFocus=document.getElementById(FBZipId);
            ErrorCtlList+=FBZipId + "|";
        }
        else if(!IsPhone(document.getElementById(FBDayPhoneAreaId).value + document.getElementById(FBDayPhoneExchId).value + document.getElementById(FBDayPhoneNumId).value))
        {
            FBErrorMessage+=sTempMessage + '<br/>';
            ControlToFocus=document.getElementById(FBDayPhoneExchId);
            ErrorCtlList+=FBDayPhoneExchId + "|";
        }
        var sError=isEmail(document.getElementById(FBEmailId));
        if(sError!=''){
            FBErrorMessage+=sError + '<br/>';
            ControlToFocus=document.getElementById(FBEmailId);
            ErrorCtlList+=FBEmailId + "|";
        }
    }
    if(FBErrorMessage!=''){
        document.getElementById('lblErrorInfo').innerHTML='Please specify a valid value for the following:<br/>' + FBErrorMessage;
        //ControlToFocus.focus();
        if(ErrorCtlList!=''){
            var ctlAll=ErrorCtlList.split('|');
            document.getElementById(ctlAll[0]).focus();
            window.scroll(0,0);
        }
        return false;
    }
    return true;
}
function IsQuestionAnswerd(QuestCtlId){
    var navRoot = document.getElementById(QuestCtlId);
    var iCount=navRoot.rows.length;
    var bChecked=false;
    if(navRoot!=null){
        for(var iIndex=0;iIndex<navRoot.rows.length;iIndex++)
        {
            var oTag=navRoot.rows[iIndex].firstChild.firstChild;
            if(oTag.type=="radio")
            {
                if(oTag.checked){
                    bChecked=true;
                    break;
                }
            }
        }
    }
    return bChecked;
}
function IsRecommentQuestionAnswerd(QuestCtlId){
    var navRoot = document.getElementById(QuestCtlId);
    var iCount=navRoot.children.length;
    var bChecked=false;
    if(navRoot!=null){
        for(var iIndex=0;iIndex<navRoot.children.length;iIndex++)
        {
            var oTag=navRoot.children[iIndex];
            if(oTag.type=="radio")
            {
                if(oTag.checked){
                    bChecked=true;
                    break;
                }
            }
        }
    }
    return bChecked;
}
function Trim(textValue) {
    // remove from end
    while (''+textValue.charAt(textValue.length - 1) == ' ') {
        textValue = textValue.substring(0, textValue.length - 1);  
    }

    // remove from beginning
    while (''+textValue.charAt(0) == ' ') {
        textValue = textValue.substr(1);
    }
    
    return textValue;
}
function IsPhoneNumberGood(oPhoneControl,maxLen){
    var PhoneValue = Trim(oPhoneControl.value);
    if(PhoneValue.length<maxLen){
        return false;
    }
    else if(PhoneValue.indexOf('-')!=-1){
        return false;
    }
    return true;
}
function IsPhone(oPhoneControl){
    var valid = "0123456789";
    sTempMessage='';
    var PhoneValue = Trim(oPhoneControl);
    if(PhoneValue.length<10){
        sTempMessage=' - Phone number must be 10 digits';
        return false;
    }
    else if(PhoneValue.indexOf('-')!=-1){
        sTempMessage=' - Phone number can not have special characters';
        return false;
    }
    else if (PhoneValue=='0000000000' || PhoneValue=='1111111111' || PhoneValue=='2222222222' || PhoneValue=='3333333333' || PhoneValue=='4444444444' || PhoneValue=='5555555555' || 
                                PhoneValue=='6666666666' || PhoneValue=='7777777777' || PhoneValue=='8888888888' || PhoneValue=='9999999999')
    {
        sTempMessage=' - Phone number can not be a number sequence';
        return false;
    }
    return true;
}
//Validate Zip code and display relevent error message if appliable.
function IsZipCode(ZipControl)
{
    var valid = "0123456789";
    var zipCode = ZipControl;// document.getElementById(fieldname);        
    
    var fieldValue = zipCode.value;
    sTempMessage=''
    if (fieldValue.length<5)
    {
        sTempMessage=' - Please make sure your ZIP code is 5 digits';
        return false;
    }
    else if (fieldValue.length==5 && (fieldValue=='00000' || fieldValue=='11111' || fieldValue=='22222' || fieldValue=='33333' || fieldValue=='44444' || fieldValue=='55555' || 
                                fieldValue=='66666' || fieldValue=='77777' || fieldValue=='88888' || fieldValue=='99999'))
    {
        sTempMessage=' - Please enter a valid ZIP code';
        return false;
    }
    else{
        // return error if zip code is formatted incorrectly
        for (var i=0; i < fieldValue.length; i++)
        {
            fieldString = fieldValue.substring(i, i+1);

            // return error if the character is not a digit or hyphen
            if (valid.indexOf(fieldString) == "-1") {
                sTempMessage=' - Your ZIP code must contain only numbers';
	            return false;
            }

            // return error if the character is a hyphen, but it is not in position 6
            if (fieldString == "-") {
                if (i != 5) {
                    sTempMessage='Please place the dash between the fifth and sixth digits of a ten digit ZIP code';
		            return false;
                }
            }

            // return error if there are 10 characters and the 6th is not a hyphen
            if ((fieldValue.length==10) && fieldValue.charAt(5)!="-") {
                sTempMessage='Please place the dash between the fifth and sixth digits of a ten digit ZIP code';
	            return false;
            }
        }
    }
    return true;
}