// 2004-06-05 joel
// Form verification routines

function ct_verify_input(oinput,cond,msg,p1,p2,p3) {
  var result=true;
  var isArr=(typeof oinput=='object' && !oinput.tagName);
  if (!isArr && oinput.tagName!='INPUT' && oinput.tagName!='SELECT' && oinput.tagName!='TEXTAREA') 
    alert('ct_verify_input js error: not an input entity');

  // default checking
  if (!cond) cond='fill';

  if (!isArr) {
	// get the display name for field
	fdn=(oinput.dname || oinput.name);
  }

  if (cond=='fill') {
    if (oinput.value=='') {
	  if (!msg) msg='Please type a value for: ['+fdn+']';
	  result=false;
	}
  } 
  else if (cond=='radios') {
	cnt=oinput.length;
	result=0;
	if (!p1) p1=1;
	for(var i=0; i< cnt; i++) {
	  if (oinput[i].checked) result++;
	}
    if (result<p1) {
	  if (!msg) msg='Please select at least '+p1+' item(s).';
	  result=false;
	}
  } 
  else if (cond=='check') {
    if (!oinput.checked) {
	  if (!msg) msg='Please check this: ['+fdn+']';
	  result=false;
	}
  } 
  else if (cond=='date') {
	  if  (!ct_verify_date(oinput.value)) { 
	  if (!msg) msg='Please enter a valid date: ['+fdn+']';
	  result=false;
	  }
  } 
  else if (cond=='username') {
	if (!p1) p1=6;
    if (oinput.value=='' || oinput.value.length<p1) {
	  if (!msg) msg='Please enter a valid name for: ['+fdn+']';
	  msg+='\n(A valid name consists of at least '+p1+' letters and numbers)';
	  result=false;
	}
    if (oinput.value.search(/^[A-Z]/i)<0 && oinput.value.charCodeAt(0)<127) {
	  if (!msg) msg='Please start the ['+fdn+'] field with a letter (A to Z).';
	  result=false;
	}
    if (oinput.value.search(/^[A-Z0-9_\x80-\xFF]*$/i)<0) {
	  if (!msg) msg='Please type only letters or numbers for: ['+fdn+']';
	  result=false;
	}
  } 
  else if (cond=='nick') {
	if (!p1) p1=6;
    if (oinput.value=='' || oinput.value.length<p1) {
	  if (!msg) msg='Please enter a valid name for: ['+fdn+']';
	  msg+='\n(A valid name consists of at least '+p1+' characters.)';
	  result=false;
	}
    if (oinput.value.charAt(0)<65) {
	  if (!msg) msg='Please start the ['+fdn+'] field with a letter (A to Z).';
	  result=false;
	}

	for (i=0; i<oinput.value.length; i++) {
	  if (oinput.value.charCodeAt(i)<127 && oinput.value.charAt(i).search(/[A-Z0-9_\-]/i)<0) {
		if (!msg) msg='Please type only letters or numbers for: ['+fdn+"]\n( '"+oinput.value.charAt(i)+"' cannot be used)";
		result=false;
	  }
	}
  } 
  else if (cond=='select') {
    if (oinput.value=='') {
	  if (!msg) msg='Please select a value for: ['+fdn+']';
	  result=false;
	}
  } 
  else if (cond=='number') {
    // 2004-08-24 joel 
    if (oinput.value=='' || isNaN(oinput.value)) {
	  if (!msg) msg='Please enter a NUMBER for: ['+fdn+']';
	  result=false;
	}
	if (oinput.value<p1 || oinput.value>p2) {
	  msg='Please enter a number between '+p1+' and '+p2+' for: ['+fdn+']';
	  result=false;
	}
  }
  else if (cond=='phone') {
    // 2004-09-24 joel 
	if (oinput.value=='' || isNaN(oinput.value) || oinput.value.length<8) {
	  msg='Please enter a PHONE NUMBER for: ['+fdn+']';
	  msg+='\n(At least 8 digits, without space and hyphen.)';
	  result=false;
	}
  }
  else if (cond=='email') {
  	/* 2006-09-25 gf: get from the web */
	  //var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		// 2008-01-10 gf: update filter
		var filter = /^[\ A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,8}$/i
  	if (filter.test(oinput.value)) {}
  	else{
		if (!msg) msg='Please type a valid email address for: ['+fdn+']';
		result = false
	}
	/*
	p=oinput.value.indexOf('@');
	if (p<1 || p==(oinput.value.length-1)) {
	  if (!msg) msg='Please type a valid email address for: ['+fdn+']';
	  result=false;
	}
	*/
  }
  else if (cond=='confirm_pw') {
    if (oinput.value!=p1.value) {
	  if (!msg) msg='The ['+fdn+'] field is different from the password field above.';
	  result=false;
	}
  }
  else if (cond=='disallow_ext' || cond=='limit_ext') { 
	  // ec: restricted ext
		var earr=p1.split(",");
		if (oinput && earr) {
			var f=oinput.value;
			for(var i=0; i<earr.length;i++) {
				var l=earr[i].length;
				var s=f.substr(f.length-l,l).toLowerCase();
				if (earr[i]==s) {
					result=false;
				}
			}
			if (!result) msg = f+" is of invalid filetype.";
		}
  }
  else if (cond=='allow_ext') { 
	  // ec: possible ext

		var earr=p1.split(",");
		if (oinput && earr) {
			var f=oinput.value;
			for(var i=0; i<earr.length;i++) {
				var l=earr[i].length;
				var s=f.substr(f.length-l,l).toLowerCase();
				if (earr[i]==s) {
					return 1;
				}
		 }
		}
		result=false;
		msg = f+" is of invalid filetype.";
	}
  else {
    result=false;
		msg='Unknown field checking type: '+cond;
  }

  if (!result) {
	if (!isArr) {
      try {
	if (oinput.style.display!='none') oinput.focus();
      } catch (e) {};
	}
	else {
      try {
				oinput[0].focus();
      } catch (e) {};
	}

    alert(msg);
  }
  return result;
}


