function selectCheckbox(id) {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
    	  if ((inputs[i].type == 'checkbox') && (inputs[i].id.indexOf(id) > 0)) {
        	inputs[i].checked = !inputs[i].checked;
        }
    }
    dsap();
    return false;
}

function ifSelectedCheckbox(id,selectall,noselectedmsg,selectedmsg) {
    var inputs = document.getElementsByTagName("input");
    var ischeked = "no";
    for (var j = 0; j < inputs.length; j++) {
		if (((selectall == 'true') || (inputs[j].checked == true)) && (inputs[j].type == 'checkbox') && (inputs[j].id.indexOf(id) > 0)) {
			ischeked = "ok";
		}
    }
    if (ischeked == "ok"){
	    var agree=confirm(selectedmsg+"?");
	    if (agree){
	    	return true ;
	    }else{
	    	return false ;
	    }
    }else{
	    alert(noselectedmsg);
    }
}

function lmgCheck(elem, yesId, noId, subform) {
	yesElement = window.document.getElementById(elem.id + ":_1");
	noElement = window.document.getElementById(elem.id + ":_2");
	try{
		if (yesElement.checked) {
			if(checkIfExists(subform + yesId)){
				document.getElementById(subform + yesId).style.display = 'inline';
			}
			if(checkIfExists(subform + noId)){
				document.getElementById(subform + noId).style.display = 'none';
				// to clear all input fileds if user change mind
				// ClearAllInputFields(subform + noId);
			}
		} else if (noElement.checked) {
			if(checkIfExists(subform + noId)){
				document.getElementById(subform + noId).style.display = 'inline';
			}
			if(checkIfExists(subform + yesId)){
				document.getElementById(subform + yesId).style.display = 'none';
				// to clear all input fileds if user change mind
				// ask Markus about clearing ClearAllInputFields(subform + yesId);
			}
		}
	}catch (e) {
	}	
}

function checkIfExists(subformId){
	if (document.getElementById(subformId)!=null){
		return true;
	}
	return false;
}


function ExtractMap(map){
	// map samle data:  
	// 1:10,3:56,4:88,7:0
	arr=map.split(',');
	map=new Array;
	for(i=0;i<=arr.length-1;i++){
		arr_str=arr[i];
		arr2=arr_str.split(":");	
		arr2_1=arr2[0];
		arr2_2=arr2[1];
		map[arr2_1]=arr2_2;
	}
	return map;
}


function lmgRadioCheck(elem, value, subform,map) {	
	map_arr=ExtractMap(map);
	
	 for(var i = 1; i <= value; i++){
		radioElement = document.getElementById(elem.id + ":_" + i);
		try{
			if(radioElement.checked){
				document.getElementById(subform + map_arr[radioElement.value]).style.display = 'inline';
			}else{
				document.getElementById(subform + map_arr[radioElement.value]).style.display = 'none';
			}
		}catch (e) {
			continue;
		}
	 }	 
}




function lmgTableCheck(elem, divForTable) {
	yesElement = document.getElementById(elem.id + ":_" + 1);
	if (yesElement.checked) {
		divForTable.style.display = 'inline';
	} else {
		divForTable.style.display = 'none';
	}
}


var interval;
function DisableButton(b) {
	
	 b.disabled = true;
	 inteval = setTimeout("EnableButton('"+b.id+"')",3000);
}
function EnableButton(id) {
	 document.getElementById(id).disabled = false;
	 clearTimeout(interval);
}

/* function for disabling image links after clicking */
jQuery(function(){
 jQuery("a.disable_link").click(function(){
   img = jQuery(this).html();
   jQuery(this).replaceWith(img);
 })
 
});


/* to clear all input fileds of subform if user change mind */
function ClearAllInputFields(subform_id){
	id="#"+subform_id+" input";
	jQuery(id).val("");
}


function clickAgain(){
	jQuery("input:radio:checked").trigger('click');
} 


 function openPriceField(elem,locationId){
	 selected = document.getElementById(elem.id + ":_2");
	 if(selected.checked){
		 jQuery("div#price"+locationId).css({'display':'inline'});
	 }else{
		 jQuery("div#price"+locationId).css({'display':'none'});
	 }
 
 }
 
 function clearZone(){
		jQuery(".subzoneClass").val("");	
	}
 


 
 var inputFieldExtraction = function(node) {  
	if(node.childNodes[0] && node.childNodes[0].hasChildNodes()) {
		node=node.childNodes[0];
	}
	if(jQuery(node).find("input").length>0){
		t=jQuery(node).find("input").val();
	} else {
		t=node.innerHTML;
	}
	return t;
}; 
 
function tableSort(){
	jQuery('.tbl_sorter').tablesorter({textExtraction: inputFieldExtraction});	 
}
 
 

/* function for checking for required fields */
function fireEffect(f){
	f.attr('style','background-color:#EFB7A4');
}
function clearEffect(f){
	f.attr('style','');
}

