function isEmpty(strng, messg) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter "+messg+"\n"
  }
return error;	  
}

function checkRadio(checkvalue, messg) {
var error = "";
   if (!(checkvalue)) {
       error = "Please select a "+messg+"\n";
    }
return error;
}

function valForm(thisForm) {
    var err = "";
    err += isEmpty(thisForm.firstName.value,"your first name");
    err += isEmpty(thisForm.lastName.value,"your last name");
    err += isEmpty(thisForm.title.value,"your title");
    err += isEmpty(thisForm.facility.value,"your facility");
    err += isEmpty(thisForm.address.value,"your address");
    err += isEmpty(thisForm.city.value,"your city");
    err += isEmpty(thisForm.state.value,"your state");
    err += isEmpty(thisForm.zip.value,"your zip");
    err += isEmpty(thisForm.officeAreaCode.value,"your office area code");
    err += isEmpty(thisForm.officePrefix.value,"your office telephone prefix");
    err += isEmpty(thisForm.officeSuffix.value,"your office telephone suffix");
    
    
    // Checking for program selection
    checkvalue="";
    for (i=0, n=thisForm.rbProgram.length; i<n; i++) {
        if (thisForm.rbProgram[i].checked) {
            var checkvalue = thisForm.rbProgram[i].value;
            break;
        } 
    }
    err += checkRadio(checkvalue,"program");
	// Checking for Partner if package deal selected 
    checkclass="";
    for (i=0, n=thisForm.rbProgram.length; i<n; i++) {
        if (thisForm.rbProgram[i].checked) {
			var checkclass = thisForm.rbProgram[i].className;
			if (thisForm.rbProgram[i].className == "package" )
			{		
				err += isEmpty(thisForm.partnerName.value, "Partner's Name");
				err += isEmpty(thisForm.partnerTitle.value, "Partner's Title");
				err += isEmpty(thisForm.partnerFacility.value, "Partner's facility");
				err += isEmpty(thisForm.partnerEmail.value, "Partner's Email");
				err += isEmpty(thisForm.partnerAreaCode.value, "Partner's area code");
				err += isEmpty(thisForm.partnerPrefix.value, "Partner's telephone prefix");
				err += isEmpty(thisForm.partnerSuffix.value, "Partner's telephone suffix");
			}
            break;
        } 
    }
	
	// Checking for CC payment info if not a check 
    checkvalue="";
    for (i=0, n=thisForm.rbPayment.length; i<n; i++) {
        if (thisForm.rbPayment[i].checked) {
            var checkvalue = thisForm.rbPayment[i].value;
			if(thisForm.rbPayment[i].value != "Check")
			{
				//alert("not a check:" + thisForm.rbPayment[i].value);
				
				err += isEmpty(thisForm.creditCardHolder.value, "Credit Card Holder");
				
				err += checkCreditCardNumberFields(checkvalue, thisForm.creditCardNumber1.value, thisForm.creditCardNumber2.value, thisForm.creditCardNumber3.value, thisForm.creditCardNumber4.value );
				err += checkZero(thisForm.expMonth.selectedIndex, "Credit Card Expiration Month");
				err += checkZero(thisForm.expYear.selectedIndex, "Credit Card Expiration Year");
			}

            break;
        } 
    }
    err += checkRadio(checkvalue,"payment");
    
    if (err != "") {
       alert(err);
       return false;
    }
    

return true;
}


function updateCCSize(cardType)
{
	var theForm = document.getElementById("regForm");

	theForm.creditCardNumber1.value = "";
	theForm.creditCardNumber2.value = "";
	theForm.creditCardNumber3.value = "";
	theForm.creditCardNumber4.value = "";
	
	if(cardType == 'amex')
	{
		theForm.creditCardNumber1.setAttribute("maxLength", 4);
		theForm.creditCardNumber2.setAttribute("maxLength", 3);
		theForm.creditCardNumber3.setAttribute("maxLength", 3);
		theForm.creditCardNumber4.setAttribute("maxLength", 5);
		
		theForm.creditCardNumber1.setAttribute("size", 4);
		theForm.creditCardNumber2.setAttribute("size", 3);
		theForm.creditCardNumber3.setAttribute("size", 3);
		theForm.creditCardNumber4.setAttribute("size", 5);		
	}
	else
	{
		theForm.creditCardNumber1.setAttribute("maxLength", 4);
		theForm.creditCardNumber2.setAttribute("maxLength", 4);
		theForm.creditCardNumber3.setAttribute("maxLength", 4);
		theForm.creditCardNumber4.setAttribute("maxLength", 4);	
		
		theForm.creditCardNumber1.setAttribute("size", 4);
		theForm.creditCardNumber2.setAttribute("size", 4);
		theForm.creditCardNumber3.setAttribute("size", 4);
		theForm.creditCardNumber4.setAttribute("size", 4);		
	}
}

