

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}







function clearField(field, text) {
	if (field.value == text) { field.value = ""; }
	return;
}

function fillField(field, text) {
	if (isblank(field.value)) { field.value = text; }
	return;
}

function isblank(x) {
	var blank = true;
	for (i = 0; i < x.length; i++) {
		if (x.charAt(i) != ' ') { blank = false; }
	}
	return blank;
}

function CheckNo(field, expected) 
{
	var valid = "0 1 2 3 4 5 6 7 8 9 . $ % ( ) - e x t"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) 
	{
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
	}

	if (ok == "no") 
	{
		alert("Invalid entry!  Only " + expected + " are accepted!");
		field.value="";
		field.focus();
		field.select();	
		return false;
	}

}

function CheckNoCo(field) 
{
	var valid = "0 1 2 3 4 5 6 7 8 9 . % ";
	var ok = "yes";
	var temp;

	for (var i=0; i<field.value.length; i++) 
	{
	temp = "" + field.value.substring(i, i+1);

	if (valid.indexOf(temp) == "-1" ) ok = "no";
	}

       if( parseFloat(field.value)  > 100) 
       {
              ok = "no";
       }      

	if (ok == "no") 
	{
		alert("Invalid entry!  Only numbers 100 or less are expected!");
		field.value="";
		field.focus();
		field.select();	
		return false;
	}

}



function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}




function contact_validate()
{
	var mess = '';

	( $F('first_name')=='' )?   mess += "First Name is a required field \n" : '';
	( $F('last_name')=='' )?    mess += "Last Name is a required field \n" : '';
	( $F('phone')=='' )?        mess += "Phone is a required field \n" : '';
	( $F('email')=='' )?        mess += "Email is a required field \n" : '';
	( $F('company')=='' )?      mess += "Company is a required field \n" : '';
	( $F('zip')=='' )?          mess += "Zip is a required field \n" : '';

	if(mess != '')
	{ 
	alert("Please correct the following fields and submit again. \n"+mess); 
	}
	else
	{
	document.contact.submit();
	}

}


