var autoFocus_skipFilled = 1;
var musicPopup;

function findObj(n,d){ 
 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { 
 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} 
 if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; 
 for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document); 
 if(!x && d.getElementById) x=d.getElementById(n); return x; 
}


function checkCodeInput(obj){
 if(obj.value && obj.value.length){
  obj.className = 'shirtcodeInput_active';
 }else{
  obj.className = 'shirtcodeInput_default';
  obj.value = obj.defaultValue;
 }
}

function getRandom(min,max){
 return (Math.round(Math.random()*(max-min)))+min;
}

function processSubmit(frm,noSubmit){
 var btn = document.getElementById('btnSubmit');
 if(typeof(frm)=='string') frm = findObj(frm);
 window.status = 'Een ogenblik geduld a.u.b.';
 if(btn){
  btn.innerHTML = 'Een ogenblik geduld a.u.b.';
  btn.disabled = true;
 }
 if(!noSubmit) frm.submit();
}

function fldSetValue(obj,val){
 if(typeof(obj)=='string') obj = findObj(obj);
 if(obj) obj.value = val;
}

function selectFirstFormField(frm,skipEmpty){
 if(!frm) frm = document.forms[0];
 if(typeof(frm)=='string') frm = findObj(frm);
 if(frm && frm.elements && frm.elements.length >= 1){
  var l = frm.elements.length;
  for(var i=0;i < l;i++){
   var el = frm.elements[i];
   if(typeof(el.type)!='undefined' && el.type == 'text'){
    if(!(el.value.length && skipEmpty)){
     el.focus();
     return true;
    }
   }else if(typeof(el.type)!='undefined' && el.type == 'radio'){
    el.focus();
    return true;
   }else if(typeof(el.type)!='undefined' && el.type == 'checkbox'){
    el.focus();
    return true;
   }else if(typeof(el.tagName)!='undefined' && el.tagName.toLowerCase()=='textarea'){
    if(!(el.value.length && skipEmpty)){
     el.focus();
     return true;
    }
   }
  }
  return false;
 }else{
  return false;
 }
}

function toggleLayerBlock(obj){
 if(typeof(obj)=='string') obj = findObj(obj);
 if(obj.style.display=='none'){
  obj.style.display = 'block';
 }else{
  obj.style.display = 'none';
 }
}

function selCheckSelected(stN,frm){
 var cf = new Array;
 if(frm){
  if(typeof(frm)=='string') frm = findObj(frm);
  var elts = (typeof(frm.elements)!='undefined')?frm.elements:false; 
  var elts_cnt = (typeof(elts.length)!='undefined')?elts.length:0;
  for(var i=0;i<elts_cnt;i++){
   if(((stN&&elts[i].name==stN)||(!stN&&typeof(elts[i])=='object'))&&elts[i].checked){
    var cfl = (typeof(cf.length)!='undefined')?cf.length:0;
    cf[cfl]=elts[i];
   }
  }
 }else{
  var elts = findObj(stN);
  var elts_cnt = (typeof(elts.length)!='undefined')?elts.length:0;
  for(var i=0;i<elts_cnt;i++){
   if(typeof(elts[i])!='undefined'&&typeof(elts[i].checked)!='undefined'&&elts[i].checked){
    var cfl = (typeof(cf.length)!='undefined')?cf.length:0;
    cf[cfl]=elts[i];
   }
  }
 }
 return cf;
}

function selCheckSelectedBegin(stN,frm){
 var cf = new Array;
 if(frm){
  if(typeof(frm)=='string') frm = findObj(frm);
  var elts = (typeof(frm.elements)!='undefined')?frm.elements:false; 
  var elts_cnt = (typeof(elts.length)!='undefined')?elts.length:0;
  for(var i=0;i<elts_cnt;i++){
   if(stN&&elts[i].name.substring(0,stN.length)==stN&&selIsChecked(elts[i])){
    var cfl = (typeof(cf.length)!='undefined')?cf.length:0;
    cf[cfl]=elts[i];
   }
  }
 }
 return cf;
}

function selGetBoxesBegin(stN,frm){
 var cf = new Array;
 if(frm){
  if(typeof(frm)=='string') frm = findObj(frm);
  var elts = (typeof(frm.elements)!='undefined')?frm.elements:false; 
  var elts_cnt = (typeof(elts.length)!='undefined')?elts.length:0;
  for(var i=0;i<elts_cnt;i++){
   if(stN&&elts[i].name.substring(0,stN.length)==stN){
    var cfl = (typeof(cf.length)!='undefined')?cf.length:0;
    cf[cfl]=elts[i];
   }
  }
 }
 return cf;
}

