﻿function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}


function CheckZip()
{
    var Zip = document.getElementById("ZipCode");
    var sZip = new String(Zip.value);
    //alert("Zip = " + Zip.value);
    if (sZip == "" || IsNumeric(sZip) == false || sZip.length < 4)
    {
        alert("A zip code is required.");
        Zip.style.backgroundColor="#cdf58a";
        Zip.focus();
        return false;
    }
    else
    {
        //return true;
    }
	
}

function sidingCheckZip()
{
    var Zip = document.getElementById("ZipCode");
    var sZip = new String(Zip.value);
    //alert("Zip = " + Zip.value);
    if (sZip == "" || IsNumeric(sZip) == false || sZip.length < 4)
    {
        alert("A zip code is required.");
        Zip.style.backgroundColor="#cdf58a";
        Zip.focus();
        return false;
    }
    else
    {
        //return true;
    }
	
    var ProjectPlan = document.getElementsByName("ProjectPlan");      
    var isGood = false;
    for (var i=0;i < ProjectPlan.length;i++)
    {
        if (ProjectPlan[i].checked)
        {
            isGood = true;
        }
        
    }
    if (isGood) 
    {
        return true;
    }
    else
    {
        alert("What type of project is this?");            
        return false;
    }	
	
}

function windowsCheckZip()
{
    var Zip = document.getElementById("ZipCode");
    var sZip = new String(Zip.value);
    //alert("Zip = " + Zip.value);
    if (sZip == "" || IsNumeric(sZip) == false || sZip.length < 4)
    {
        alert("A zip code is required.");
        Zip.style.backgroundColor="#cdf58a";
        Zip.focus();
        return false;
    }
    else
    {
        //return true;
    }
	
    var numWindows = document.getElementsByName("How many windows:");      
    var isGood = false;
    for (var i=0;i < numWindows.length;i++)
    {
        if (numWindows[i].checked)
        {
            isGood = true;
        }
        
    }
    if (isGood) 
    {
        return true;
    }
    else
    {
        alert("How many windows?");            
        return false;
    }	
	
}


function CheckServices()
{
    var Services = document.getElementsByName("SVID");      
    var isGood = false;
    for (var i=0;i < Services.length;i++)
    {
        if (Services[i].checked)
        {
            isGood = true;
        }
        
    }
    if (isGood) 
    {
       // return true;
    }
    else
    {
        alert("Please select what kind of service best describes what you're looking for.");            
        return false;
    }
	
    var HowSoon = document.getElementsByName("QSHowSoon");      
    var isGood = false;
    for (var i=0;i < HowSoon.length;i++)
    {
        if (HowSoon[i].checked)
        {
            isGood = true;
        }
        
    }
    if (isGood) 
    {
        return true;
    }
    else
    {
        alert("How soon do you want to begin the project?");            
        return false;
    }

	
}        

function CheckDetails()
{
    var SQRows = document.getElementsByTagName("TR");
    for (var x = 0;x < SQRows.length;x++)
    {
        if (SQRows[x].title == "ServiceQuestions")
        {
            if (SQRows[x].style.backgroundColor == "#cdf58a")
            {
                SQRows[x].style.backgroundColor = "";
            }
        }
    }       
    
    var Notes = new String(document.getElementById("txtNotes").value);
    
    if (Notes == "" || Notes.length < 10)
    {
        alert("Please provide a brief description of your project.");
        document.getElementById("txtNotes").style.backgroundColor="#cdf58a";
        document.getElementById("txtNotes").focus();
        return false;
    }

    
    var DropDowns = document.getElementsByTagName("SELECT");
    var Inputs = document.getElementsByTagName("INPUT");
    //alert(Inputs.length);
    
   //check dropdowns
   for (var d = 0;d < DropDowns.length; d++)
    {
           if (DropDowns[d].title == "Required")
           {
                //alert(DropDowns[d].name);
                //alert(DropDowns[d].options[DropDowns[d].selectedIndex].value);
                if (DropDowns[d].options[DropDowns[d].selectedIndex].value == "")
                {
                    alert("Please answer all the required questions.");
                    document.getElementById("tr_"+DropDowns[d].id).style.backgroundColor="#cdf58a";
                    DropDowns[d].focus();
                    return false;
                }                                                            
           }
    }
    
    var IsSet = false;       
    for (var i = 0;i < Inputs.length;i++)
    {
        //alert(Inputs[i].name);
        if (Inputs[i].title == "Required")
        {
            //Check Radio Buttons
            if (Inputs[i].type == "radio")
            {
                //get radio group
                var rg = document.getElementsByName(Inputs[i].name);
                var isGroupGood = false
                for (var rgCount = 0;rgCount < rg.length;rgCount++)
                {
                    if (rg[rgCount].checked)
                    {
                        isGroupGood = true;
                    }
                }    
                if (isGroupGood)
                {
                    IsSet = true;
                }
                                    
            }  
            //Check Textboxes
            if (Inputs[i].type == "text")
            {
                if(Inputs[i].value != "")
                {
                    //alert(Inputs[i].name);
                    IsSet = true;
                }
            }  
            //Check Checkboxes
            if (Inputs[i].type == "checkbox")
            {
                if(Inputs[i].checked)
                {
                    //alert(Inputs[i].name);
                    IsSet = true;
                }
            }              
            if (IsSet == false)
            {
                alert("Please answer all the required questions.");
                //Inputs[i].style.backgroundColor="#cdf58a";
                document.getElementById("tr_"+Inputs[i].id).style.backgroundColor="#cdf58a";
                Inputs[i].focus();
                return false;
            }
            else
            {
                IsSet = false;
            }
        }
        //alert(IsSet);
        //check our set item
    }
    
}