function checkZero(value, mesg)
{
var error = "";
   if (value == "0") {
       error = "Please select a "+mesg+"\n";
    }
return error;
}

function checkCreditCardNumberFields(paymentMethod, field1, field2, field3, field4)
{
var hasError = false;
var error = "";
var amex = false;

	if(paymentMethod == "American Express")
	{
		amex = true;

		if(field1.length < 4 || field2.length < 3 || field3.length < 3 || field4.length < 5)
		{
			hasError = true;
			//alert("'" + paymentMethod + "'");
		}
		else
		{
				var field = "0";
		
				//field1
				if(field1.charAt(0) < "0" || field1.charAt(0) > "9")
				{
					hasError = true;
					field = "1,0";
				}
				else if(field1.charAt(1) < "0" || field1.charAt(1) > "9")
				{
					hasError = true;
					field = "1,1";
				}
				else if(field1.charAt(2) < "0" || field1.charAt(2) > "9")
				{
					hasError = true;
					field = "1,2";
				}
				else if(field1.charAt(3) < "0" || field1.charAt(3) > "9")
				{
					hasError = true;
					field = "1,3";
				}
				
				//field2
				if(field2.charAt(0) < "0" || field2.charAt(0) > "9")
				{
					hasError = true;
					field = "2,0";
				}
				else if(field2.charAt(1) < "0" || field2.charAt(1) > "9")
				{
					hasError = true;
					field = "2,1";
				}
				else if(field2.charAt(2) < "0" || field2.charAt(2) > "9")
				{
					hasError = true;
					field = "2,2";
				}
				
				//field3
				if(field3.charAt(0) < "0" || field3.charAt(0) > "9")
				{
					hasError = true;
					field = "3,0";
				}
				else if(field3.charAt(1) < "0" || field3.charAt(1) > "9")
				{
					hasError = true;
					field = "3,1";
				}
				else if(field3.charAt(2) < "0" || field3.charAt(2) > "9")
				{
					hasError = true;
					field = "3,2";
				}				

				//field4
				if(field4.charAt(0) < "0" || field4.charAt(0) > "9")
				{
					hasError = true;
					field = "4,0";
				}
				else if(field4.charAt(1) < "0" || field4.charAt(1) > "9")
				{
					hasError = true;
					field = "4,1";
				}
				else if(field4.charAt(2) < "0" || field4.charAt(2) > "9")
				{
					hasError = true;
					field = "4,2";
				}
				else if(field4.charAt(3) < "0" || field4.charAt(4) > "9")
				{
					hasError = true;
					field = "4,3";
				}
				else if(field4.charAt(4) < "0" || field4.charAt(4) > "9")
				{
					hasError = true;
					field = "4,4";
				}		
		}
	}
	else
	{
		//not american express
		if(field1.length < 4 || field2.length < 4 || field3.length < 4 || field4.length < 4)
		{
			hasError = true;
		}
		else
		{
			for(i = 0; i < 4; i++)
			{
				if(field1.charAt(i) < "0" || field1.charAt(i) > "9")
				{
					hasError = true;
				}
				else if(field2.charAt(i) < "0" || field2.charAt(i) > "9")
				{
					hasError = true;
				}
				else if(field3.charAt(i) < "0" || field3.charAt(i) > "9")
				{
					hasError = true;
				}
				else if(field4.charAt(i) < "0" || field4.charAt(i) > "9")
				{
					hasError = true;
				}						
			}
		}
	}

	
	if(hasError)
	{
		if(amex)
		{
			error = "Please enter a valid 15 digit credit card number\n";
		}
		else
		{
			error = "Please enter a valid 16 digit credit card number\n";
		}
	}

return error;
}

function showCCInfo()
{
	document.getElementById("ccInfo").style.display = "block";
}

function hideCCInfo()
{
	document.getElementById("ccInfo").style.display = "none";
}

function showPartnerInfo()
{
	document.getElementById("partnerInfo").style.display = "block";
}

function hidePartnerInfo()
{
	document.getElementById("partnerInfo").style.display = "none";
}