if(!window.ND){window.ND={};window.NextDigital=window.ND;}
/* 2008/01/10 - ND.addLoadListener v0.82 - Glenn Baker */
ND.addLoadListener=function(fn){if(typeof window.addEventListener!='undefined'){window.addEventListener('load',fn,false);}else if(typeof document.addEventListener!='undefined'){document.addEventListener('load',fn,false);}else if(typeof window.attachEvent!='undefined'){ window.attachEvent('onload',fn);}else{var oldfn=window.onload;if(typeof window.onload!='function'){window.onload=fn;}else{window.onload=function(){oldfn();fn();};}}};
/* 2008/01/14 - ND.anchors v0.99 - Glenn Baker - Support for, external, popup:XXX:XXX, close, and print. Includes PopupWindow(url,n,w,h,sc) */
ND.anchors={storage:{external:function(){var ex=window.open(this.href);return !ex;},popup:function(){var dims=arguments&&arguments.length==2?arguments:window.__popupDims;popupWindow(this.href,'name',dims[0],dims[1],'yes');return false;},close:function(){window.close();return false;},print:function(){window.print();return false;}},init:function(){if(this !=ND.anchors){return ND.anchors.init();}window.popupWindow=this.popupWindow;if(!window.__popupDims)window.__popupDims=[619,380];var atype=window.__aType?window.__aType:"rel";if(document.getElementsByTagName&&document.getElementById){var anchors=document.getElementsByTagName("a");for(i=0;i<anchors.length;i++){var attr=String(atype=="class"?anchors[i].className:anchors[i].getAttribute(atype));if(attr){var matches=attr.match(/([^\s])+/g);for(var j=0;j<matches.length;j++){var match=matches[j].replace(/^([\w]*)[:|][^,\s]+([,]?[^,\s]+)*$j/i,"$j1");var args=(matches[j].replace(match,"")).match(/[^,|:\s]+/g);if(this.storage[match]){anchors[i].onclick=this.Bind(this.storage[match], args);}}}}}},popupWindow:function(url,n,w,h,sc){var sw=screen.availWidth;var sh=screen.availHeight;lp=(sw)?(sw-w)/2:0;tp=(sh)?(sh-h)/2:0;se='height='+h+',width='+w+',top='+tp+',left='+lp+',scrollbars='+sc+',resizable=yes';win=window.open(url,n,se);win.focus();},add:function(rel, fn){if(!this.storage[rel])this.storage[rel]=fn;else throw("Behaviour already exisits for '"+rel+"'");},setPopupDimensions:function(w,h){window.__popupDims=[w,h];},setAttrType:function(a){window.__aType=a;},Bind:function(fn){var args=[];if(arguments.length>1){args=(new String(arguments[1])).split(",");}return function(){return fn.apply(this, args);};}};
/* 2007/12/05 - ND.classFunctions v0.92 - Glenn Baker - includes addClass, removeClass, hasClass, toggleClass and replaceClass */
ND.classFunctions={addClass:function(t, c){var p=new RegExp("(^|)"+c+"(|$j)");if(!p.test(t.className)){if(t.className==""){t.className=c;}else{t.className+=" "+c;}}return true;},removeClass:function(t, c){var removedClass=t.className;var p=new RegExp("(^|)"+c+"(|$j)");removedClass=removedClass.replace(p, "$j1");removedClass=removedClass.replace(/$j/, "");t.className=removedClass;return true;},replaceClass:function(t, prec, c){removeClass(t,prec);addClass(t,c);},toggleClass:function(t, c){if(hasClass(t,c)){removeClass(t,c);}else{addClass(t,c);}},hasClass:function(t, c){var p=new RegExp("(^|)"+c+"(|$j)");return p.test(t.className);},init:function(){for(n in this){window[n]=this[n]}}};ND.classFunctions.init();

var setIEHovers = function(list) {
	var listItems = list.getElementsByTagName("li");

	for(var k=0; k < listItems.length; k++) {
		var node = listItems[k];
		node.onmouseover=function() {this.className+=" hover";}
		node.onmouseout=function() {this.className=this.className.replace(" hover", "");}
	}
}

var hoverIE = function() {
	// IE6 only
	if(window.ActiveXObject && window.external && (typeof window.XMLHttpRequest == "function")) {
		var ul_array = document.getElementsByTagName("ul");

		for (var i=0; i < ul_array.length; i++) {
			if(ul_array[i].parentNode.className == 'hoverable') {
				setIEHovers(ul_array[i]);
			}
		}
	}
}

ND.anchors.setPopupDimensions(619,320);

var myInit = function(){hoverIE();ND.anchors.init();initOverLabels();};
ND.addLoadListener(myInit);


/* Overlabels */
var hideLabel = function(field_id, hide){
	var field_for;
	var labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute('for');
		if (field_for == field_id && hasClass(labels[i],"overlabel-apply")) {
			labels[i].style.display = (hide) ? 'none' : 'block';
			return true;
		}
	}
};

