/*
'------------------------------------------------+
' Generic JavaScript Functions Library           |
' Nick Sumner                                    |
' dev@nicksumner.com                             |
' 22.09.2003                                     |
'------------------------------------------------+

*/

// -----------------------------------------------

function SetCustomResponses (){

// add your custom responses for each form field name 
// currently this is the only way to validate radio button groups

  Validate ('email','Please enter your email address');
  Validate ('tac','Please accept the terms and conditions ');
  Validate ('option','Please select a payment option ');
  
} // end custom response definitions

// -----------------------------------------------

function FormSubmit(obj){
if (obj) {

  if (navigator.appName.indexOf('Microsoft')==-1) {
    //alert('Frame name: ' + window.frames.name);
    //alert('Object name: ' + obj);
    //alert('Eval: ' + 'window.frames.document.'+obj+'.submit()');
    eval('window.frames.document.'+obj+'.submit()');
  }
  else {
    eval('window.document.'+obj+'.submit()');
  }

} // end test for form
  
}

// -----------------------------------------------

/*****************************************************
  generic form validation js script
  include this file as a js src file

  attach the following code to the submit button html:
  onclick="Chk(this.form)"

  to make a form field compulsory, either
   a) set the id of the field to one of the codes given below, or
   b) add the name of the field to the list of custom responses

 id codes:  
   id="V"  : any value required (string)
   id="X"  : value required (string) 2-20 chars not all the same
   id="A"  : alphanumeric value required
   id="I"  : integer value required 
   id="E"  : email value required 
   id="W"  : web address value required 
   id="P"  : postcode value required
   id="T"  : tel value required (digits +spaces)
   id="S"  : tel value required (digits +spaces)
   id="N"  : Integer value required (no floats) checks for .
 
*/

  // global variables 

  ElementArray = new Array();
  PromptArray = new Array();
  errmsg= 'OK';
  BadCharList= '\"\\/><}{][#=+)(*&^%$£!,:;|¬ ';
  BadCharList2= '@-_~';
  SetCustomResponses ();
 
  // function definitions 

// -----------------------------------------------

function Validate (a,b){
  var s= ElementArray.length;
  ElementArray[s] = a;
  PromptArray[s] = b;
}

// -----------------------------------------------

function ValidateRadios (frm){

  var m= ElementArray.length;
  var n= frm.elements.length;
  var tmp='';

  // check if in custom reponse list 
  for (var k=0;k<m;k++){ 
  tmp='';
  // loop thru form elements
  for (var i=n-1;i>-1;i--){

  e= frm.elements[i];
  u= e.name;
  v= e.value;
  x= e.type; 
  
  if ((x.indexOf('radio')>-1)||(x.indexOf('checkbox')>-1)) {

        if (u==ElementArray[k]) {

    if ((!e.checked) && (tmp==''))
    tmp=PromptArray[k];  
    else if (e.checked)
    tmp='OK';  
  }


  } // end if

  } // end for

  if ((tmp!='')&&(tmp!='OK'))
  errmsg= tmp;

  } // end for
  return false;
}

// -----------------------------------------------

function Check(obj) { 
  errmsg= '';   
  var thisErrmsg= '';

  var m= ElementArray.length;
  var n= obj.elements.length;

  ValidateRadios(obj);

  // loop thru form elements
  for (var i=n-1;i>-1;i--){
  thisErrmsg= '';
  e= obj.elements[i];
  u= e.name;
  v= e.value;
  x= e.type; 

  if (x.indexOf('radio')==-1) {

  //var s=eval('obj.'+u+'.id');
  //alert(s);

  var w=e.id;
  switch (w){

    case 'V': 
    case 'v': if (v=='')
        thisErrmsg= 'This field requires a value: \'' + u + '\'';
              break;  

    case 'X':
    case 'x': if (!ChkBocsSurname(v))
              thisErrmsg= 'This field requires a value (2-20 chars): \'' + u + '\'';
              break;  

    case 'I':
    case 'i': if ((isNaN(v))||(v==''))
              thisErrmsg= 'This field requires a integer value: \'' + u + '\'';
              break;    

    case 'A':
    case 'a': if (ChkAlphaNumeric (v)>-1)
              thisErrmsg= 'This field requires a alphanumeric value: \'' + u + '\'';
              break;

    case 'D':
    case 'd': if (!isDate(v))
              thisErrmsg= 'Please enter a valid date. ';
              break; 
       
    case 'E':
    case 'e': if (!isEmail(v))
              thisErrmsg= 'Please enter a valid email address. ';
              break;          

    case 'W':
    case 'w': if (!isWebAddress(v))
              thisErrmsg= 'Please enter a valid web address. ';
              break;   

    case 'P':
    case 'p': if (!isPostcode(v))
              thisErrmsg= 'Please enter a valid postcode. ';
              break; 

    case 'T':
    case 't': if (isNaN(TrimSpace(v)))
              thisErrmsg= 'Please enter digits and spaces only for this field: \'' + u + '\'';
              break;  

    case 'S':
    case 's': if (isNaN(TrimSpace(v))||(v==''))
              thisErrmsg= 'Please enter digits and spaces only for this field: \'' + u + '\'';
              break;  
    case 'N':
    case 'n': if ((isNaN(v))||(v=='')||(v.indexOf('.')>-1))
              thisErrmsg= 'This field requires a integer value: \'' + u + '\'';
              break;  			  

    }  // end switch
  
  if (thisErrmsg!=''){
     errmsg=thisErrmsg;
  }

  // check if in custom reponse list 
  if ((v=='') || (thisErrmsg!='')) {
      for (var k=0;k<m;k++){ 
        if (u==ElementArray[k]) {
          errmsg= PromptArray[k];
  }
      }
    }

  } // end if (radio)

  } // end elements loop
  if ((errmsg!='')) alert(errmsg);
  return (errmsg=='');
} 

