var spouse = 0;
var children = 0;

add_spouse = new Image();
add_spouse.src = "/images/add-spouse2.jpg";
add_spouse_gray = new Image();
add_spouse_gray.src = "/images/add-spouse-gray2.jpg";

add_child = new Image();
add_child.src = "/images/add-child.jpg";
add_child_gray = new Image();
add_child_gray.src = "/images/add-child-gray.jpg";

function addSpouse() {
	if(spouse==0) {
		document.getElementById('spouse').style.display='';
		spouse = 1;
		document.images['add-spouse'].src=add_spouse_gray.src;
	}
}

function deleteSpouse() {
	document.getElementById('spouse').style.display='none';
	spouse = 0;
	document.images['add-spouse'].src=add_spouse.src;
	
	//Reset Spouse Values
	document.getElementById('insured_2_gender_male').checked=false;
	document.getElementById('insured_2_gender_female').checked=false;
	document.getElementById('insured_2_dobMM').value='';
	document.getElementById('insured_2_dobDD').value='';
	document.getElementById('insured_2_dobYYYY').value='';
	document.getElementById('insured_2_heightFT').value='';
	document.getElementById('insured_2_heightIN').value='';
	document.getElementById('insured_2_weight').value='';
	document.getElementById('insured_2_smoker_yes').checked=false;
	document.getElementById('insured_2_smoker_no').checked=false;
}

function addChild() {
	if(children<6) {
		children++;
		document.getElementById('child'+children).style.display='';
		if(children==6)
			document.images['add-child'].src=add_child_gray.src;
	}
}

function deleteChild(x) {
	for(i=(x+2);i<(children+2);i++) {
		document.getElementById('insured_'+i+'_gender_male').checked=document.getElementById('insured_'+(i*1+1)+'_gender_male').checked;
		document.getElementById('insured_'+i+'_gender_female').checked=document.getElementById('insured_'+(i*1+1)+'_gender_female').checked;
		document.getElementById('insured_'+i+'_dobMM').value=document.getElementById('insured_'+(i*1+1)+'_dobMM').value;
		document.getElementById('insured_'+i+'_dobDD').value=document.getElementById('insured_'+(i*1+1)+'_dobDD').value;
		document.getElementById('insured_'+i+'_dobYYYY').value=document.getElementById('insured_'+(i*1+1)+'_dobYYYY').value;
		document.getElementById('insured_'+i+'_heightFT').value=document.getElementById('insured_'+(i*1+1)+'_heightFT').value;
		document.getElementById('insured_'+i+'_heightIN').value=document.getElementById('insured_'+(i*1+1)+'_heightIN').value;
		document.getElementById('insured_'+i+'_weight').value=document.getElementById('insured_'+(i*1+1)+'_weight').value;
	}
	
	document.getElementById('child'+children).style.display='none';
	
	document.getElementById('insured_'+(children*1+2)+'_gender_male').checked=false;
	document.getElementById('insured_'+(children*1+2)+'_gender_female').checked=false;
	document.getElementById('insured_'+(children*1+2)+'_dobMM').value='';
	document.getElementById('insured_'+(children*1+2)+'_dobDD').value='';
	document.getElementById('insured_'+(children*1+2)+'_dobYYYY').value='';
	document.getElementById('insured_'+(children*1+2)+'_heightFT').value='';
	document.getElementById('insured_'+(children*1+2)+'_heightIN').value='';
	document.getElementById('insured_'+(children*1+2)+'_weight').value='';
	
	if(children==6)
		document.images['add-child'].src=add_child.src;
	
	children--;
}

function showHidden(id, value) {
	if(value=="yes")
		document.getElementById(id).style.display='';
	else
		document.getElementById(id).style.display='none';
}


var posx = 0;
var posy = 0;

function popup(s, event) {
	closeOthers(s);
	var e = event || window.event;
	var pos = getRelativeCoordinates(event, document.getElementById(s));
	document.getElementById(s).style.left=(posx*1+30)+"px";
	document.getElementById(s).style.top=(posy*1+30)+"px";
	document.getElementById(s).style.display='';
}

var hideDiv;
function hide(s) {
	hideDiv = s;
	window.setTimeout('hidePopup()',1300);
}

function hidePopup() {
	if(document.getElementById('address_1_state').style.display=='none') state();
	document.getElementById(hideDiv).style.display='none';
}

function closeOthers(s) {
	if(s!="health-info") document.getElementById('health-info').style.display='none';
	if(s!="health-conditions") document.getElementById('health-conditions').style.display='none';
	if(s!="contact-info") { document.getElementById('contact-info').style.display='none'; if(document.getElementById('address_1_state').style.display=='none') state(); }
	if(s!="security-info") document.getElementById('security-info').style.display='none';
}


/**
 * Retrieve the absolute coordinates of an element.
 *
 * @param element
 *   A DOM element.
 * @return
 *   A hash containing keys 'x' and 'y'.
 */
function getAbsolutePosition(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};

/**
 * Retrieve the coordinates of the given event relative to the center
 * of the widget.
 *
 * @param event
 *   A mouse-related DOM event.
 * @param reference
 *   A DOM element whose position we want to transform the mouse coordinates to.
 * @return
 *    A hash containing keys 'x' and 'y'.
 */
function getRelativeCoordinates(event, reference) {
  var x, y;
  event = event || window.event;
  var el = event.target || event.srcElement;

  if (!window.opera && typeof event.offsetX != 'undefined') {
    // Use offset coordinates and find common offsetParent
    var pos = { x: event.offsetX, y: event.offsetY };

    // Send the coordinates upwards through the offsetParent chain.
    var e = el;
    while (e) {
      e.mouseX = pos.x;
      e.mouseY = pos.y;
      pos.x += e.offsetLeft;
      pos.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Look for the coordinates starting from the reference element.
    var e = reference;
    var offset = { x: 0, y: 0 }
    while (e) {
      if (typeof e.mouseX != 'undefined') {
        x = e.mouseX - offset.x;
        y = e.mouseY - offset.y;
        break;
      }
      offset.x += e.offsetLeft;
      offset.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Reset stored coordinates
    e = el;
    while (e) {
      e.mouseX = undefined;
      e.mouseY = undefined;
      e = e.offsetParent;
    }
  }
  else {
    // Use absolute coordinates
    var pos = getAbsolutePosition(reference);
    x = event.pageX  - pos.x;
    y = event.pageY - pos.y;
  }
  // Subtract distance to middle
  return { x: x, y: y };
}


function hideSelect() {
	document.getElementById('address_1_state').style.display='none';
}

function showSelect() {
	window.setTimeout('state()', 1300);
}

function state() {
	document.getElementById('address_1_state').style.display='';
}