function validate()
{

	var mess = '';

	step_1 = $RF('rfp', 'I_am_a');


	if(step_1 == 'broker')
	{
		( $F('Name_of_Firm')=='' )?                 mess += "The Firm's name is a required field \n" : '';
		( $F('Firm_Address')=='' )?                 mess += "The Firm's address is a required field \n" : '';
		( $F('Firm_City')=='' )?                    mess += "The Firm's city is a required field \n" : '';
		( $F('Firm_State')=='State' )?              mess += "The Firm's state is a required field \n" : '';		
		( $F('Firm_Zip')=='' )?                     mess += "The Firm's zip is a required field \n" : '';
		( $F('Firm_Phone')=='' )?                   mess += "The Firm's phone is a required field \n" : '';
		( $F('name_of_employer')=='' )?             mess += "The name of employer is a required field \n" : '';
		( $F('name_of_employer_city')=='' )?        mess += "The employer's city is a required field \n" : '';
		( $F('name_of_employer_State')=='State' )?  mess += "The employer's state is a required field \n" : '';

		
	}
	else if(step_1 == 'employer')
	{
		( $F('Employer_Name')=='' )?          mess += "The Employer name is a required field \n" : '';
		( $F('Employer_address')=='' )?       mess += "The address of the employer is a required field \n" : '';
		( $F('Employer_City')=='' )?          mess += "The city of the employer is a required field \n" : '';
		( $F('Employer_State')=='State' )?    mess += "The state of the employer is a required field \n" : '';	
		( $F('emp_Zip')=='' )?                mess += "The zip of the employer is a required field \n" : '';
		( $F('Employer_phone')=='' )?         mess += "The phone number for the employer is a required field \n" : '';
		( $F('Employer_fax')=='' )?           mess += "The fax number for the employer is a required field \n" : '';
	}
	else if (step_1 ==undefined)
	{
		 mess += "Step 1 is required  \n";
	}

	if(step_1 == 'employer' || step_1 == 'broker')
	{	
		( $F('Primary_Name')=='' )?             mess += "Please provide a contact name \n" : '';
		( $F('Primary_Phone')=='' )?            mess += "Please provide a contact phone number \n" : '';
		( $F('Primary_Email')=='' )?            mess += "Please provide an email address \n" : '';
	}




	var file_types = [".doc",".xls","csv",".pdf"];
	
	
// Plan Summary Document Upload validation
var plan_summary_document = document.rfp.elements["plan_summary_document[]"];
var psd_ft = true;

// All attachments must have the correct filetype, or else alert messsage is thrown
for(i=0; i<plan_summary_document.length; i++) {
  docfilename = plan_summary_document[i].value;

	if(docfilename!=""){
		for(var j=0; j<file_types.length; j++) {
			if(docfilename.lastIndexOf(file_types[j])>=0) {
 			  psd_ft = true;
			  break;
			} else {
	 			psd_ft = false;
			}
		}
    
    // Append message and break as soon as we find an invalid file type attached
    if(psd_ft==false){
      mess += "For Step 3, the Plan Summary document can only be uploaded in the following formats: (.doc, .xls, .csv, .pdf)  \n"; 
      break;
    }
	}
}

// Plan Summary Document Upload validation
var census_file = document.rfp.elements["census_file[]"];
var cf = true;

// All attachments must have the correct filetype, or else alert messsage is thrown
for(i=0; i<census_file.length; i++) {
  docfilename = census_file[i].value;

	if(docfilename!=""){
		for(var j=0; j<file_types.length; j++) {
			if(docfilename.lastIndexOf(file_types[j])>=0) {
 			  cf = true;
			  break;
			} else {
	 			cf = false;
			}
		}
    
    // Append message and break as soon as we find an invalid file type attached
    if(cf==false){
      mess += "For Step 3, the Census file can only be uploaded in the following formats: (.doc, .xls, .csv, .pdf)  \n"; 
      break;
    }
	}
}

/* //old version - jla
	if(document.rfp.plan_summary_document.value!="")
	{
  	var psd_ft = true;

			for(var i=0; i < file_types.length; i++)
			{
				if(document.rfp.plan_summary_document.value.lastIndexOf(file_types[i])>=0)
				{
			         psd_ft = true;
				  break;
				}
				else
				{
 				  psd_ft = false;
				}
			}

		if(psd_ft==false){ mess += "For Step 3, the Plan Summary document can only be uploaded in the following formats: (.doc, .xls, .csv, .pdf)  \n"; }
	}
*/


/* //old version - jla
	if(document.rfp.census_file.value!="")
	{
  	var cf = true;

			for(var i=0; i < file_types.length ;i++)
			{
				if(document.rfp.census_file.value.lastIndexOf(file_types[i])>=0)
				{
				cf = true;
				  break;
				}
				else
				{
 				  cf = false;
				}
			}

		if(cf==false){ mess += "For Step 3, the Census file can only be uploaded in the following formats: (.doc, .xls, .csv, .pdf)  \n"; }
	}
*/	
	




	validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
	
	form_date = $F('coverage_start_month')+'/'+$F('coverage_start_day')+'/'+$F('coverage_start_year') ;

	day_test = ($F('coverage_start_month')>12)? true : false; 
	month_test = ($F('coverage_start_day')>31)? true : false;
	year_test = ($F('coverage_start_year')<2008)? true : false;

	if ( !validformat.test(form_date) || day_test == true || month_test == true || year_test == true )
	{
	 mess += "You must use a valid numeric date format for the Effective Date of Coverage field. \n";
	}
	
	if( $F('in_network_utilzation')>100 )
	{
	 mess += "Network Utilization is represented as a percentage and must be a number of 100 or less. \n";
	}



	//mess=""; 




	if(mess != '')
	{ 
	alert("Please correct the following fields and submit again. \n"+mess); 
	}
	else
	{
	document.rfp.submit();
	}


}//end function validate()





function  tier_status(instance)
{

 instance = $RF('rfp', 'tiers');

	if(instance == '3 Tier')
	{
	document.getElementById('2generic_cost').disabled = true;
	document.getElementById('2brand_cost').disabled = true;
	document.getElementById('3generic_cost').disabled = false;
	document.getElementById('3prefered_cost').disabled = false;
	document.getElementById('3nonprefered_cost').disabled = false;
	}
	else
	{
	document.getElementById('2generic_cost').disabled = false;
	document.getElementById('2brand_cost').disabled = false;
	document.getElementById('3generic_cost').disabled = true;
	document.getElementById('3prefered_cost').disabled = true;
	document.getElementById('3nonprefered_cost').disabled = true;
	}
}