function ct_dirty_input(oinput) {
  document.body.ct_dirty_form=true;
  oinput.style.backgroundColor='#FFD7D7';
  barr=document.getElementsByTagName('INPUT');
  for (i=0; barr!=null && i<barr.length; i++) {
    if (barr[i].type=='submit') barr[i].disabled=false;
  }
}

function ct_check_dirty_form() {
  if (document.body.ct_dirty_form)
    event.returnValue='Changes have not been submitted yet.';
}

function ct_fix_url(which) {
  if (!which || which.search(/http:\/\//i)==0 || which.search(/\//)==0) return which;
  return 'http://'+which;
}

function ct_verify_date_order(odate1,odate2) {
  dn1=odate1.name;
  dn2=odate2.name;
  if (odate1.value>odate2.value) {
    alert("Please enter a "+dn2+" date which is later than the "+dn1+" date.");
    odate2.focus();
    return false;
  }
  return true;
}

function ct_verify_picslot(obj,ext,msg) {
 // ec: ensure picture
 var f=obj.value;
 if (f=="") return 1;
 if (typeof ext=="undefined") ext = ".gif,.jpg,.jpeg,.png,jpe,bmp";
 if (typeof msg =="undefined") msg="Please upload only picture file (GIF, JPG, BMP, PNG).";
 var earr=ext.split(",");
 if (!earr) return;
 for(var i=0; i<earr.length;i++) {
   var l=earr[i].length;
   var s=f.substr(f.length-l,l).toLowerCase();
   if (earr[i]==s) {
      return 1;
    }
 }
 alert(msg);
 return 0;
}


function ct_verify_date(d) {
  // ec: d is a str
  var reg = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
  if ((d.match(reg)) && (d!='')) return true;
	return false;
}

