var ErrorString = "";
var Error=false;
var ErrorCRLF="\n";
var CLR_timer_id = null;
var CLR_LoopCount = 0;
var Good_Int_Chars = new String("0123456789-");
var Good_Real_Chars = new String("0123456789.,-");

function StripControl(control) {
  var i=control.indexOf(",",0);
  if(i>=0)return control.substring(0,i);
  else return control;
}

function StripTitle(control) {
  var i=control.indexOf(",",0);
  if(i++>0)return control.substring(i,control.length);
  else return control;
}


function Check_Chars(f,source,goodchars) {
  errcnt=0;
  for(i=0;i<source.length;i++)if(goodchars.search(source.charAt(i))<0)errcnt++;
  return(errcnt>0);
}

function Check_Int(f,control,MinVal,MaxVal) {
  var c=StripControl(control); 
  if((f[c].value == "")||(Check_Chars(f,f[c].value,Good_Int_Chars))){
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else {

    LocalError=false;
    if(MinVal!=null)LocalError=LocalError||(parseInt(f[c].value)<MinVal);
    if(MaxVal!=null)LocalError=LocalError||(parseInt(f[c].value)>MaxVal);
    if(LocalError){
      ErrorString+="  "+StripTitle(control)+ErrorCRLF;
      return true;
    }
    else return false;
  }
}

function Check_Real(f,control) {
  var c=StripControl(control); 
  if((f[c].value == "")||(Check_Chars(f,f[c].value,Good_Real_Chars))){
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else return false;
}

function Check_String(f,control) {
//  alert(StripControl(control));
  if(f[StripControl(control)].value == ""){
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else return false;
}

function Check_EMail(f,control) {
  var c=StripControl(control); 
  if((f[c].value == "")||(f[c].value.indexOf('@',0)==-1)||(f[c].value.indexOf('.',0)==-1)){
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else return false;
}

function Check_Datum(f,control) {
  var c=StripControl(control); 
  if((Check_Chars(f,f[c+"_Dag"].value,Good_Int_Chars))||(Check_Chars(f,f[c+"_Maand"].value,Good_Int_Chars))||(Check_Chars(f,f[c+"_Jaar"].value,Good_Int_Chars))||
     (f[c+"_Dag"].value < 1)||(f[c+"_Dag"].value > 31)||(f[c+"_Maand"].value < 1)||(f[c+"_Maand"].value > 12)||(f[c+"_Jaar"].value < 1900)||(f[c+"_Jaar"].value>2100)){
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else return false;
}

function Check_Time(f,control) {
  var c=StripControl(control); 
  
  
  if((Check_Chars(f,f[c+"_Uur"].value,Good_Int_Chars))||
     (Check_Chars(f,f[c+"_Minuut"].value,Good_Int_Chars))||
     (f[c+"_Uur"].value < 0)||
     (f[c+"_Uur"].value > 24)||
     (f[c+"_Minuut"].value < 0)||
     (f[c+"_Minuut"].value > 59)){
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else return false;
}


function Check_Check(f,control) {
  var OneChecked=false;
  var c=StripControl(control); 

  for(i=0;i<f[c].length;i++) 
    OneChecked=f[c][i].checked||OneChecked;
  if(! OneChecked) ErrorString+="  "+StripTitle(control)+ErrorCRLF;
  return !OneChecked;
}

function Check_Options(f,control) {
  var c=StripControl(control); 
  if(f[c].selectedIndex<0) {
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else {
    if(f[c][f[c].selectedIndex].value>0)return false;
    else { 
      ErrorString+="  "+StripTitle(control)+ErrorCRLF;


      return true;
    }
  }
}

function ClearListBox(f,control) {
  var c=StripControl(control); 
  while(f[c].selectedIndex>=0) 
    f[c][f[c].selectedIndex].selected=false;
}

function Check_Int_Bounds(f,control,MinVal,MaxVal) {
  var c=StripControl(control); 
  
  
  if((f[c].value == "")||(Check_Chars(f,f[c].value,Good_Int_Chars))){
    ErrorString+="  "+StripTitle(control)+ErrorCRLF;
    return true;
  }
  else return false;
}

function F_KiesKleur(f,TableName,FieldName){
  var RGB=document[TableName][FieldName].value;
  var URL="kieskleur.asp?RGB="+RGB+"&TableName="+TableName+"&FieldName="+FieldName+"&SS="+QueryDocumentDir()+"style.css";
  F_OpenWindow(URL,130,200,false);
}

function F_KiesPlaatje(f,TableName,FieldName,previewWidth,previewHeight){
  var URL="kiesplaatje.asp?ID="+eval(document[TableName].ID.value)+"&TableName="+TableName+"&FieldName="+FieldName+"&SS="+QueryDocumentDir()+"style.css&W="+eval(previewWidth)+"&H="+eval(previewHeight);
  
 F_OpenWindow(URL,550,450,true);
}

function F_AskDelete(DisplayThis,URL) {
  if(confirm(DisplayThis))document.location=URL;
}

function F_Ask(DisplayThis,URL) {
  if(confirm(DisplayThis))document.location.href=URL;
}

function F_SetColor(name,color) {
  document[name].src="scripts/ColourGif.dll?RGB="+color;
}

function F_OpenWindow(url,awidth,aheight,aresize) {
  var w = (awidth)?awidth:600;
  var h = (aheight)?aheight:400;  
  var r = (aresize)?"yes":"no";
  var S="resizable="+r+",scrollbars=yes,toolbar=no,status=yes,menubar=no,width="+eval(w)+",height="+eval(h);
  S=S.replace(/,/gi,"&");      
  var wWidth=QueryValue(S,"width")
  var wHeight=QueryValue(S,"height")
  var wLeft=(screen.width)?(screen.width-wWidth)/2:0;
  var wTop=(screen.height)?(screen.height-wHeight)/2:0;
  var D=new Date();
  S=QueryReplace(QueryReplace(S,"left",eval(wLeft)),"top",eval(wTop)).replace(/&/gi,",");
  var newWindow=window.open(url, "w"+eval(D.getTime()), S); 
  if(parseInt(navigator.appVersion) >= 4)newWindow.window.focus();
}


function CLR_Submit(f) {
  layerShow(f,false)
}


function RGBToStr(R,G,B) {
  HexKars="0123456789ABCDEF";
  return HexKars.charAt((parseInt(R)>>4)&0xF)+HexKars.charAt(parseInt(R)&0xF)+HexKars.charAt((parseInt(G)>>4)&0xF)+HexKars.charAt(parseInt(G)&0xF)+HexKars.charAt((parseInt(B)>>4)&0xF)+HexKars.charAt(parseInt(B)&0xF);
}


function CLR_DoDown(f,welke,hoeveel) {
  clearTimeout(CLR_timer_id);
  sHelp = "CLR_DoDown('"+f+"','"+welke+"',"+eval(hoeveel)+")";
  if(CLR_LoopCount>10) CLR_timer_id = setTimeout(sHelp, 50);
  else if(CLR_LoopCount>5) CLR_timer_id = setTimeout(sHelp, 100);
  else CLR_timer_id = setTimeout(sHelp, 200);
  CLR_LoopCount++;

  document.forms[0][f+"_"+welke].value=(Math.round(document.forms[0][f+"_"+welke].value)+hoeveel)%256;
  CLR_Change(f);
}

function CLR_DoUp(welke) {
  clearTimeout(CLR_timer_id);
  CLR_LoopCount = 0;
}

function CLR_Update(f) {
  var h=document.forms[0][f+"_HEX"].value.toUpperCase();
  if(h=="NONE"){
    document[f+"_DSP"].src="pics/trans.gif";
    document.forms[0][f.substr(4)].value="-1";
  }
  else {
    document[f+"_DSP"].src="includes/scripts/colourgif.dll?RGB="+h;
    document.forms[0][f.substr(4)].value=eval(CLR_HexToDec(h));
  }  
}

function CLR_Change(f) {
  var u=document[f+"_DSP"].src.split("?");
  document.forms[0][f+"_HEX"].value=RGBToStr(document.forms[0][f+"_rood"].value,document.forms[0][f+"_groen"].value,document.forms[0][f+"_blauw"].value);
  CLR_Update(f);
}

function CLR_HEXChange(f) {
  var h=document.forms[0][f+"_HEX"].value.toUpperCase();
  if(h=="NONE"){
    document.forms[0][f+"_rood"].value="255";
    document.forms[0][f+"_groen"].value="255";
    document.forms[0][f+"_blauw"].value="255";  
  }
  else{
    document.forms[0][f+"_rood"].value=CLR_HexToDec(h.substr(0,2));
    document.forms[0][f+"_groen"].value=CLR_HexToDec(h.substr(2,2));
    document.forms[0][f+"_blauw"].value=CLR_HexToDec(h.substr(4,2));  
  }
  CLR_Update(f);
}

function CLR_HexToDec(v){
  var r=0;  
  for(i=0;i<v.length;i++){
    r*=16;
    var ch=v.charCodeAt(i);
    if(ch<65)r+=(ch-48);
    else r+=(ch-55);
  }
  return r;
}


function CLR_Preset(f,v){
  document.forms[0][f+"_HEX"].value=v.toUpperCase();
  CLR_HEXChange(f);
}