function selIsChecked(obj){
 if(typeof(obj)=='string') obj=findObj(obj);
 if(obj && typeof(obj.checked)!='undefined' && obj.checked){
  return true;
 }
 return false;
}

function selIsDisabled(obj){
 if(typeof(obj)=='string') obj=findObj(obj);
 if(obj && typeof(obj.disabled)!='undefined' && obj.disabled){
  return true;
 }
 return false;
}

function testValidPostcode(pc){
 if(pc.length >= 6){
  for(var i=0;i<4;i++){
   if(!(pc.charAt(i)>=0&&pc.charAt(i)<=9)) return false;
  }
  if(pc.charAt(4)==' '){
   if(!((pc.charAt(5)>='a'&&pc.charAt(5)<='z')||(pc.charAt(5)>='A'&&pc.charAt(5)<='Z'))) return false;
   if(!((pc.charAt(6)>='a'&&pc.charAt(6)<='z')||(pc.charAt(6)>='A'&&pc.charAt(6)<='Z'))) return false;
  }else{
   if(!((pc.charAt(4)>='a'&&pc.charAt(4)<='z')||(pc.charAt(4)>='A'&&pc.charAt(4)<='Z'))) return false;
   if(!((pc.charAt(5)>='a'&&pc.charAt(5)<='z')||(pc.charAt(5)>='A'&&pc.charAt(5)<='Z'))) return false;
  }
  return true;
 }else return false;
}

function showPng(file,w,h){
 var lay = findObj('imgLayer');
 var img = new Image();
 var winwidth=document.all?document.body.clientWidth:window.innerWidth;
 var winheight=document.all?document.body.clientHeight:window.innerHeight;
 img.src = file;
 lay.style.height = h + 'px';
 lay.style.width = w + 'px';
 lay.style.zIndex = 10001;
 var t = ((winheight / 2) - h / 2);
 var l = ((winwidth / 2) - w / 2);
 lay.style.left = (l+5)+'px';
 lay.style.top = t+'px';
 lay.style.position = 'absolute';
 
 if(window.navigator.userAgent.indexOf('Win')!=-1 && window.navigator.userAgent.indexOf('MSIE')!=-1){
  lay.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + img.src + '",sizingMethod="scale")';
 }else{
  lay.innerHTML = '<img src="'+img.src+'" height="'+h+'" width="'+w+'" alt="" />';
 }
 lay.style.display = 'inline';
}

function hidePng(){
 var lay = findObj('imgLayer');
 lay.style.filter = '';
 lay.style.display = 'none';
}

function photoLarge2(prodImg,detailImg,prodCode,prodDescr){
 var lay=findObj('imgLayer'), ml=findObj('popupMeasure'), im1=new Image(), im2=new Image();
 var pt=(ml?xPageY(ml):0),pl=(ml?xPageX(ml):0); im1.src=prodImg; im2.src=detailImg;
 var l_prodImg=findObj('imgLayer_prodImg');
 var l_detailImg=findObj('imgLayer_detailImg');
 var l_prodCode=findObj('imgLayer_prodCode');
 var l_prodDescr=findObj('imgLayer_prodDescr');
 lay.style.zIndex = 1000;
 lay.style.display='block';
 l_prodImg.innerHTML='<img src="'+im1.src+'" alt="" />'; 
 l_detailImg.innerHTML='<img src="'+im2.src+'" alt="" />';
 l_prodCode.innerHTML=prodCode; l_prodDescr.innerHTML=prodDescr;
 lay.style.left=pl+'px'; lay.style.top=pt+'px';
 return false;
}

function photoLarge3(prodID,root){
 var lay=findObj('imgLayer'), ml=findObj('popupMeasure');
 var pt=(ml?xPageY(ml):0),pl=(ml?xPageX(ml):0);
 var l_prodInfo=findObj('imgLayer_frame');
 l_prodInfo.src = root+'index.php?p=26&prodID='+prodID+'&rand='+getRandom(1,99);
 lay.style.zIndex = 1000;
 lay.style.display='block';
 lay.style.left=pl+'px'; lay.style.top=pt+'px';
 return false;
}

function photoLarge(prodID,root){
 var ml=findObj('popupMeasure');
 var pt=(ml?xPageY(ml):0),pl=(ml?xPageX(ml):0);
 pt+=80; pl-= 50;
 var win = window.open(root+'index.php?p=26&prodID='+prodID+'&rand='+getRandom(1,99),'img','height=650,width=400,top='+pt+',left='+pl+',statusbar=no,scrolling=no,directories=no');
 win.focus();
 return false;
}