function validateRequiredInputs(f){
	jQuery("table:not(.tbl_sorter):has(td."+f+")").attr('style','border-collapse:separate;border-spacing:2px;');
	
	jQuery('td.'+f).each(function(i){
		var td=jQuery(this);
		td.attr('style','');
		td.find('fieldset.iceSelMnyCb.required, fieldset.iceSelOneRb.required').each(function(i){
			var l=jQuery(this).find('input:checked').length;
			if (l==0){
				fireEffect(td);
			} else {
				clearEffect(td);
			}
		});
		
		td.find('input.required, .iceInpTxtArea.required ,.iceSelOneMnu.required, textarea.required').each(function(i){
			if (jQuery(this).val()=="" || jQuery(this).val()=="org.jboss.seam.ui.NoSelectionConverter.noSelectionValue" ){
				fireEffect(td);
			} else {
				clearEffect(td);
			}
		});
		
		td.find('input:checkbox.required').each(function(i){
			if (!jQuery(this).is(':checked')){
				fireEffect(td);
			} else {
				clearEffect(td);
			}
		});
		
	});
	
}
 var enable_scroll;
 function scrollUp(){
	 enable_scroll=true;
	 // mark required unanswered fields
	 //validateRequiredInputs("answerInput");       
	 //validateRequiredInputs("featureAnswer");
 }

function fixMessages() {
	jQuery(".messages-box:gt(0)").each(function(x){
		jQuery(this).css('display','none');
	});

	var a = new Array();
	jQuery(".messages-box ul").children("li").each(function(x){
			//console.info(jQuery(this).html());
	        test = false;
	        b = a[x] = jQuery(this).html();
	        n=a.length-1;
	        for(i=0;i < n;i++){
	                if (b ==a[i]) test =true;
	        }
	       if (test) jQuery(this).remove();
	}) ;	
}



function checkBackSpace(e){
	t=e.target.tagName;
	kc=e.keyCode;
	if(kc==8 & t=="HTML"){
		//alert("back space");
		e.stopPropagation();
		return false;
	}
	return true;
}

jQuery(function(){
	jQuery("body").attr("onkeydown","return checkBackSpace(event);");

});

document.onkeydown = function() {
	var t=event.srcElement.tagName;
    var kc=event.keyCode;
    if(kc==8 && t!="INPUT" && t!="SELECT" && t!="TEXTAREA" && t!="SUBMIT"){
		return false;
    }
	return true;
}



var enable_scroll_to_previous_contracts;
function scrollToPreviousContracts(){
	enable_scroll_to_previous_contracts=true;
}


function isIe6(){
	return (Liferay.Browser.isIe() && Liferay.Browser.getMajorVersion() == "6.0");
}

function scrollIe6() {
	if (isIe6()) {
		if(!jQuery("div.icePnlPop").is(":visible")){
			h1=jQuery("#wrapper").height();
			h2=jQuery(window.document).height();
			document.documentElement.scrollTop=document.documentElement.scrollTop-(h2-h1);
		}
	}	
}

function servletLoaded(){
	jQuery(".iceOutConStatActv").css("visibility","hidden");
	jQuery("iframe#dervletIframe").remove();
}

function callServlet(url){
		//if(Liferay.Browser.isIe()){
			window.open(url,'Download');
		//} else {
		//	jQuery(".iceOutConStatActv").css("visibility","visible");
		//	jQuery("#download_iframe").append('<iframe id="servletIframe" style="display:none;" onload="servletLoaded();" src="'+url+'"></iframe>');
		//}
}

/* Function to deselect Sellect All pages checkbox */
function dsap(){
	var checkAll=jQuery('.select_all_checkbox');
	checked=checkAll.is(':checked');
	if(checked){
		checkAll.trigger('click');
	}
}


function deselectCc(c){
	c=jQuery(c);
	tr=c.parent().parent();
	cc=tr.find(".email_cc");
	if(c.is(':checked')){
		cc.attr("checked", false);
	}
}
function deselectTo(c){
	c=jQuery(c);
	tr=c.parent().parent();
	cc=tr.find(".email_to");
	if(c.is(':checked')){
		cc.attr("checked", false);
	}
}
function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}

function scrollFormsToTop(){
	if(Liferay.Browser.isIe() && Liferay.Browser.getMajorVersion() == "6.0")
	{
		w=jQuery("#navigation").width();
		jQuery('html, body').animate({scrollTop:0}, 'fast');
		jQuery('html, body').animate({scrollLeft:0}, 'fast');
		jQuery("#banner").css('width',w+'px');
		w1=w-jQuery(".lfr-dock").width();
		jQuery(".lfr-dock").css('left',w1+'px');
	}
}


function fixFormLayout(){
	jQuery(".bb, .sb").each(function(){
		var bb=jQuery(this);
		var maxWidth=0;
		bb.find(".answInp .lbl").each(function(){
			w=jQuery(this).width();
			if(w>maxWidth){
				maxWidth=w;
			}
		});
		bb.find(".answInp .lbl").css("width",maxWidth+"px");
		
		maxWidth=0;
		bb.find(".answInp").each(function(){
			var lbl=jQuery(this).find(".lbl").width();
			var answ=jQuery(this).find(".answ").width();
			var info=jQuery(this).find(".info").width();
			var nh=jQuery(this).find(".nh").width();
			w=lbl+answ+info+nh;
			if(w>maxWidth){
				maxWidth=w;
			}
		});
		bb.find(".answInp").css("width",maxWidth+16+"px");
	});
}


function attachToolTip(){
	jQuery(".tt").mouseover(function(e) { 
		var div=jQuery(this).parent().find("div.icePnlTlTip");
		if(div==null) return;
		div.css("display","block");
		div.css("position","absolute");
		div.css("top", e.pageY + 10 );  
		div.css("left", e.pageX + 20 );  
	}).mousemove(function(e) { 
		var div=jQuery(this).parent().find("div.icePnlTlTip");
		if(div==null) return;
		div.css("top", e.pageY + 10 );  
		div.css("left", e.pageX + 20 );  
	}).mouseout(function() { 
		var div=jQuery(this).parent().find("div.icePnlTlTip");
		if(div==null) return;
		div.css({"display":"none"});
	});
	
}


