﻿function despace(testVal)
{ return testVal.replace(/ +/g,''); }

function despaceTA(testVal)
{ tempStr = testVal.replace(/ +/g,''); return tempStr.replace(/\n+/g,''); }

function doVal(thisform)
{
    var re = new RegExp;
    re.compile("[.\r\n]*?[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+[.\r\n]*?");

     if ( re.test(thisform.taMail.value) || re.test(thisform.taAdd.value) || re.test(thisform.taDir.value) || re.test(thisform.taComments.value) )
    { alert("You cannot enter URLs in this form."); return false; }

    if (!despace(thisform.txtName.value)) { alert("You must enter a name."); return false; }
    if (radioIndex(thisform.radContactMethod) < 0) { alert("You must choose a contact method."); return false; }
    if (radioIndex(thisform.radContactMethod) == 0) {
        if (!despaceTA(thisform.taMail.value)) { alert("You must enter a mailing address if you chose it as your contact method."); return false; }
    }
    if (radioIndex(thisform.radContactMethod) == 1) {
        if (!despace(thisform.txtEmail.value)) { alert("You must enter an e-mail address if you chose it as your contact method."); return false; }
    }
    if (despace(thisform.txtEmail.value)) { if ( !thisform.txtEmail.value.match(/\w+@\w+.\w{2,}/) ) { alert ("You must enter a valid e-mail address."); return false; } }
    if ( !despace(thisform.txtPhoneHome.value) && !despace(thisform.txtPhoneWork.value) && !despace(thisform.txtPhoneCell.value) && !despace(thisform.txtPhoneFax.value) )
        { alert("You must enter at least one contact phone number."); return false; }
    
    if (!despaceTA(thisform.taAdd.value)) { alert("You must enter the physical address of the site."); return false; }
    if (radioIndex(thisform.radMeasure) < 0) { alert("You must choose if it is OK to measure, or if we should call you to arrange a time."); return false; }
     
   
    
    /*
    if (!despace(thisform.ccity.value)) { alert("You must enter a city."); return false; }
    if (!despace(thisform.cemail.value)) { alert("You must enter an email address."); return false; }
    if (!despace(thisform.cphone.value) && !despace(thisform.cemail.value) ) { alert("Please enter a phone number or an e-mail address."); return false; }
    if (despace(thisform.cemail.value)) { if ( !thisform.cemail.value.match(/\w+@\w+.\w{2,}/) ) { alert ("You must enter a valid e-mail address."); return false; } }
    */
}

function radioIndex(rb)
{ checkedIndex = -1;
 for (i=0;i<rb.length;i++)
 { if (rb[i].checked) { checkedIndex = i; } }
 return checkedIndex;
}