var initOverLabels = function(){
	if (!document.getElementById) return;  	
	var labels, id, field;
	labels = document.getElementsByTagName('label');
	
	for (var i=0; i < labels.length; i++) {
	    if (labels[i].className == 'overlabel') {
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id))){continue;}
			labels[i].className = 'overlabel-apply';		
			if (field.value !== ''){hideLabel(field.getAttribute('id'), true);}
			field.onfocus = function(){
			    hideField(this.getAttribute('id'));
			    hideLabel(this.getAttribute('id'), true);
			};
			
			field.onblur = function(){
			    checkPassword(this.getAttribute('id'));
				if(this.value === ''){
					hideLabel(this.getAttribute('id'), false);
				}
			};
			labels[i].onclick = function(){
				var id, field;
				id = this.getAttribute('for');
				if(id && (field = document.getElementById(id))) {
					field.focus();
				}
			};
		}
	}
};

var checkPassword = function(currentFieldID)
{
    if(currentFieldID == searchKeyword)
    {
        emailField = document.getElementById(searchKeyword);
        
        if(emailField.value != '')
        {
            hideLabel(emailField, true);
        }
        
        
        if(emailField.value == ''){
            hideLabel(searchKeyword, false);
        }
    }
};

var hideField = function(currentFieldID)
{
    if(currentFieldID == searchKeyword )
    {
        hideLabel(searchKeyword, true);
    }
};