// -----------------------------------------------

function isDate(date) {
    // NaN is never equal to itself.
    if (Date.parse(date) != Date.parse(date))
        return false
    else
        return true;
}

// -----------------------------------------------

function ChkAlphaNumeric (str) {

// does not allow no entry

  if (str=='')
    return 1;

  else{
  var m= -1;
  var n= -1;

  var x= BadCharList;
  var y= x.length;
  for (z=0;z<y;z++){
    n=str.indexOf(x.charAt(z));
    if (n != -1)
      return n;
  }

  x= BadCharList2;
  y= x.length;
  for (z=0;z<y;z++){
    n=str.indexOf(x.charAt(z));
    if (n != -1)
      return n;
  }

  return m;
  }
}

// -----------------------------------------------

function TrimSpace (str) {

  arrayString = str.split(' '); 
  var tmp='';

  if (str=='')
    return '';

  else if (arrayString[0] == null){
  // old browser no split
    return '';
  }
  else {
    var a= arrayString.length-1;
    for(var b=0;b<=a;b++)
       tmp=tmp+arrayString[b];
  }
  return tmp;
}

// -----------------------------------------------

function isPostcode(str) {
// does not allow no entry

  if (str.indexOf(' ') == -1)
    return false;  
  else if (str.length<6)
    return false;
  else if (ChkAlphaNumeric (str)>-1)
      return false;

  arrayString = str.split(' '); 

  if (arrayString.length!=2)
      return false;
  else if (arrayString[0] == '')
      return false;
  else if (arrayString[1] == '')
      return false;
  else if (ChkAlphaNumeric (arrayString[0])>-1)
      return false;
  return true;
}

// -----------------------------------------------

function isEmail(argvalue) {
// does not allow no entry

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;
  else if (argvalue.indexOf(".") == -1)
    return false;
    
  var x= BadCharList;
  var y= x.length;
  for (z=0;z<y;z++)
    if (argvalue.indexOf(x.charAt(z)) != -1)
      return false;

  arrayString = argvalue.split("@"); 

  if (arrayString[0] == null){
  // old browser no split
  }
  else {

    if (arrayString[1].indexOf(".") == -1)
      return false;
    else if (arrayString[1].indexOf(".") == 0)
      return false;
    else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
      return false;
    }
  }

  return true;
}

// -----------------------------------------------

function isWebAddress(argvalue) {
// allows no entry
  if (argvalue=='')
    return true;

// accepts with/without 'http://'
  argvalue.toUpperCase();
  if (argvalue.indexOf("http://") != -1){
    if (argvalue.indexOf("http://") != 0)
      return false;
      argvalue=argvalue.substring(7,argvalue.length);
  }

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length-1)=='.')
    return false;

  var x= BadCharList;
  var y= x.length;
  for (z=0;z<y;z++)
    if (argvalue.indexOf(x.charAt(z)) != -1)
      return false;

  arrayString = argvalue.split("."); 

  if (arrayString[0] == null){
  // old browser no split
  }
  else {

      if (arrayString.length<3)
        return false;
      else if (arrayString.length>5)
        return false;
  }

  return true;
}

// -----------------------------------------------

function openWin(a, w, h, l, t, hnd){
  var s;

  s = "width=" + w;
  s = s + ", height=" + h;
  s = s + ", left=" + l;
  s = s + ", top=" + t;
  s = s + ", status=no, toolbar=no, menubar=no, resizable=yes, scrollbars=yes";

  mywin = open(a, hnd, s);
  if (parseInt(navigator.appVersion) >= 4) { mywin.window.focus(); }
}

// -----------------------------------------------

function Hyperlink(obj){
  str=obj.value;
  ret='';
  WordArray = new Array();
//  str=' '+str+' ';
  WordArray = str.split(' ');
  var plen= WordArray.length;

  for (i=0;i<plen;i++){
    var v=WordArray[i];

    if (v.indexOf('@')>-1){
      ret =ret+' <a href="mailto:' + v + '">' + v + '</a>'
    }
    else if (v.indexOf('http://')==0){
      ret =ret+' <a href="' + v + '">' + v + '</a>'
    }
    else if (v.indexOf('www.')==0){
      ret =ret+' <a href="http://' + v + '">' + v + '</a>'
    }
    else{
      ret =ret+' '+ v ;
    }
  }
  ret=ret.substring(1,ret.length);
  obj.value=ret;
  return ret;

}

// -----------------------------------------------

// fns to count chars
maxKeys = 7900;
keysSoFar = 0;

function keyup(what) {

    body_text_sofar = document.form1.body_text.value.length;
    keysSoFar= what.value.length;
    if (keysSoFar > maxKeys) {
        alert('Maximum characters allowed: ' + maxKeys );
        what.value = what.value.substring(0,maxKeys); // chop the last typed char
	keysSoFar = maxKeys;

    }
    charcount.innerHTML = maxKeys-keysSoFar + ' (' + body_text_sofar + ') total.';

}
