// JavaScript Document
var j = jQuery.noConflict();
j(document).ready(function(){
    j("#approved_person_form").validate();
});

var firmrows = 0;
var siterows = 0;

function toggleLayer(whichLayer, visibility)
{
 if (document.getElementById)
 {
  // this is the way the standards work
  var style2 = document.getElementById(whichLayer).style;
  style2.display = visibility;
 }
 else if (document.all)
 {
  // this is the way old msie versions work
  var style2 = document.all[whichLayer].style;
  style2.display = visibility;
 }
 else if (document.layers)
 {
  // this is the way nn4 works
  var style2 = document.layers[whichLayer].style;
  style2.display = visibility;
 }
}

function toggleTickboxLayer(whichLayer, value) {
	var exisiting = document.getElementById(whichLayer).style.display;
	if (exisiting == 'none') {
		toggleLayer(whichLayer, '');
	} else {
		toggleLayer(whichLayer, 'none');
	}
}


function toggleToggleLayer(whichLayer, value) {
	if (value) {
		toggleLayer(whichLayer, '');
	} else {
		toggleLayer(whichLayer, 'none');
	}
}

function checkNum(){
	var carCode = event.keyCode;
	if (((carCode < 48) || (carCode > 57)) && (carCode != 46)) {
		event.cancelBubble = true
		event.returnValue = false;
	}
}

function show_sales_types() {
	sales_types = document.sales_form.sales_type;
	for (var i=0; i<sales_types.length; i++) {
		if (sales_types[i].checked) {
			toggleToggleLayer('layer_'+sales_types[i].value, 1);
		} else {
			toggleToggleLayer('layer_'+sales_types[i].value, 0);			
		}
	}
}

function toggle_sig_pads(optionId) {
	if (optionId in {'2':'', '4':''}) {
		toggleToggleLayer('layer_sigpads', 1);
	} else {
		toggleToggleLayer('layer_sigpads', 0);			
	}
}

function showLayerOnValueExists(layer, requiredValue, thisObj) {
	for (var i=0; i<thisObj.length; i++) {
		if (thisObj[i].checked) {
			if (thisObj[i].value == requiredValue) {
				toggleToggleLayer(layer, 1);
				return true;
			}
		}
	}
	toggleToggleLayer(layer, 0);
	return false;
}

function showLayerOnValue(layer, requiredValue, value, tests) {
	if (requiredValue == value) {
		toggleToggleLayer(layer, 1);
	} else if (tests <= 1) {
		toggleToggleLayer(layer, 0);
	}
}

function confirmIntroducer(appTypeValue) {
	if (appTypeValue == 'TRAVELINTRODUCER') {
		if (confirm('IMPORTANT: The scope of activity for an Introducer AR will be limited to distributing insurance literature and passing details of prospective customers on to an authorised firm. You will not be able to sell insurance, collect premiums (even if this is included within the price of a holiday or free of charge) or discuss any particular policies with customers, including claims issues. If you do wish to undertake any of these restricted activities you will need to become a full Appointed Representative.\n\nClick OK to confirm you wish to become an Introducer AR otherwise click CANCEL.')) {
			return true;
		} else {
			document.getElementById('app_type').value = 'TRAVEL';
		}
	}
}

function introducerVisibility(introValue, layer) {
	for (var i in introducersName) {
		if (introducersName[i] == introValue) {
			if (introducersReps[i] == 1) {
				document.getElementById(layer).style.display = '';
			} else {
				document.getElementById(layer).style.display = 'none';
			}
		}
	} 
}

function addfirmRow(id){
	firmrows ++;

	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
	var row = document.createElement("TR")
	var td1 = document.createElement("TD")
	
	// Create a text field
 	textField = document.createElement('INPUT');
 	textField.type = 'text';
 	textField.setAttribute('size', 40);
 	textField.setAttribute('name', 'firm_firmname_'+firmrows);
	
	td1.appendChild(textField);
	var td2 = document.createElement("TD")
	
	// Create a text field
 	textField = document.createElement('INPUT');
 	textField.type = 'text';
 	textField.setAttribute('size', 40);
 	textField.setAttribute('name', 'firm_jobtitle_'+firmrows);

	td2.appendChild (textField);
	row.appendChild(td1);
	row.appendChild(td2);
	tbody.appendChild(row);
}

