/*Validate South African ID Number*/
function validateID(elementID)
{   
    var SAIDRegEx = /^(((\d{2}((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[13456789]|1[012])(0[1-9]|[12]\d|30)|02(0[1-9]|1\d|2[0-8])))|([02468][048]|[13579][26])0229))(( |-)(\d{4})( |-)(\d{3})|(\d{7}))/;
    if (SAIDRegEx.test(elementID)) 
    {
        return true; 
    }
    return false;
}
function imposeMaxLength(Event, Object, MaxLen)
{
        return (Object.value.length <= MaxLen)||(Event.keyCode == 8 ||Event.keyCode==46||(Event.keyCode>=35&&Event.keyCode<=40))
}
function StripSpacesFromEnds(s)

{

	while((s.indexOf(' ',0) == 0) && (s.length > 1))

	{

		s = s.substring(1,s.length);

	}

	while((s.lastIndexOf(' ') == (s.length - 1) && (s.length > 1)))

	{

		s = s.substring(0,(s.length - 1));

	}

	if((s.indexOf(' ',0) == 0) && (s.length == 1)) s = '';

	return s;

}

function IsEmpty(aTextField) {
   textValue = stripSpaces(aTextField)
   if ((textValue.length==0) ||
   (textValue==null)) {
      return true;
   }
   else { return false; }
}

function IsEditing() {
  for (c=0;c<=document.forms.length;c++)
  {
    for (d=0;d<=(document.forms[c].elements.length)-1;d++)
    { 
      if (document.forms[c].elements[d].type != 'checkbox' || document.forms[c].elements[d].value != null)
      {
      alert("form "+c+" element "+d+" = "+document.forms[c].elements[d].type);
      }
    }
  }
}
function stripSpaces(aTextField) {
    var x = document.getElementById(aTextField).value;
    document.getElementById(aTextField).value = (x.replace(/^\s+/,'')).replace(/\s+$/,'');
    return document.getElementById(aTextField).value;
}


function IsEmptyDrop(aTextField) {
   if ((document.getElementById(aTextField).innerHtml.length==0) ||
   (document.getElementById(aTextField).innerHtml==null)) {
      return true;
   }
   else { return false; }
}

function IsItPresent(s,explanation)

{

	s = StripSpacesFromEnds(s);

	if(s.length) return s;

	return '';

}

//Function to limit text input
 function imposeMaxLength(Event, Object, MaxLen)
{
        return (Object.value.length <= MaxLen)||(Event.keyCode == 8 ||Event.keyCode==46||(Event.keyCode>=35&&Event.keyCode<=40))
}


function maxLengthPaste(field,maxChars)
{
   event.returnValue=false;
   if((field.value.length +  window.clipboardData.getData("Text").length) > maxChars) {
     return false;
   }
   event.returnValue=true;
}



// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
    var bracket=3
    strPhone=trim(strPhone)
    if(strPhone.indexOf("+")>1) return false
    if(strPhone.indexOf("-")!=-1)bracket=bracket+1
    if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
    var brchr=strPhone.indexOf("(")
    if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
    if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
    s=stripCharsInBag(strPhone,validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function CheckPhone(dom){
	var Phone=document.getElementById(dom);
	
	if ((Phone.value==null)||(Phone.value=="")){
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		Phone.value=""
		Phone.style.background="red";
        Phone.focus(); 
		return false
	}
	Phone.style.background="white";
	return true
	
 }

function CheckDates(startDom, endDom){
	var startDate=document.getElementById(startDom);
	var endDate = document.getElementById(endDom);
	
	if (startDate.value > endDate.value){
		alert("Start Date is larger than End Date");
		startDate.value="";
		startDate.style.background="red";
		endDate.style.background="red";
    startDate.focus(); 
		return false;
	} 
	return true;
	
 }
function CheckTimes(startT, endT){
	var startTime=document.getElementById(startT);
	var endTime = document.getElementById(endT);
	
	if (startTime.value >= endTime.value){
		alert("Start Time cannot be larger or the same as End Time");
		startTime.style.background="red";
		endTime.style.background="red";
        startTime.focus(); 
		return false;
	}
	startTime.style.background="white";
	endTime.style.background="white";
	return true;
	
 }
function CheckName(dom)

{
  var s_name = document.getElementById(dom).value;
	s_name = IsItPresent(s_name,'your name');

	if(! s_name) return false;

	var i = s_name.indexOf('  ',0);

	while(i > -1)

	{

		s_name = s_name.substring(0,(i + 1)) + 

			s_name.substring((i + 2),s_name.length);

		i = s_name.indexOf('  ',0);

	}

	s_name = s_name.toLowerCase();

	var s = new String(s_name.substring(0,1));

	s = s.toUpperCase();

	s_name = s + s_name.substring(1,s_name.length);

	i = s_name.indexOf(' ',0);

	if(i == (s_name.length - 1)) i = -1;

	var ts = new String("");

	var j = 0;

	while(i > -1)

	{

		i++;

		j = i + 1;

		s = s_name.substring(i,j);

		s = s.toUpperCase();

		ts = '';

		if(i > 0) ts = s_name.substring(0,i);

		s_name = ts + s + s_name.substring(j,s_name.length);

		i = s_name.indexOf(' ',j);

		if(i == (s_name.length - 1)) i = -1;

	}

	document.getElementById('name').value = s_name;

	return true;

}
function isAlpha(val)
{
// True if val is a single alphabetic character.
var re = /^([a-zA-Z])$/;
return (re.test(val));
}


function CheckEmail(dom)

{

  var emailDom = document.getElementById(dom);
  var s_email = emailDom.value;

	s_email = IsItPresent(s_email,'your email address');

	if(! s_email) return false;

	var i = s_email.indexOf(' ',0);

	while(i > -1)

	{

		s_email = s_email.substring(0,i) + 

			s_email.substring((i + 1),s_email.length);

		i = s_email.indexOf(' ',0);

	}

	document.getElementById(dom).value = s_email;

	if((s_email.length < 6) ||

	   (s_email.indexOf('@',0) < 1) ||

	   (s_email.lastIndexOf('@') != s_email.indexOf('@',0)) ||

	   (s_email.lastIndexOf('@') > (s_email.length - 5)) ||

	   (s_email.lastIndexOf('.') > (s_email.length - 3)) ||

	   (s_email.lastIndexOf('.') < (s_email.length - 4)) ||

	   (s_email.indexOf('..',0) > -1) ||

	   (s_email.indexOf('@.',0) > -1) ||

	   (s_email.indexOf('.@',0) > -1) ||

	   (s_email.indexOf(',',0) > -1))

	{
		emailDom.style.background="red";
		emailDom.focus();
		return false;

	}
    emailDom.style.background="white";
	return true;

}


function numbersonly(e)
{
   var unicode=e.charCode? e.charCode : e.keyCode
   if (unicode!=8)
   {
      if (unicode!=9)
      { //if the key isn't the backspace key (which we should allow)
         if (unicode<48||unicode>57)
         return false //disable key press
      }
   }
}
function lettersonly(e)
{
   var unicode=e.charCode? e.charCode : e.keyCode
   if (unicode!=8)
   {
      if (unicode!=9)
      { //if the key isn't the backspace key (which we should allow)
           //alert(unicode) ;

         if (unicode>64&&unicode<123 || unicode==45 || unicode==32)
         return true; 
         else
         return false;  //disable key press
      }
   }
}
// allow only pos amounts
function amountsonly(e)
{

   var unicode=e.charCode? e.charCode : e.keyCode
   if (unicode!=8)
   {
      if (unicode!=9)
      { //if the key isn't the backspace key (which we should allow)
         if (unicode<46||unicode>57) {
            return false //disable key press
        } else {
            if (unicode==47)
            return false //disable key press
        }
      }
   }
}
function checktime(thetime) {
   var a,b,c,f,err=0;
   a=thetime.value;
   if (a.length != 5) err=1;
   b = a.substring(0, 2);
   c = a.substring(2, 3);
   f = a.substring(3, 5);
   if (/\D/g.test(b)) err=1; //not a number
   if (/\D/g.test(f)) err=1;
   if (b<0 || b>23) err=1;
   if (f<0 || f>59) err=1;
   if (c != ':') err=1;
   if (err==1) {
      alert ('That is not a valid time.\nPlease re-enter in format hh:mm ');
      thetime.value = "";
      thetime.style.background="red";
      thetime.focus();
      return false;
   } else
   {
      thetime.style.background="white";
      return true;
   }
}
function Check_RSA_ID(obj_value)
{
	strID = obj_value;
	runningTotal = 0;
	if(strID.length == 13)
	{
		for(i = 0; i < 13; i += 2) runningTotal += parseInt(strID.substr(i, 1));
		for(i = 1; i < 13; i += 2) if(strID.substr(i,1) < 5) runningTotal += parseInt((2*strID.substr(i,1))); 
		else runningTotal += parseInt((2*strID.substr(i,1) + 1));
		if(runningTotal % 10 == 0) return true;
		else alert("ID Number Invalid. Please enter a valid RSA ID Number");
		return false;
	}
	else alert("ID Number Invalid.");
	return false;
}

