// JavaScript Documentfunction 

// credit application request form validation
function validateCARequest()
{
	//var accnumber = document.requestCreditApplication.account_number.value;
	var bizstart  = document.requestCreditApplication.date_business_started.value;
	var companyname = document.requestCreditApplication.company_name.value;
	var companyphone = document.requestCreditApplication.company_phone.value;
	var address = document.requestCreditApplication.address.value;
	var city    = document.requestCreditApplication.city.value;
	var state   = document.requestCreditApplication.state.value;
	var zip     = document.requestCreditApplication.zip.value;
	var creditline = document.requestCreditApplication.line_of_credit.value;
	
	// check that at least one partner name is there
	var name1   = document.requestCreditApplication.name1.value;
	var title1  = document.requestCreditApplication.title1.value;

	// reference information
	var refname1    = document.requestCreditApplication.ref_name1.value;
	var refaddress1 = document.requestCreditApplication.ref_address1.value;
	
	// bank information
	var branch1  = document.requestCreditApplication.branch1.value;  
	var account1 = document.requestCreditApplication.account1.value;  
	
	// authorization details
	var authorization  = document.requestCreditApplication.authorization.value;
	var signaturedate  = document.requestCreditApplication.signature_date.value;
	var signature      = document.requestCreditApplication.signature.value;
	var signaturetitle = document.requestCreditApplication.authorizator_title.value;

    // general
	var success        = true;
	var message        = "Please provide the following details:\n";
	
	/**if (accnumber.toString() == '')
	{
	    message += "- Account Number\n";
		success  = false;
	}*/
	
	if (bizstart.toString() == '')
	{
	    message += "- Business Start Date\n";
		success  = false;
	}
	
	if (companyname.toString() == '')
	{
	    message += "- Company Name\n";
		success  = false;
	}

	if (companyphone.toString() == '')
	{
	    message += "- Company Phone\n";
		success  = false;
	}

	if (address.toString() == '')
	{
	    message += "- Company Address\n";
		success  = false;
	}
	
	if (city.toString() == '')
	{
	    message += "- City\n";
		success  = false;
	}

	if (state.toString() == '')
	{
	    message += "- State\n";
		success  = false;
	}

	if (zip.toString() == '')
	{
	    message += "- Zip\n";
		success  = false;
	}
	
	if (creditline.toString() == '')
	{
	    message += "- Credit Line Amount\n";
		success  = false;
	}

	if (name1.toString() == '')
	{
	    message += "- Partner, Owners or Officer\n";
		success  = false;
	}

	if (title1.toString() == '')
	{
	    message += "- Partner, Owners or Officer Title\n";
		success  = false;
	}
	
	if (refname1.toString() == '')
	{
	    message += "- Trade Rederence Name\n";
		success  = false;
	}
	
	if (refaddress1.toString() == '')
	{
	    message += "- Trade Reference Address\n";
		success  = false;
	}
	
	if (branch1.toString() == '')
	{
	    message += "- Bank/Branch Name\n";
		success  = false;
	}
	
	if (account1.toString() == '')
	{
	    message += "- Bank/Branch Account Number\n";
		success  = false;
	}
	
	if (authorization.toString() == '')
	{
	    message += "- Authorization\n";
		success  = false;
	}
	
	if (signaturedate.toString() == '')
	{
	    message += "- Signature Date\n";
		success  = false;
	}
	
	if (signature.toString() == '')
	{
	    message += "- Signature Initials\n";
		success  = false;
	}
	
	if (signaturetitle.toString() == '')
	{
	    message += "- Signature Title\n";
		success  = false;
	} 

	if (success == false)
	{
	    alert(message);
		return false;
	}
	else
	{
	    return true;
    }
}


