/* Copyright 2016 National Association of Insurance Commissioners. All Rights Reserved. */

var pageSubmitted = false;


function isReportSubmitted(form){
	if(pageSubmitted == true){
		alert("Please be patient while your report is generated.");
		return false;
	}
    	return true;
}

function CountsByStateSearch(form){
	var msg = "";
    	if(msg == ""){
	    pageSubmitted = true;
	    return true;
    	}else{
    	    alert(msg);
	    return false;
    	}
}

function CountsByCodeSearch(form){
	var msg = "";
    	if(msg == ""){
	    pageSubmitted = true;
	    return true;
    	}else{
    	    alert(msg);
	    return false;
    	}
}

function IndexSearch(form){
	var msg = "";

	if(form.reportType.selectedIndex != null && form.reportType.selectedIndex == 0){
		msg += "Please select the type of insurance.\n";
	}
    	if(msg == ""){
	    pageSubmitted = true;
	    return true;
    	}else{
    	    alert(msg);
	    return false;
    	}
}

function checkComplaintForm(form){
    var msg = "";

    if(form.firstName.value.length < 1) msg += "Provide your first name\n";
    if(form.lastName.value.length < 1) msg += "Provide your last name\n";
    if(form.address1.value.length < 1) msg += "Provide your address\n";
    if(form.city.value.length < 1) msg += "Provide your city\n";
    if(form.state.selectedIndex != null && form.state.selectedIndex == 0){
		msg += "Select your state\n";
    }
    if(checkZip(form.zip.value)){
      msg += "Provide a valid zip code\n";
    }

    if(form.email.value.length < 1){
	msg += "Provide your e-mail address\n";
    }
    if(checkNumber(form.phone1.value) ||checkNumber(form.phone2.value) || checkLongNumber(form.phone3.value)){
	msg += "Provide a valid phone number\n";
    }

    if(form.altPhone1.value != "" || form.altPhone2.value != "" || form.altPhone3.value != ""){
    	if(checkNumber(form.altPhone1.value) ||checkNumber(form.altPhone2.value) || checkLongNumber(form.altPhone3.value)){
			msg += "Provide a valid alternate phone number\n";
     	}
     }
    if(form.companyName.value.length < 1) msg += "Provide an insurance company name\n";
    if(form.insuranceType.value.length < 1) msg += "Provide the type of insurance\n";
    if(form.insuredPerson.value.length < 1) msg += "Provide the name of the insured person\n";
    if(form.complaint.value.length < 3) msg += "Provide the text of your complaint\n";
   if(msg == ""){
        return true;
    }else{
        alert("_________________________________________________________\n\n" +
              "The complaint was not submitted due to the following error(s).\n" +
              "Please correct these error(s) and re-submit the complaint for confirmation.\n" +
              "_________________________________________________________\n\n" +

              msg);

        return false;
    }
}

function checkUserTypeForm(){
    var form = document.forms[0];
    var msg = "";
    if(form.userType.selectedIndex == 0)msg += "Select your User Type\n";

    if(msg == ""){
        form.submit();
    }else{
        alert("Please select your user type.");
    }
}

function checkUserDemographicsForm(){

    var form = document.userDemographics;
    var msg = "";
    if(form.userType.selectedIndex == 0)msg += "Select your User Type\n";
    if(form.userType.selectedIndex != 2){
        if(form.companyName.value.length < 1) msg += "Provide a Company Name\n";
        if(form.contactName.value.length < 1) msg += "Provide a Contact Name\n";
        if(form.address.value.length < 1) msg += "Provide an Address\n";
        if(form.city.value.length < 1) msg += "Provide a City\n";
        if(form.state.selectedIndex != null && form.state.selectedIndex == 0)msg += "Select a State\n";
        if(checkZip(form.zipCode.value))msg += "Provide a valid Zip Code\n";
        if(checkNumber(form.phone1.value) ||checkNumber(form.phone2.value) || checkLongNumber(form.phone3.value)){
            msg += "Provide a valid Phone Number\n";
        }
        if(!isEmail(form.email.value))msg += "Provide a valid E-mail Address\n";
    }

    if(msg == ""){
        form.submit();
    }else{
        alert("______________________________________________\n\n" +
              "Please correct the following error(s) and \n" +
              "re-submit to download your financial report.\n" +
              "______________________________________________\n\n" +

              msg);
    }
}

function checkZip(number){
  var test = false;
  if(isNaN(parseInt(number.charAt(0)))) test = true;
  if(isNaN(parseInt(number.charAt(1)))) test = true;
  if(isNaN(parseInt(number.charAt(2)))) test = true;
  if(isNaN(parseInt(number.charAt(3)))) test = true;
  if(isNaN(parseInt(number.charAt(4)))) test = true;
  return test;
}

function checkNumber(number){
  var test = false;
  if(isNaN(parseInt(number.charAt(0)))) test = true;
  if(isNaN(parseInt(number.charAt(1)))) test = true;
  if(isNaN(parseInt(number.charAt(2)))) test = true;
  return test;
}