function showtype(instance)
{
    if(instance=='employer')
    {
       $('broker').hide();
	$('broker_egi').hide();
       $('employer').show();
       $('contact_type').show();
    }
    else
    {
       $('employer').hide();
       $('broker').show();
       $('broker_egi').show();
       $('contact_type').show();
    }

return instance;
}





function clickMe(item) {

	if (item=='opt1') {
		showType('opt1', 'opt1.1', 'opt1.2', 'opt1.3', 'opt1.4', 'opt1.5', 'opt1.6');
		hideType('opt2', 'opt2.1', 'opt2.2', 'opt2.3', 'opt2.4', 'opt2.5', 'opt2.6');
		hideType('opt3', 'opt3.1', 'opt3.2', 'opt3.3', 'opt3.4', 'opt3.5', 'opt3.6');
		showThis('otherhide', 'other');
		hideMe('other2', 'other3');
		return;
			}
	else if (item=='opt2') {
		showType('opt1', 'opt1.1', 'opt1.2', 'opt1.3', 'opt1.4', 'opt1.5', 'opt1.6');
		showType('opt2', 'opt2.1', 'opt2.2', 'opt2.3', 'opt2.4', 'opt2.5', 'opt2.6');
		hideType('opt3', 'opt3.1', 'opt3.2', 'opt3.3', 'opt3.4', 'opt3.5', 'opt3.6');
		showThis('otherhide', 'other');
		hideMe('other2', 'other3');
		return;
			}
	else if (item=='opt3') {
		showType('opt1', 'opt1.1', 'opt1.2', 'opt1.3', 'opt1.4', 'opt1.5', 'opt1.6');
		showType('opt2', 'opt2.1', 'opt2.2', 'opt2.3', 'opt2.4', 'opt2.5', 'opt2.6');
		showType('opt3', 'opt3.1', 'opt3.2', 'opt3.3', 'opt3.4', 'opt3.5', 'opt3.6');
		showThis('otherhide', 'other');
		hideMe('other2', 'other3');
		return;
			}
	else if (item=='opt4') {
		showThis('other', 'otherhide');
		showMe('other2', 'other3');
		hideType('opt1', 'opt1.1', 'opt1.2', 'opt1.3', 'opt1.4', 'opt1.5', 'opt1.6');
		hideType('opt2', 'opt2.1', 'opt2.2', 'opt2.3', 'opt2.4', 'opt2.5', 'opt2.6');
		hideType('opt3', 'opt3.1', 'opt3.2', 'opt3.3', 'opt3.4', 'opt3.5', 'opt3.6');
			return;
			}
}
		
	function showType(on, on2, on3, on4, on5, on6, on7) {
		$('_'+on).show();
		$('_'+on2).show();
		$('_'+on3).show();
		$('_'+on4).show();
		$('_'+on5).show();
		$('_'+on6).show();
		$('_'+on7).show();
	  return;
	}
	
	function hideType(off, off2, off3, off4, off5, off6, off7) {
		$('_'+off).hide();
		$('_'+off2).hide();
		$('_'+off3).hide();
		$('_'+off4).hide();
		$('_'+off5).hide();
		$('_'+off6).hide();
		$('_'+off7).hide();
	  return;
}
	function showThis(on, off) {
		$('_'+on).show();
		$('_'+off).hide();
	  return;
		}
	function showMe(on, on2) {
		$('_'+on).show();
		$('_'+on2).show();
		  return;
}
	function hideMe(off, off2) {
		$('_'+off).hide();
		$('_'+off2).hide();
		  return;
}

/**
  * Adds an additional upload field for Plan Summary Document
  */
function addUploadFieldPS(docType) {
	var div_name = docType + "_fields";
  new Insertion.Bottom(div_name, "<br/><input name='" + docType + "[]' id='files' type='file'>");
}