var $j = jQuery.noConflict();
$j(document).ready(function(){
		  
   //ADD another product 
    $j("DIV.products-wrap ul.product li div.product:not(:first)").hide();
	$j("DIV.products-wrap ul.product li span.switch:first").addClass("close");
	$j("DIV.products-wrap ul.product li span.switch:not(:first)").addClass("open");		 				   
	$j("div.products-wrap ul.product li").each(function (index){
		$j(this).children("span.switch").click(function (){												
			//alert($j(this)[0].className);
			if($j(this)[0].className=="switch close"){
				$jthis_div = $j(this).siblings("div.product");
				$j(this).attr("class","switch open");
				$jthis_div.hide();
				return;
			}else if($j(this)[0].className=="switch open"){	
				$j("div.products-wrap ul.product li").each(function (index){
					 $jdiv = $j(this).children("div.product");	 
				if($jdiv.is(":visible")){
					 $j(this).children("span.switch").attr("class" , "switch open");
					$jdiv.hide();
				 }
				 });			
				$jthis_div = $j(this).siblings("div.product");
				$j(this).attr("class","switch close");
				$jthis_div.show();
			}
		});
	});
	 
	 var productSize = $j("div.products-wrap ul.product li").size() + 1; 	 
	 for(var i = 1; i < productSize; i++){		 
			$j('#addoption' + i).click(function() {				
				var idIndex = this.id.substring('addoption'.length,this.id.length);								
				var idToAdd = $j('#markets' + idIndex + ' option:selected').val();
				var textToAdd = $j('#markets' + idIndex + ' option:selected').text();
				if (idToAdd != undefined && idToAdd != "")
				{
				    var result = $j("input[name='marketsResult" + idIndex + "']").val();
				    var resultString = $j("input[name='marketsResultString" + idIndex + "']").val();
				    if (result != "")
				    {
				        $j("input[name='marketsResult" + idIndex + "']").attr("value", result + "|" + idToAdd);
				        $j("input[name='marketsResultString" + idIndex + "']").attr("value", resultString + ", " + textToAdd);
				    }
				    else
				    {
				        $j("input[name='marketsResult" + idIndex + "']").attr("value", idToAdd);
				        $j("input[name='marketsResultString" + idIndex + "']").attr("value", textToAdd);
				    }				    
				}
				
				$j('#markets' + idIndex + ' option:selected').appendTo('#selectedMarkets' + idIndex);
								
				return false;
			});
			$j('#removeoption' + i).click(function() {		   
				var idIndex = this.id.substring('removeoption'.length,this.id.length);				
				var idToRemove = $j('#selectedMarkets' + idIndex + ' option:selected').val();
				var textToRemove = $j('#selectedMarkets' + idIndex + ' option:selected').text();
				if (idToRemove != "")
				{
				    var ids = $j("input[name='marketsResult" + idIndex + "']").val();
				    var ids = ids.replace("|" + idToRemove, "");
					var ids = ids.replace(idToRemove + "|", "");
					var ids = ids.replace(idToRemove, "");
					$j("input[name='marketsResult" + idIndex + "']").attr("value", ids);	
					
					var marketString = $j("input[name='marketsResultString" + idIndex + "']").val();	
					var marketString = marketString.replace(", " + textToRemove, "");
					var marketString = marketString.replace(textToRemove + ", ", "");
					var marketString = marketString.replace(textToRemove, "");	
					$j("input[name='marketsResultString" + idIndex + "']").attr("value", marketString);			
				}
				
				$j('#selectedMarkets'  + idIndex + ' option:selected').appendTo('#markets' + idIndex);
				
				return false;
		    });		 
	 }
	 	 
	 $j("div.add-button a").click(function(){
		    var $janother =	$j("div.another-product").html();		    
		    var productSize = $j("div.products-wrap ul.product li").size() + 1;	        
		    $j("div.products-wrap ul.product").append($janother.replace(/\{0\}/g, productSize));
		    $j("span.delete").click(function(){
				$j(this).parent("li").hide();
				$j(this).parent("li").children().remove();
			  });		    
		    $j("div.products-wrap ul.product li:not(:last) div.product").hide();
		    $j("div.products-wrap ul.product li:not(:last) span.switch").attr("class","switch open");
		    $j("div.products-wrap ul.product li:last span.switch").attr("class","switch close");
		    $j("div.products-wrap ul.product li:last span.switch").click(function (){										
			   if($j(this)[0].className=="switch close"){
			    $jthis_div = $j(this).siblings("div.product");
			    $j(this).attr("class","switch open");
			    $jthis_div.hide();
			    return;
			  }else if($j(this)[0].className=="switch open"){	
			    $j("div.products-wrap ul.product li").each(function (index){
				     $jdiv = $j(this).children("div.product");				 
			    if($jdiv.is(":visible")){
				     $j(this).children("span.switch").attr("class" , "switch open");
				    $jdiv.hide();
			     }
			     });
			    $jthis_div = $j(this).siblings("div.product");
			    $j(this).attr("class","switch close");
			    $jthis_div.show();
			   }
		    });
			 
			$j('#addoption' + productSize).click(function() {
			    var idToAdd = $j('#markets' + productSize + ' option:selected').val();
			    var textToAdd = $j('#markets' + productSize + ' option:selected').text();			    
				if (idToAdd != undefined && idToAdd != "")
				{
				    var result = $j("input[name='marketsResult" + productSize + "']").val();
				    var resultString = $j("input[name='marketsResultString" + productSize + "']").val();
				    if (result != "")
				    {
				        $j("input[name='marketsResult" + productSize + "']").attr("value", result + "|" + idToAdd);
				        $j("input[name='marketsResultString" + productSize + "']").attr("value", resultString + ", " + textToAdd);
				    }
				    else
				    {
				        $j("input[name='marketsResult" + productSize + "']").attr("value", idToAdd);	
				        $j("input[name='marketsResultString" + productSize + "']").attr("value", textToAdd);			    
				    }
				}			
			
				$j('#markets'  + productSize + ' option:selected').appendTo('#selectedMarkets' + productSize);
				return false;
				   });
				$j('#removeoption' + productSize).click(function() {
				var idToRemove = $j('#selectedMarkets' + productSize + ' option:selected').val();
				var textToRemove = $j('#selectedMarkets' + productSize + ' option:selected').text();				
				if (idToRemove != "")
				{
				    var ids = $j("input[name='marketsResult" + productSize + "']").val();
				    var ids = ids.replace("|" + idToRemove, "");
					var ids = ids.replace(idToRemove + "|", "");
					var ids = ids.replace(idToRemove, "");
					$j("input[name='marketsResult" + productSize + "']").attr("value", ids);
					
					var marketString = $j("input[name='marketsResultString" + productSize + "']").val();	
					var marketString = marketString.replace(", " + textToRemove, "");
					var marketString = marketString.replace(textToRemove + ", ", "");
					var marketString = marketString.replace(textToRemove, "");	
					$j("input[name='marketsResultString" + productSize + "']").attr("value", marketString);			
				}
				
				$j('#selectedMarkets'  + productSize + ' option:selected').appendTo('#markets' + productSize);
				
				return false;
		     });
			
			$j("input[name='productsCount']").attr("value", productSize);  
	    });    
	
	  //Edit Product
	   $j('#addoption').click(function() {
				var id = $j('#markets  option:selected').val();
				if (id != undefined && id != "")
				{					
					var result = $j("input[name='marketsResult']").val();
					if (result != "")
					{
						$j("input[name='marketsResult']").attr("value", result + "|" + id);
					}
					else
					{
						$j("input[name='marketsResult']").attr("value", id);
					}
				}
				$j('#markets  option:selected').appendTo('#selectedMarkets');				
				return false;
				   });
				$j('#removeoption').click(function() {
				var idToRemove = $j('#selectedMarkets  option:selected').val();
				if (idToRemove != "")
				{
					var ids = $j("input[name='marketsResult']").val();
					var ids = ids.replace("|" + idToRemove, "");
					var ids = ids.replace(idToRemove + "|", "");
					var ids = ids.replace(idToRemove, "");					
					$j("input[name='marketsResult']").attr("value", ids);					
				}
				
				$j('#selectedMarkets option:selected').appendTo('#markets');
				return false;
		 });	
              
      //Table Interlaces discoloration
	  $j("TABLE.organisation TBODY TR:even").addClass("odd");
	  $j("TABLE.logo-fee TR").removeClass();
	  
	  $j("li.print a, a#print").click(function(){
             window.print();
			 return false;
       })   
	  
    });