function emailCheck(emailaddr) {
var s=/^(.+)@(.+)\.(.+)$/;
if (emailaddr.match(s)==null) {       
	return false;       
}
return true;
}

function CheckContactInfo()
{
    var fn = document.getElementById("txtFirstName");
    fn.style.backgroundColor = "";
    var ln = document.getElementById("txtLastName");
    ln.style.backgroundColor = "";
    var address = document.getElementById("txtAddress");
    address.style.backgroundColor = "";
    var city = document.getElementById("txtCity");
    city.style.backgroundColor = "";
    var state = document.getElementById("ddlStates");
    state.style.backgroundColor = "";
    var zip = document.getElementById("txtZip");
    zip.style.backgroundColor = "";
    var email = document.getElementById("txtEmail");
    email.style.backgroundColor = "";
    var areacode = document.getElementById("txtDayPhoneArea");
    areacode.style.backgroundColor = "";
    var prefix = document.getElementById("txtDayPhonePre");
    prefix.style.backgroundColor = "";
    var suffix = document.getElementById("txtDayPhoneFour");
    suffix.style.backgroundColor = "";

    if (fn.value == "")
    {
        alert("A first name is required.");
        fn.style.backgroundColor = "#cdf58a";
        fn.focus(); 
        return false;                            
    }
    if (ln.value == "")
    {
        alert("A last name is required.");
        ln.style.backgroundColor = "#cdf58a";
        ln.focus(); 
        return false;                            
    }
    if (address.value == "")
    {
        alert("An address is required.");
        address.style.backgroundColor = "#cdf58a";
        address.focus(); 
        return false;                            
    }
    if (city.value == "")
    {
        alert("A first name is required.");
        city.style.backgroundColor = "#cdf58a";
        city.focus(); 
        return false;                            
    }
    if (state.options[state.selectedIndex].value == "")
    {
        alert("A state is required.");
        state.style.backgroundColor = "#cdf58a";
        state.focus(); 
        return false; 
    }
    var sZip = new String(zip.value);        
    if (sZip == "" || sZip.length < 4 || IsNumeric(sZip) == false)
    {
        alert("A valid zip code is required.");
        zip.style.backgroundColor = "#cdf58a";
        zip.focus(); 
        return false;                            
    }
    if (email.value == "")
    {
        alert("An email is required.");
        email.style.backgroundColor = "#cdf58a";
        email.focus(); 
        return false;                            
    }

	if (!(emailCheck(email.value))) 
	{
		alert("Please enter a valid email address.  Example: 'yourname@company.com'.");
        email.style.backgroundColor = "#cdf58a";
        email.focus(); 
		return(false);
	}

    var sAreaCode = new String(areacode.value);
    if (sAreaCode == "" || sAreaCode.length != 3 || IsNumeric(sAreaCode) == false)
    {
        alert("An phone number is required.");
        areacode.style.backgroundColor = "#cdf58a";
        prefix.style.backgroundColor = "#cdf58a";
        suffix.style.backgroundColor = "#cdf58a";
        areacode.focus(); 
        return false;                            
    }
    var sPrefix = new String(prefix.value);
    if (sPrefix == "" || sPrefix.length != 3 || IsNumeric(sPrefix) == false)
    {
        alert("A phone number is required.");
        areacode.style.backgroundColor = "#cdf58a";
        prefix.style.backgroundColor = "#cdf58a";
        suffix.style.backgroundColor = "#cdf58a";
        prefix.focus(); 
        return false;                            
    }
    var sSuffix = new String(suffix.value);
    if (sSuffix == "" || sSuffix.length != 4 || IsNumeric(sSuffix) == false)
    {
        alert("A phone number is required.");
        areacode.style.backgroundColor = "#cdf58a";
        prefix.style.backgroundColor = "#cdf58a";
        suffix.style.backgroundColor = "#cdf58a";
        suffix.focus(); 
        return false;                               
    }
    
    //return false;
}