function photoLargeClose3(root){
 var lay=findObj('imgLayer');
 var l_prodInfo=findObj('imgLayer_frame');
 l_prodInfo.src = root+'blank.html';
 lay.style.display='none';
 return false;
}

function photoLargeClose(root){
 var lay=findObj('imgLayer');
 var l_prodInfo=findObj('imgLayer_frame');
 l_prodInfo.src = root+'blank.html';
 lay.style.display='none';
 return false;
}


function checkFldFocus(){
 var fl = document.forms.length;
 if(!fl) return;
 for(var i=0;i<fl;i++){
  var e = document.forms[i].elements;
  var el = e.length;
  if(!el) return;
  for(var j=0;j<el;j++){
   var cel = e[j];
   if(typeof(cel.autofocus)!='undefined' && cel.autofocus){
    var celt = (typeof(cel.type)!='undefined') ? cel.type.toLowerCase() : '';
    var sf = autoFocus_skipFilled;
    switch(cel.type){
     case 'text':
     case 'radio':
     case 'checkbox':
      if(!cel.disabled){
       if((sf && !cel.value)||(!sf && cel.value)){
        cel.focus();
        return;
       }
      }
      break;
     
     default:
      if(typeof(cel.tagName)!='undefined' && cel.tagName.toLowerCase()=='textarea' && !cel.disabled){
       if((!sf && cel.innerHTML)||(sf && cel.innerHTML)){
        cel.focus();
        return;
       }
      }
      break;
    }
   }
  }
 }
}


function wsSel_setCountryParam(obj,useURL){
	if(typeof(obj)=='string') obj = findObj(obj);
	var lsObj=findObj('lastState');
	if(!obj) return;
	if(typeof(obj.selectedIndex)!='undefined'){
		var selVal = (typeof(obj.options)!='undefined'&&typeof(obj.options[obj.selectedIndex])!='undefined')?obj.options[obj.selectedIndex].value : '';
		var selValName = obj.getAttribute('name');
		var loc = useURL ? useURL : 'index.php?p=22&viewState='+escape(lsObj.value);
		loc+='&'+escape(selValName)+'='+escape(selVal);
		location.replace(loc);
	}
}

function wsSel_setCityParam(obj,countryObj,useURL){
	if(typeof(obj)=='string') obj = findObj(obj);
	var lsObj=findObj('lastState');
	if(!obj) return;
	if(typeof(countryObj)=='string') countryObj = findObj(countryObj);
	if(!countryObj) return;
	if(typeof(obj.selectedIndex)!='undefined'){
		var selVal = (typeof(obj.options)!='undefined'&&typeof(obj.options[obj.selectedIndex])!='undefined')?obj.options[obj.selectedIndex].value : '';
		var cSelVal = (typeof(countryObj.options)!='undefined'&&typeof(countryObj.options[countryObj.selectedIndex])!='undefined')?countryObj.options[countryObj.selectedIndex].value : '';
		var selValName = obj.getAttribute('name');
		var cSelValName = countryObj.getAttribute('name');
		var loc = useURL ? useURL : 'index.php?p=22&viewState='+escape(lsObj.value);
		loc+='&'+escape(cSelValName)+'='+escape(cSelVal);
		loc+='&'+escape(selValName)+'='+escape(selVal);
		location.replace(loc);
	}
}

function wsSel_countrySubmit(obj){
	var f_act=findObj('fld_action');
	var f_vst=findObj('fld_viewState');
	var f_lst=findObj('fld_lastState');
	f_act.value='';
	f_vst.value=f_lst.value;
	f_lst.value='';
	if(!obj) return;
	obj.form.submit();
	return true;
}
function wsSel_citySubmit(obj){
	var f_act=findObj('fld_action');
	var f_vst=findObj('fld_viewState');
	var f_lst=findObj('fld_lastState');
	f_act.value='';
	f_vst.value=f_lst.value;
	f_lst.value='';
	if(!obj) return;
	obj.form.submit();
	return true;
}


function checkSelectedDealer(selObj,showObj,focusObj){
 if(typeof(selObj)=='string') selObj = findObj(selObj);
 if(typeof(showObj)=='string') showObj = findObj(showObj);
 if(typeof(focusObj)=='string') focusObj = findObj(focusObj);
 if(selObj && selObj.options && showObj){
  if(selObj.selectedIndex == selObj.options.length - 1){
   //anders, namelijk
   showObj.style.display = 'inline';
   if(focusObj) focusObj.focus();
  }else{
   showObj.style.display = 'none';
  }
 }
}

function setMusicStatus(status){
 if(musicPopup){
	musicPopup.music(status);
 }else{
  window.status = 'No musicPopup found, status='+status;
 }
}

