// JScript File
var HILITECOLOR = "#FFCC33"; //"#EFB549"; //"EE9A04"
var ie = (navigator.appName.indexOf("Explorer") > -1);

function OnChangeFormField(o, hiddenField)
{
    o.style.backgroundColor = (o.value=="")? HILITECOLOR : "";
    
    if (typeof(hiddenField) != "undefined")
    {
        document.getElementById(hiddenField).value = o.value;
    }
}

function OnClickContactPreference()
{
    var oDay = document.getElementById("day");
    var oEvening = document.getElementById("evening");
    
    var value = 0;
    
    value = (oDay.checked) ? oDay.value : 0;
    value = (oEvening.checked) ? (parseInt(value)+parseInt(oEvening.value)) : value;
    
    var o = document.getElementById("ContactPreference");
    o.value = value;
    
    ValidateContactPreference();
}

function SetPhone()
{
    var o = document.getElementById("Phone");
    o.value = document.getElementById("arecode").value + "-" + document.getElementById("phone1").value + "-" + document.getElementById("phone2").value;
}


function OnChangeState(o)
{
    var oState = document.getElementById("State");
    oState.value = o.value;
    
    ValidateFormField("ddlState");
}

function OnChangeInitialInvestment(o)
{
    var oInitialInvestment = document.getElementById("InitialInvestment");
    oInitialInvestment.value = o.value;
    
    ValidateFormField("ddlInitialInvestment");
    
    SetLeadStastus();
    
}

function ValidateFormFields1()
{

    var isValid = true;
    
    isValid =  ( ValidateFormField("CompanyName1") & ValidateFormField("ContactName1") & 
                 ValidateFormField("Brands1") & ValidateFormField("Title1") &
                 ValidateFormField("Phone1") & ValidateEmail1() );
    if (isValid)
    {
        document.forms[0].submit();    
    }
    else
    {
        alert("Please enter a valid format.");
    }
}
function ValidateFormFields2()
{

    var isValid = true;
    
    isValid =  ( ValidateFormField("CompanyName2") & ValidateFormField("ContactName2") & 
                 ValidateFormField("Title2") &
                 ValidateFormField("Phone2") & ValidateEmail2() );
    if (isValid)
    {
        document.forms[1].submit();    
    }
    else
    {
        alert("Please enter a valid format.");
    }
}
function ValidateFormFields3()
{

    var isValid = true;
    
    isValid =  ( ValidateFormField("name") & ValidateFormField("address") & 
                 ValidateFormField("city") & ValidateFormField("state") &
                 ValidateFormField("phone") & ValidateEmail3() & ValidateFile3());
    if (isValid)
    {
        document.forms[2].submit();    
    }
    else
    {
        alert("Please enter a valid format.");
    }
}


function ValidateFormField(fieldname)
{
    var o = document.getElementById(fieldname);
    
    o.style.backgroundColor = "";
    if (o.value == "")
    {
        o.style.backgroundColor = HILITECOLOR;
        return false;
    }
    
    if(hasWhiteSpace(o.value)==false)
    {
       o.style.backgroundColor = HILITECOLOR;
       return false;
    }

    return true;
}

function hasWhiteSpace(s) 
{
 
     reWhiteSpace = new RegExp(/^\s+$/);
 
     // Check for white space
     if (reWhiteSpace.test(s)) {
          return false;
     }
      return true;
}


function ValidatePhone()
{
    var retVal = ( ValidateFormField("arecode") & ValidateFormField("phone1") & ValidateFormField("phone2") );
    
    var o = document.getElementById("arecode");
    if (o.value.length < 3)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }
    
    o = document.getElementById("phone1");
    if (o.value.length < 3)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }
    
    o = document.getElementById("phone2");
    if (o.value.length < 4)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }

    return retVal;
}

function ValidateLength(o, len)
{
    var retVal = true;
    
    if (o.value.length < len)
    {
        o.style.backgroundColor = HILITECOLOR;
        retVal = false;
    }

    return retVal;
    
}