// validate generator service request form
function validateGSRequest()
{
	var companyname    = document.requestGeneratorService.company_name.value;
	var billingaddress = document.requestGeneratorService.billing_address.value;
	var billingcity    = document.requestGeneratorService.billing_city.value;
	var billingstate   = document.requestGeneratorService.billing_state.value;
	var billingzip     = document.requestGeneratorService.billing_zip.value;
	var billingname    = document.requestGeneratorService.billing_contact_name.value;
	var billingphone   = document.requestGeneratorService.billing_phone.value;
	var schedule       = getCheckedValue(document.forms['requestGeneratorService'].elements['schedule_basis']);
	var specify_other  = document.requestGeneratorService.specify_other.value;
	var numofgalons    = document.requestGeneratorService.number_of_gallons.value;
	var success        = true;
	var message        = "Please provide the following details:\n";
	
    if (companyname.toString() == '')
	{
	    message += "- Company Name\n";
		success  = false;
	}
	
	if (billingaddress.toString() == '')
	{
	    message += "- Billing Address\n";
		success  = false;
	}
	
	if (billingcity.toString() == '')
	{
	    message += "- Billing City\n";
		success  = false;
	}
	
	if (billingstate.toString() == '')
	{
	    message += "- Billing State\n";
		success  = false;
    }
	
	if (billingzip.toString() == '')
	{
	    message += "- Billing Zip\n";
		success  = false;
	}
	
	if (billingname.toString() == '')
	{
	    message += "- Billing Contact\n";
		success  = false;
	}
	
	if (billingphone.toString() == '')
	{
	    message += "- Billing Phone\n";
		success  = false;
	}
	
	if (schedule.toString() == 'other')
	{
	    if (specify_other.toString() == '')
		{
		    message += "- Specify Other Schedule\n";
			success  = false;
		}
	}
	
	if (numofgalons.toString() == '')
	{
	    message += "- Number Of Gallons\n";
		success  = false;
	}
	
	if (success == false)
	{
	    alert(message);
		return false;
	}
	else
	{
	    var _checkzip = isZip(billingzip);
		var _checkboxempty = isCheckboxArrayEmpty();
		
		if (_checkzip == false)
		{
		    alert("Billing Zip is invalid!");
			success = false;
		}
		
		if (_checkboxempty == true)
		{
		    alert("Please specify the requested service.");
			success = false;
		}
		
		if (success)
		{
		    return true;
		}
		else
		{
		    return false;
		}
	}
}


function isCheckboxArrayEmpty() 
{
	var count = 0;
    boxes = document.requestGeneratorService.service_requested.length;
    for (i = 0; i < boxes; i++) 
	{
        if (document.requestGeneratorService.service_requested[i].checked) 
		{
		    count++;
        }
    }
	
	if (count > 0)
	{
	    return false;
	}
	else
	{
	    return true; // empty
	}
}

// validate fleet service request form
function validateFSRequest()
{
	var companyname    = document.requestFleetService.company_name.value;
	var billingaddress = document.requestFleetService.billing_address.value;
	var billingcity    = document.requestFleetService.billing_city.value;
	var billingstate   = document.requestFleetService.billing_state.value;
	var billingzip     = document.requestFleetService.billing_zip.value;
	var billingname    = document.requestFleetService.billing_contact_name.value;
	var billingphone   = document.requestFleetService.billing_phone.value;
	var numoftrucks    = document.requestFleetService.number_of_trucks.value;
	var numofdays      = getCheckedValue(document.forms['requestFleetService'].elements['number_of_days']);
	var specify_other  = document.requestFleetService.specify_other.value;
	var success        = true;
	var message        = "Please provide the following details:\n";
	
    if (companyname.toString() == '')
	{
	    message += "- Company Name\n";
		success  = false;
	}
	
	if (billingaddress.toString() == '')
	{
	    message += "- Billing Address\n";
		success  = false;
	}
	
	if (billingcity.toString() == '')
	{
	    message += "- Billing City\n";
		success  = false;
	}
	
	if (billingstate.toString() == '')
	{
	    message += "- Billing State\n";
		success  = false;
    }
	
	if (billingzip.toString() == '')
	{
	    message += "- Billing Zip\n";
		success  = false;
	}
	
	if (billingname.toString() == '')
	{
	    message += "- Billing Contact\n";
		success  = false;
	}
	
	if (billingphone.toString() == '')
	{
	    message += "- Billing Phone\n";
		success  = false;
	}
	
	if (numoftrucks.toString() == '')
	{
	    message += "- Number Of Trucks\n";
		success  = false;
	}
	
	if (numofdays.toString() == 'other')
	{
	    if (specify_other.toString() == '')
		{
		    message += "- Specify Other Days\n";
			success  = false;
		}
	}
	
	if (success == false)
	{
	    alert(message);
		return false;
	}
	else
	{
	    var _checkzip = isZip(billingzip);
		var _checktrucks = isPositiveInteger(numoftrucks);
		
		if (_checkzip == false)
		{
		    alert("Zip is invalid!");
			success = false;
		} 
		
		if (_checktrucks == false)
		{
		    alert("Number of trucks should be an Integer!");
			success = false;
		}
		
		if (success)
		{
		    return true;
		}
		else
		{
		    return false;
		}
	}
}

// check for the correct zip
function isZip(s) 
{
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
     if (!reZip.test(s)) 
	 {
          return false;
     }
    return true;
}

// check for integer
function isPositiveInteger(val)
{
      if(val==null){return false;}
      if (val.length==0){return false;}
      for (var i = 0; i < val.length; i++) {
            var ch = val.charAt(i)
            if (ch < "0" || ch > "9") {
            return false
            }
      }
      return true;
}

// get radio button
function getCheckedValue(radioObj) 
{
	if(!radioObj) return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// verify email
function verifyEmail(str)
{
    var regexpr = /\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi;
	return regexpr.test(str);
}