function popupSWF(){
 musicPopup = window.open('assets/audio/player.html','popup',"height=100,width=100,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no,status=no,statusbar=no,statusbars=no,directories=no");
 musicPopup.focus();
}

function popupPhoto(prodID,root){
 var ml=findObj('popupMeasure');
 var pt=(ml?xPageY(ml):0),pl=(ml?xPageX(ml):0);
 pt+=80; pl-= 50;
 var win = window.open(root+'index.php?p=26&prodID='+prodID+'&rand='+getRandom(1,99),'img','height=550,width=400,top='+pt+',left='+pl+',statusbar=no,scrolling=no,directories=no');
 win.focus();
}

var sh_tableSelect_Data = [];

function sh_tableSelect_load(parentObj){
	if(typeof(parentObj)=='string') parentObj = findObj(parentObj);
	if(!parentObj) window.status='tableSelect: object not found';
	var selID = parentObj.getAttribute('id');
	var selTable = parentObj.firstChild;
	
	var inputObj = findObj('shts_c_'+selID);
	if(!inputObj){
		alert('Unable to find input objects, please place the javascript beneath the inputs');
	}
	if(selIsChecked(inputObj)){
		selTable.className='sh_tableSelect';
		parentObj.setAttribute('shTableSelected',true);
		inputObj.checked = true;
	}else{
		selTable.className='sh_tableNoSelect';
		parentObj.setAttribute('shTableSelected',false);
		inputObj.checked = false;
	}
	
	//xAddEventListener(inputObj,'change',function(){sh_tableSelect(parentObj);});
	//xAddEventListener(parentObj,'mouseover',function(){sh_tableSelect_over(selID);});
	//xAddEventListener(parentObj,'mouseout',function(){sh_tableSelect_out(selID);});
	
	var t=inputObj.getAttribute('type').toLowerCase();
	if(t=='radio'){
		var n=inputObj.getAttribute('name');
		if(typeof(sh_tableSelect_Data[n])=='undefined') sh_tableSelect_Data[n]=[];
		var l=(typeof(sh_tableSelect_Data[n])!='undefined'&&typeof(sh_tableSelect_Data[n].length)!='undefined')?sh_tableSelect_Data[n].length:0;
		sh_tableSelect_Data[n][l]=selID;
	}
}

function sh_tableSelect_over(tID){
	var t=findObj(tID);
	if(!t) return;
	if(t.getAttribute('shTableSelected')){
		return;
	}else{
		var ct=t.firstChild;
		ct.className='sh_tableNoSelectOver';
	}
}

function sh_tableSelect_out(tID){
	var t=findObj(tID);
	if(!t) return;
	if(t.getAttribute('shTableSelected')){
		return;
	}else{
		var ct=t.firstChild;
		ct.className='sh_tableNoSelect';
	}
}

function sh_tableSelect_showActive(obj){
	if(typeof(obj)=='string') obj=findObj(obj);
	obj.setAttribute('shTableSelected',true);
	obj.firstChild.className='sh_tableSelect';
}

function sh_tableSelect(parentObj,clickHandler){
	var selID = parentObj.getAttribute('id');
	var selTable = parentObj.firstChild;
	var inputObj = findObj('shts_c_'+selID);
	if(!inputObj) return;
	if(!clickHandler) clickHandler = function(){ };
	var t=inputObj.getAttribute('type').toLowerCase();
	if(t=='radio'){
		var n=inputObj.getAttribute('name'); var selObj;
		if(!(typeof(sh_tableSelect_Data[n])!='undefined'&&typeof(sh_tableSelect_Data[n].length)!='undefined')){
			sh_tableSelect_Data[n] = [selID];
		}
		for(var i=0;i<sh_tableSelect_Data[n].length;i++){
			var si=sh_tableSelect_Data[n][i]; var so=findObj(si); var sp=findObj('shts_c_'+si);
			if(si==selID){
				sp.checked = true;
				so.setAttribute('shTableSelected',true);
				so.firstChild.className='sh_tableSelect';
				selObj=sp;
			}else{
				sp.checked = false;
				so.setAttribute('shTableSelected',false);
				so.firstChild.className='sh_tableNoSelect';
			}
		}
		clickHandler(selObj);
	}else{
		if(selIsChecked(inputObj)){
			inputObj.checked = false;
			selTable.className='sh_tableNoSelect';
			parentObj.setAttribute('shTableSelected',false);
		}else{
			inputObj.checked = true;
			selTable.className='sh_tableSelect';
			parentObj.setAttribute('shTableSelected',true);
		}
		clickHandler(inputObj);
	}
}

xAddEventListener(window,'load',checkFldFocus);