function ValidateEmail1()
{
    var retVal = ValidateFormField("Email1");
    
    if (retVal)
    {
        var o = document.getElementById("Email1");
        document.getElementById("Email1").value = o.value;
        
        var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
        var regex = new RegExp(emailReg);
        if (!regex.test(o.value))
        {
            o.style.backgroundColor = HILITECOLOR;
            return false;
        }
    }

    return retVal;
}

function ValidateEmail2()
{
    var retVal = ValidateFormField("Email2");
    
    if (retVal)
    {
        var o = document.getElementById("Email2");
        document.getElementById("Email2").value = o.value;
        
        var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
        var regex = new RegExp(emailReg);
        if (!regex.test(o.value))
        {
            o.style.backgroundColor = HILITECOLOR;
            return false;
        }
    }

    return retVal;
}

function ValidateEmail3()
{
    var retVal = ValidateFormField("email");
    
    if (retVal)
    {
        var o = document.getElementById("email");
        document.getElementById("email").value = o.value;
        
        var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
        var regex = new RegExp(emailReg);
        if (!regex.test(o.value))
        {
            o.style.backgroundColor = HILITECOLOR;
            return false;
        }
    }

    return retVal;
}

function ValidateFile3()
{
	var allowSubmit = true;
	var o = document.getElementById("fileupload");
	var file1 = o.value;
	//alert(file1);
	
	extArray = new Array(".pdf", ".doc", ".docx");
	//alert(extArray.length);

	while (file1.indexOf("\\") != -1)
		file1 = file1.slice(file1.indexOf("\\") + 1);
	
	ext = file1.slice(file1.indexOf(".")).toLowerCase();
	//alert(file1);
	//alert(ext);
	if (file1 == "") return true;
	
	for (var i = 0; i < extArray.length; i++) 
	{
	   //alert(i);
		if (extArray[i] == ext)
		 { return true;}
		else 
		{
		  allowSubmit = false;
		}
	}
	if (allowSubmit == false )
	{ alert("Please only upload files that end in types:  " 
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file to upload and submit again.");
		return false;
	}
		
}


function ValidateContactPreference()
{
    var otd = document.getElementById("tdContactPreference");
    
    otd.style.backgroundColor = "";
    
    var o = document.getElementById("ContactPreference");
    
    if (o.value == "0")
    {
        otd.style.backgroundColor = HILITECOLOR;
        
        return false;
    }
    return true;
}

function ValidateQuestion(question)
{

    var otd = document.getElementById("td"+question);
    
    otd.style.backgroundColor = "";
    
    var a = document.getElementById("Ans"+question);
    
    if (a.value == "")
    {
        otd.style.backgroundColor = HILITECOLOR;
        return false;
    }
    
    return true;    
}

function OnClickRadHandler(o)
{
    var a = document.getElementById("Ans"+o.getAttribute("name"));
    a.value = o.value;
    
    var otd = document.getElementById("td"+o.getAttribute("name"));    
    otd.style.backgroundColor = "";
    
    SetLeadStastus();
}

function SetLeadStastus()
{
    var o = document.getElementById("LeadStatus");
    
    var a1 = document.getElementById("Ansq1");
    var a2 = document.getElementById("Ansq2");
    var a3 = document.getElementById("Ansq3");
    var a4 = document.getElementById("Ansq4");
	var dd = document.getElementById("ddlInitialInvestment");
    
    o.value = ( (a1.value=="1") && (a2.value=="1") && (a3.value=="1") && (a4.value=="1") && (dd.value !="under_60000") )? "RFI Qualified" : "RFI Unqualified";
}

function AllowNumericOnly(o)
{
	if(/[^0-9]/.test(o.value))
	{
		o.value=o.value.replace(/([^0-9])/g,"");
	}
}

function AllowNumericAndDash(o)
{
	if(/[^0-9-]/.test(o.value))
	{
		o.value=o.value.replace(/([^0-9-])/g,"");
	}
}