function checkLongNumber(number){
  var test = false;
  if(isNaN(parseInt(number.charAt(0)))) test = true;
  if(isNaN(parseInt(number.charAt(1)))) test = true;
  if(isNaN(parseInt(number.charAt(2)))) test = true;
  if(isNaN(parseInt(number.charAt(3)))) test = true;
  return test;
}

function isEmail(str){
  var supported = 0;
  if (window.RegExp){
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if(!supported){
      return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  }
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function doConfirmAlert(){
   var msg = "";
   msg += "Please confirm your complaint and click the\n";
   msg += "Submit Complaint button\n";
   msg += "at the bottom of the page";
   alert(msg);
}

function getPDF(filing){
   document.getForm.filingId.value = filing;
   document.getForm.target = filing;
   document.getForm.submit();
}

function explain(aValue){
	var msg = aValue;
  if(aValue == 'National Median Complaint Ratio'){
          msg += ":\n";
          msg += "This value is calculated by dividing the ";
          msg += "National Median Complaint Index by itself.\n";
          msg += "This value represents the nation-wide 'complaint to premium' ratio ";
          msg += "for the policy type for the calendar year.";
	}else if(aValue == 'Complaint Ratio'){
          msg += ":\n";
          msg += "This value is calculated as follows:  ";
          msg += "The company's Complaint Index is divided by the National Median ";
          msg += "Complaint Index, which is the median complaint index of all company ";
          msg += "indices obtained under the policy type for the calendar year.  ";
          msg += "This calculation provides the company's Complaint Ratio, ";
          msg += "which is the company's ratio of complaints relative to the ";
          msg += "National Median Complaint Index.";
	}else if(aValue == 'Policy Type'){
          msg += ":\n";
          msg += "The Policy Type represents the category of insurance under which ";
          msg += "the complaints are grouped.  Currently, there are seven policy types ";
          msg += "included in CIS:\n\n";
          msg += "     All Property\n";
          msg += "     Homeowners\n";
          msg += "     Private Passenger\n";
          msg += "     Individual Life\n";
          msg += "     Group Life\n";
          msg += "     Individual Accident and Health\n";
          msg += "     Group Accident and Health\n\n";
          msg += "Please refer to the CIS Help for a description of what lines of insurance ";
          msg += "are lincluded within each policy type.";
	}else if(aValue == 'Complaint Year'){
          msg += ":\n";
          msg += "The Complaint Year is the calendar year in which the complaints were ";
          msg += "closed in the respective states in which they were filed.";
	}else if(aValue == 'Premium Year'){
          msg += ":\n";
          msg += "The Premium Year is the calendar year in which premiums written \n";
          msg += "data was filed with the NAIC.";
	}else if(aValue == 'Complaint Index'){
          msg += ":\n";
          msg += "The company's Complaint Index is calcuated by dividing the company's Complaint Share ";
          msg += "(under the policy type) by the company's Premium Share (under the policy type).";
	}else if(aValue == 'National Median Complaint Index'){
          msg += ":\n";
          msg += "The National Median Complaint Index is calculated as the median index of all ";
          msg += "complaint indices, nation-wide, under the policy type for the calendar year.  ";
          msg += "This value represents the 'center' of the complaint indices for the policy type ";
          msg += "for the calendar year, in which 50% of the companies exhibit higher indices (more complaints) ";
          msg += "and 50% of the companies exhibit lower indices (less complaints).  Please note that this figure is calculated ";
          msg += "on a monthly basis, as complaints data for the current year, as well as previous years, ";
          msg += "is continually received by NAIC throughout the year. ";
	}else if(aValue == 'Complaint Share'){
          msg += ":\n";
          msg += "The company's Complaint Share is determined by dividing the company's Total Complaints ";
          msg += "by the sum of the complaints of all companies under the policy type for ";
          msg += "the calendar year.  This calculation provides the company's share of ";
          msg += "all complaints, nation-wide, under the policy type for the calendar year. ";
	}else if(aValue == 'Total Complaints'){
        msg += ":\n";
        msg += "The company's total complaints under the policy type for the ";
        msg += "calendar year are summed as \"Total Complaints.\" ";
        msg += "Note that \"Total Complaints\" include only confirmed complaints, which are defined as follows: \n";
        msg += "\n";
        msg += "A Confirmed complaint is a complaint in which the state department of insurance determines: \n";
        msg += "a) The insurer, licensee, producer, or other regulated entity committed any violation of: \n";
        msg += "   1) an applicable state insurance law or regulation;\n" ;
        msg += "   2) a federal requirement that the state department of insurance has the authority to enforce; or \n"; 
        msg += "   3) the term/condition of an insurance policy or certificate; or \n";
        msg += "b) The complaint and entity's response, considered together, indicate that the entity was in error. \n";
        msg += "\n";
        msg += "Confirmed Complaints include those complaints in which one of the complaint resolution codes used \n";
        msg += "by the state, also known as the \"complaint disposition,\" upheld the consumer's complaint position. \n";
        msg += "Complaint resolutions that uphold a consumer's complaint position are as follows \n";
        msg += "(implemented on 12/13/2010): Compromised Settlement/Resolution; Claim Reopened; \n";
        msg += "Claim Settled; Fine; Referred for Disciplinary Action; Company Position Overturned. \n";
        msg += "\n";
        msg += "Please refer to the CIS Help for a description of these resolutions. \n" ;
	}else if(aValue == 'Premium Share'){ //same as Market Share (below)
          msg += ":\n";
          msg += "The company's Premium Share is calculated by dividing the company's Total Premiums written ";
          msg += "by the sum of the premiums of all companies under the policy type for ";
          msg += "the calendar year.  This calculation provides the company's share of ";
          msg += "all premiums, nation-wide, under the policy type for the calendar year.";
	}else if(aValue == 'U.S. Market Share'){
          msg += ":\n";
          msg += "The company's U.S. Market Share is calculated by dividing the company's Total Premiums written ";
          msg += "by the sum of the premiums of all companies under the policy type for ";
          msg += "the calendar year.  This calculation provides the company's share of ";
          msg += "all premiums, nation-wide, under the policy type for the calendar year.";
	}else if(aValue == 'Total Premiums'){
          msg += ":\n";
          msg += "The company's Total Premiums are calculated as the sum of all premiums written, ";
          msg += "nation-wide, for the company under the policy type for the calendar year.\n";
          msg += "Please note that premiums only represent business within the U.S.,\n";
          msg += "since complaints are not collected for business outside the U.S.";
  }else if(aValue == 'Company Name'){
          msg += ":\n";
          msg += "To view information about a company you must first locate the company in the NAIC Database.";
          msg += " Please note that required information for locating a company is indicated with a red asterisk (*).";
          msg += " Please be aware that an insurance company may have different subsidiaries with different names.";
          msg += " Please check your policy for the correct name.";
          msg += " If you can not find the company you are looking for, try using part of the legal name. " ;
          msg += " (Example: American Insurance Company Inc. try just American Ins) ";
  }else if(aValue == 'Company Code'){
          msg += ":\n";
          msg += "To view information about a company you must first locate the company in the NAIC Database.";
          msg += " Please note that required information for locating a company is indicated with a red asterisk (*).";
          msg += " Please be aware that the cocode is a 5 digit unique number assigned to an insurrance entity by the NAIC.";
          msg += " If you can not find the company you are looking for, try using the Company Name search. " ;
	}else if(aValue == 'State'){
          msg += ":\n";
          msg += "Select the state in which the company is doing business.";
	}else if(aValue == 'Business Type'){
          msg += ":\n";
          msg += "If you know the business type of the company, select it from the\n";
          msg += "drop-down in order to narrow your search. If you don't know the\n";
          msg += "business type of the company, select \'All\' ";
	}else if(aValue == 'Statement Type'){
          msg += ":\n";
          msg += "If you know the statement type of the company, select it from the\n";
          msg += "drop-down in order to narrow your search. If you don't know the\n";
          msg += "statement type of the company, select \'All\' ";
	}
	alert(msg);
}
function checkSearchHelp(form){
    var msg = "";
    //var emailCheck = new RegExp('^[a-z0-9]+([_|\.|-][a-z0-9]+)*@[a-z0-9]*[\.](com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
    var emailCheck = new RegExp('^[0-9a-zA-Z\.]+@[0-9a-zA-Z\.]+[\.](com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');  

    if(form.firstName.value.length < 1) msg += "Provide your first name\n";
    if(form.lastName.value.length < 1) msg += "Provide your last name\n";

    if(form.companyName.value.length < 1) msg += "Provide an insurance company name you are searching for.\n";

    if(form.comments.value.length < 1) msg += "Provide comments that may help us aid you in your search.\n";

    if(form.phone.value != "" ){
    	if(checkNumber(form.phone.value)){
			msg += "Provide a valid phone number\n";
     	}
     }else{
    	 msg += "Provide a phone number that we may contact you with.\n"
     }
    


    if (form.email.value.search(emailCheck)==-1 || form.email.value.length < 1) {
    	msg +=("Provide a valid email address.");
    }


   if(msg == ""){
        return true;
    }else{
        alert("_________________________________________________________\n\n" +
              "The information you are searching for was not submitted due to the following error(s).\n" +
              "Please correct these error(s) and re-submit for confirmation.\n" +
              "_________________________________________________________\n\n" +

              msg);

        return false;
    }
}



function moveArrow (score) {

if (score < 0.0){
	 score = 0.0
}
else if (score > 2.0){
	 score = 2.0
}
		var arrowWidth = 9; // Total width of arrow indicator in px
	    var distance = score * 180 + arrowWidth;
	    $('.arrowWrapper .arrow').css('margin-left', distance + 'px');
	}



