/**
 * THE PURPOSE OF THIS all.js FILE IS TO HOLD GENERAL JAVASCRIPT THAT CAN BE USED ANYWHERE IN THE APPLICATION
 **/

/*
 * POP-UP WINDOW
 * Use to create a general simple pop-up window
 */
function f_winOpen(p_file) {
	window.open(p_file,"","left=40,top=40,width=800,height=500,menubar=no,location=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,directories=no");
}


function toggleLang(obj) {
	
	if (obj.className == "cur") return;
			
	var ele =  document.getElementById(obj.getAttribute("targetName"));
	var opObjID = obj.id.match("_e")?obj.id.replace("_e","_c"): obj.id.replace("_c","_e");
	var opObj	= document.getElementById(opObjID);
	var opEle   = document.getElementById(opObj.getAttribute("targetName"));
	
		
	obj.className='cur';
	opObj.className = null;
	ele.className=ele.className.replace(" hidden","");
	opEle.className=opEle.className+' hidden';
		
}


function countLoadingTime(serverSideDuration){
	
	var clientSideEnd = new Date();	  
	document.getElementById("debugArea").innerHTML+= "The page loaded on Serverside spent: "+ serverSideDuration+ " seconds<br/>";
	document.getElementById("debugArea").innerHTML+= "The page loaded on Clientside  spent: "+
	               (clientSideEnd.getSeconds()-clientSideStart.getSeconds())+(clientSideEnd.getMilliseconds()-clientSideStart.getMilliseconds())/1000+" seconds";
	  	 
	
} 


var oPopWin;
			
function closePopup(){
 if (oPopWin){
	 oPopWin.close();
   //  window.onfocus= null;
 }
}	

function showPopup(obj){
   var winArg = 'status="1",dependent="1",scrollbars="0",toolbar="0",resizable="0"';
   
   //set by obj position
   var popupHeight= obj.getAttribute("popupHeight");
   var popupWidth = obj.getAttribute("popupWidth");
   var popupTop  = getTop(obj)- getScrollY();
   var popupLeft = getScrollX()>0?getLeft(obj)-getScrollX()-popupWidth:getLeft(obj)-popupWidth/3;

   winArg += ',width='+popupWidth+',height='+popupHeight+',top='+popupTop+',left='+popupLeft;
   oPopWin = window.open(obj.getAttribute("popupURL"),"GOWPopup",winArg);
   window.onfocus= closePopup;
   oPopWin.focus();
}

function getTop(obj){
	return (obj.offsetParent)?obj.offsetTop+getTop(obj.offsetParent):obj.offsetTop;
}

function getLeft(obj){
	return (obj.offsetParent)?obj.offsetLeft+getLeft(obj.offsetParent):obj.offsetLeft;
}

function getScrollY(){
			if(window.pageYOffset != null) {
			return window.pageYOffset;
			} else {
			return document.body.scrollTop;
			}
}

function getScrollX(){
			if(window.pageXOffset != null){
			return window.pageXOffset;
			} else {
			return document.body.scrollLeft;
			}
}  
  

function show(obj){
     if(!obj.className.match("hidden")) return;
     
     obj.className = obj.className.replace(/\s*hidden/,'');
    
    }

function hide(obj){
     if(obj.className.match("hidden")) return;
     
      obj.className+=' hidden';
    }


function f_address(obj,lang) {
				var v_postcode = document.getElementById(obj.getAttribute("base")).value;
		        v_postcode = v_postcode.replace(/\s/, "").toUpperCase();
				if (!(lang==1)){
				alert("The Address Finder only supports English version. Please switch your language to English to use this function.");
				return;
				}
				if (v_postcode.length ==0 ) {
				alert("Please enter a Postcode first.");
				return;
				}
				else if (!v_postcode.match(/^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?[0-9][ABD-HJLN-UW-Z]{2}$|^GIR0AA$/)){
				alert("Invalid UK postcode format!");
				return;
				} 
		        
				 var winArg = "status=1,dependent=1,scrollbars=0,toolbar=0,resizable=0";
				   
				   //set by obj position
				   var popupHeight= 500;
				   var popupWidth = 400;
				   var popupTop  = getTop(obj)- getScrollY();
				   var popupLeft = getScrollX()>0?getLeft(obj)-getScrollX()-popupWidth:getLeft(obj)-popupWidth/3;
				
				   winArg += ",width="+popupWidth+",height="+popupHeight+",top="+popupTop+",left="+popupLeft;
				   oPopWin = window.open("gw5.com.address_finder?p_address="+v_postcode+"&p_target="+obj.getAttribute("target")+"&p_region_tar="+obj.getAttribute("region_tar"),"GOWPopup",winArg);
				   window.onfocus= closePopup;
				   oPopWin.focus();
				  
	}	
	
//This function return a selected option value within a dropdown list
function getSelectValue(select){
   var options =  select.getElementsByTagName("option");
   for (var i=0; i< options.length; i++){
    if (options[i].selected){
      return options[i].value;
      } 
   }
}

function toggleSiblingCheckBox(control){
      if(control.checked) $(control).parent().siblings().find(':checkbox').each(function(){
													     this.disabled="disabled";
													   });
	        else $(control).parent().siblings().find(':checkbox').each(function(){
													     this.disabled="";
													   });
}

//initialize some classes
$(function(){
  $('.helpInfo')
  .click(function(){
      getHelpInfo(this);
      return false;});
  if ($('.dateInput').size()>0)
  $('.dateInput').datepicker({yearRange: "-60:+5",dateFormat: "dd/mm/yy",showOn: "both",duration: '', buttonImage: "/gw/assets/images/icons/16x16/actions/calendar.gif",buttonImageOnly: true});    
});

function getHelpInfo(obj){
     var targetArea = obj.id+'Area'; 
     if($('#'+targetArea).size()==0){
     $(obj.parentNode).append('<div id="'+targetArea+'" class="helpInfoArea hidden"></div>');
     $('#'+targetArea).click(function(){$(this).remove();}).load(obj.href).fadeIn('slow');
     }
}

$(function(){
  $('.option_any')
  .click(function(){
	toggleSiblingCheckBox(this);	
      }).each(function(){toggleSiblingCheckBox(this);});
});

$(function(){
  var intraNav = document.getElementById('intraNav'); 
 if (intraNav){ 
  window.location= intraNav.href;
  $(intraNav.href.replace(/^.*[#]/,'#')).css('border','2px solid orange');
 }
});

