/** 
* Toggle edit mode
**/
function editmode() {
	var href = window.location.href;
	if (href.indexOf("p_edit=true") != -1) {
		window.location.href = href.replace("true","false");
	} else if (href.indexOf("p_edit=false") != -1) {
		window.location.href = href.replace("false","true");
	} else if (href.indexOf("?") != -1) {
		window.location.href = href + "&p_edit=true";
	} else {
		window.location.href = href + "?p_edit=true";
	}
}

function show_form(obj){
  if (obj.selectedIndex != "0"){
    window.open(obj.value);
  }
}

/**
* Workaround to be able to open office documents in a new window.
**/
function show_form2(obj){
  if (obj.selectedIndex != "0"){
    window.open('/ikbViewer/page/gard/doc?p_url='+obj.value);
  }
}

function show_page(obj){
  if (obj.selectedIndex != "0"){
    window.location.href = obj.value;
  }
}

function show_product(obj){
  
  if (obj.selectedIndex != "0"){
    if (obj.value.charAt(0)>0){
      call_ikb_homeplace(obj.value,'TITLE_REDIRECT','10','2','7641','17567','0','');
    }else{
      window.open(obj.value);
    }
  }
}

function language_claims(obj){
   if (obj != '0'){
   window.location = '/ikbViewer/page/covering-risks/small-craft/claims?p_lang=' + obj;
  }
}

function language_application(obj){
   window.location = '/ikbViewer/page/covering-risks/small-craft/form?p_lang=' + obj;
}

var xcord = 900;
var ycord = 430;
/* Used to position mouse over on list of correspondents map */
function setDiv(id) {
  if (jQuery('#europe').css('display') != 'block' && jQuery('#africa').css('display') != 'block' && jQuery('#northamerica').css('display') != 'block' && jQuery('#southamerica').css('display') != 'block' && jQuery('#oceania').css('display') != 'block' && jQuery('#asia').css('display') != 'block' ) {
    jQuery(id).css({'top' : ycord, 'left' : xcord});
    jQuery(id).show();
  }
}

jQuery('#corr_map').mousemove(function(e) {
  xcord = e.pageX - 30;
  ycord = e.pageY - 30;
});


/***** LoC PDF *************************************/
var tree;
var form;
var win;
function openCorrList(element) {
	iKnowBase.withExt3(function() {
        tree = new Ext.tree.TreePanel({
        title: 'Use the menu below to select the Regions, Countries, or Ports in your trading area. The custom List of Correspondents will be sent to your email address.',
        height: 300,
        width: 500,
        useArrows:false,
        autoScroll:true,
        animate:true,
        enableDD:false,
        containerScroll: true,
        rootVisible: false,
        frame: true,
        root: {
            nodeType: 'async'
        },
        
        // auto create TreeLoader
        dataUrl: '/ikbViewer/page/modulepage/making-contact/loc/json',        
        listeners: {
            'checkchange': function(node, checked){
                if(checked){
                  selectChild(node, true);
                }else{
		  deselectParent(node);
                  selectChild(node, false);
                }
            }
        }
		});

		form = new Ext.form.FormPanel({
			title   : 'Enter your e-mail address',
			autoHeight: true,
			width   : 500,
			labelWidth   : 130,
			
			bodyStyle: 'padding: 5px',
			defaults: {
				anchor: '0'
			},
			items   : [
				{
					xtype     : 'textfield',
					name      : 'email',
					fieldLabel: 'Email Address',
					anchor    : '-20'
				},
				{
					xtype     : 'checkbox',
					name      : 'key',
					fieldLabel: 'Include a list of Gard claims personnel',
					anchor    : '-20'
				}]
		});

		win = new Ext.Window({
					xlayout:'fit',
					width:500,
					height:500,
					closeAction:'close',
					plain: true,
					items: [tree,
						form],
							buttons: [{
				text: 'Order PDF',
				handler: function(){
					var msg = '', area = '',  country = '', place = '', selNodes = tree.getChecked();
					Ext.each(selNodes, function(node){
						if (node.attributes.placeid != null) {
							if (!node.parentNode.attributes.checked) {
								if(msg.length > 0){
									msg += ',';
								}
								if(place.length > 0){
									place += ',';
								}
								msg += node.attributes.placeid;
								place += node.attributes.placeid;
							} 
						} else if (node.attributes.countryid != null) {
							if (!node.parentNode.attributes.checked) {
								if(msg.length > 0){
									msg += ',';
								}
								if(country.length > 0){
									country += ',';
								}
								msg += node.attributes.countryid;
								country += node.attributes.countryid;
							}
						} else if (!node.parentNode.attributes.checked) {
							if(msg.length > 0){
								msg += ',';
							}
							if(area.length > 0){
								area += ',';
							}
							area += node.attributes.areaid;
							msg += node.attributes.countryid;
						}
					});
					if (place == '') place = 0;
					if (country == '') country = 0;
					if (area == '') area = 0;
					if (place == 0 && country == 0 && area == 0 ) alert('Please choose correspondents');
					else if (form.form.getValues().email == '' || form.form.getValues().email.indexOf('@') == -1) alert('Please enter a valid e-mail address');
					else orderPDF(form.form.getValues().email, form.form.getValues().key, area, country, place);
				}
			},{
						text: 'Close',
						handler: function(){
							win.close();
						}
					}]
				});
		win.show();
		tree.getRootNode().expand(false);
	});
}

function selectChild (node, check){
    selModel = node.getOwnerTree().getSelectionModel();
    if (node.hasChildNodes) {
        node.expand();
        node.eachChild(function(child){
			child.getUI().toggleCheck(check);
            selectChild(child, check);
        });
    }
}

function deselectParent(node) {
  if (node.nodeParent != tree.getRootNode()) {
	  tree.suspendEvents(false);
	  node.parentNode.getUI().toggleCheck(false);
	  tree.resumeEvents();  
	  if (node.parentNode.parentNode != tree.getRootNode()) {
		deselectParent(node.parentNode);
	  }
  }
}

function orderPDF(email, key, area, country, place) {
    var p_key = 2;
    if (key == 'on') {
      p_key = 1;
    }
  jQuery.post('/ikbViewer/pls/gard_loc.orderPDF', {p_mail:email, p_key: p_key, p_area: area, p_country: country, p_place: place}, function(data) {
    if (data.indexOf('OK') == -1) {
	alert(data);
    } else {
	 jQuery.post('/ikbViewer/pls/gard_loc.createPDF', {p_document_id: data.substring(data.indexOf('-')+1,data.length).trim(), p_mail:email, p_key: p_key, p_area: area, p_country: country, p_place: place});
	}
     win.close();
  });
}