function addsiteRow(id, showBm){
	siterows ++;
    
	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
	var row = document.createElement("TR")
	var td1 = document.createElement("TD")
	
	// Create a text field
 	var textField = document.createElement('INPUT');
 	textField.type = 'text';
 	textField.setAttribute('size', 25);
 	textField.setAttribute("name", 'site_sitename_'+siterows);
 	textField.setAttribute("id", 'site_sitename_'+siterows);
	
	td1.appendChild(textField);
	var td2 = document.createElement("TD")
	
	// Create a text field
 	var textField = document.createElement('INPUT');
 	textField.type = 'text';
 	textField.setAttribute("name", 'site_address1_'+siterows);
 	textField.setAttribute("id", 'site_address1_'+siterows);
 	textField.setAttribute('size', 25);

	td2.appendChild (textField);
	
	var td3 = document.createElement("TD")
	
	// Create a text field
 	var textField = document.createElement('INPUT');
 	textField.type = 'text';
 	textField.setAttribute("name", 'site_towncity_'+siterows);
 	textField.setAttribute("id", 'site_towncity_'+siterows);
 	textField.setAttribute('size', 25);

	td3.appendChild (textField);
	
	var td4 = document.createElement("TD")
	
	// Create a text field
 	var textField = document.createElement('INPUT');
 	textField.type = 'text';
 	textField.setAttribute("name", 'site_postcode_'+siterows);
 	textField.setAttribute("id", 'site_postcode_'+siterows);
 	textField.setAttribute('size', 10);

	td4.appendChild (textField);
	
	// Show BM?
    if (showBm == 1) {
        var td5 = document.createElement("TD")

        // Create a text field
        var textField = document.createElement('INPUT');
        textField.type = 'text';
        textField.setAttribute("name", 'site_bm_'+siterows);
        textField.setAttribute("id", 'site_bm_'+siterows);
        textField.setAttribute('size', 25);

        td5.appendChild (textField);
    }
	
	row.appendChild(td1);
	row.appendChild(td2);
	row.appendChild(td3);
	row.appendChild(td4);
    // Show BM?
    if (showBm == 1) {
        row.appendChild(td5);
    }
	tbody.appendChild(row);
}

function multiplyEntsRows(noRows) {
	if (noRows > 1) {
		return true;
	} else {
		return false;
	}
}

function multiplySiteRows(noRows, showBm) {
	if (noRows > 1) {
		if (noRows > siterows) {
			for (i = 1; i <= noRows; i ++) {
				if (i > siterows) {
					addsiteRow('sites_table', showBm);
				}
			}
		} else {	
			for (i = siterows; i > noRows; i --) {
				var trs = document.getElementById('sites_table').getElementsByTagName('tr');
				trs[i].parentNode.removeChild(trs[i]);
				siterows --;
			}
		}
		return true;
	} else {
		return false;
	}
}

/*
 * Slightly changed function copied from above, for SONYEU apps that need all branch information.
 */
function multiplySonySiteRows(noRows, showBm) {
    
    if (noRows > siterows) {
        for (i = 1; i <= noRows; i ++) {
            if (i > siterows) {
                addsiteRow('sites_table', showBm);
            }
        }
    } else {	
        for (i = siterows; i > noRows; i --) {
            var trs = document.getElementById('sites_table').getElementsByTagName('tr');
            trs[i].parentNode.removeChild(trs[i]);
            siterows --;
        }
    }
    
    return true;
        
}

function multiplyFirmRows(noRows) {
	if (noRows > 1) {
		if (noRows > firmrows) {
			for (i = 1; i <= noRows; i ++) {
				if (i > firmrows) {
					addfirmRow('firms_table');
				}
			}
		} else {	
			for (i = firmrows; i > noRows; i --) {
				var trs = document.getElementById('firms_table').getElementsByTagName('tr');
				trs[i].parentNode.removeChild(trs[i]);
				firmrows --;
			}
		}
		return true;
	} else {
		return false;
	}
}


function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOption(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
	  
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
    
  sortlist(theSelTo);
}

function sortlist(listElem) {
	var lb = listElem;
	arrTexts = new Array();
	
	for(i=0; i<lb.length; i++)  {
	  arrTexts[i] = lb.options[i].text;
	}
	
	arrTexts.sort();
	
	for(i=0; i<lb.length; i++)  {
	  lb.options[i].text = arrTexts[i];
	  lb.options[i].value = arrTexts[i];
	}
}

function selectAllOptions(selStr)
{
  var selObj = document.getElementById(selStr);
  for (var i=0; i<selObj.options.length; i++) {
    selObj.options[i].selected = true;
  }
}

function calcPercentage(fromField, toField) {
	fromValue = document.getElementById(fromField).value;
	toValue = 100 - fromValue;
	if (toValue == 0) {
		toValue = 0;
	}
	document.getElementById(toField).value = toValue;
}

function checkNum(){
	var carCode = event.keyCode;
	if (((carCode < 48) || (carCode > 57)) && (carCode != 46)) {
		event.cancelBubble = true;
		event.returnValue = false;
	}
}

checked=false;
function checkedAll (frm1) {
	var aa= document.getElementById('frm1');
	 if (checked == false)
          {
           checked = true
          }
        else
          {
          checked = false
          }
	for (var i =0; i < aa.elements.length; i++) 
	{
	 aa.elements[i].checked = checked;
	}
}

function showEmpSection(show, num) {
    if (show) {
        j('.emp-' + num).show();
        j('.nemp-' + num).hide();
    } else {
       j('.emp-' + num).hide();
       j('.nemp-' + num).show();
    }
}
