//set current date in menu bar
function SetCurDate() {

  var val = new Date(curYear, curMonth-1, curDay);
	var d = val.getDate();
	var m = val.getMonth();
	var yr = val.getFullYear();
	if (document.all) {
		var cd = document.all.currentdate;
	} else if (document.getElementById) {
		var cd = document.getElementById("currentdate");
	}

	if (cd) cd.innerHTML = d + " " + arrMonth[m] + " " + yr

}

//set select box value
function SetSelVal(ctl, newVal) {
	for (var i=0; i<ctl.length; i++) {
		if (ctl[i].value == newVal) {
			ctl[i].selected = true
			ctl.selectedIndex = i
			break
		}
	}
}

//get select box value
function GetSelVal(ctl) {
	var selIdx = ctl.selectedIndex;
	return selIdx == -1 ? "" : ctl[selIdx].value;
}

//date validation
function validDate(d, ctlDay) {
	return d.getDate() == GetSelVal(ctlDay);
}

//trim string
function trim(stringToTrim) {
	
	var i, trimmedString = ""
	//left trim
	for(i=0; i<stringToTrim.length; i++) {
		if (stringToTrim.charAt(i) != " ") break
	}

	trimmedString = stringToTrim.substring(i)

	//right trim
	for(i=trimmedString.length-1; i>=0; i--) {
		if (trimmedString.charAt(i) != " ") break
	}

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

	return trimmedString
}

// Return checked element of form
function IDChecked(el) {
  
  for (i=0; i < el.length; i++) {
    if (el[i].checked == true) return i;
  }
}

// Check for correct (ASCII) characters
function ascii(str) {
  for (i=0; i<str.length; i++) {
    if (str.charCodeAt(i)>127) {
      return false;
    }
  }
  return true;
}

//validate email address
function notEmail(field) {
  
	var email = trim(field.value);

	var at = false;
	var dot = false;

	for (var i=0; i<email.length; i++) {
		if (email.charAt(i) == "@") at = true;
		if (email.charAt(i) == "." && at) dot = true;
		if (IncChar(email.charAt(i))) {
		  alert("Sorry, your e-mail address contains incorrect symbols.");
		  field.focus();
 		  return true;
		}
	}
	if (at && dot && email.length > 6) {
		return false;
	} else {
		alert("The e-mail you entered is not a valid e-mail address.");
		field.focus();
		return true;
	}
}

//incorrect email characters
function IncChar(chr) {
	
	// 45(-),46(.),48-57(0-9),64(@), 65-90(A-Z), 95(_), 97-122(a-z)
	c = chr.charCodeAt(0);
	if (c>45 && c<122) {
		if ((c==45 || c==46 || c==64 || c==95) || (c>=48 && c<=57) || (c>=97 && c<=122)) {
   		return false;
		}
	}
  return true;	
}

// check field not empty
function FNotEmpty(el) {

  val = el.value;
  // remove spaces from begin and end of field value
  val = trim(val);
  el.value = val;
  if (val == "") return false;
  else return true;
  
}

//validate phone & fax numbers
function notPhone(field) {

	var phone = trim(field.value);
    var str = '';

	for (var i = 0; i < phone.length; i++) {
        code = phone.charAt(i).charCodeAt(0);
        //alert(code);
        //48-57(0-9), 40('('), 41(')'), 32(' '), 43('+'), 45('-'), 95(_), 44(',') , 59(;)
        if ( (code<48 || code>57) && code != 40  && code != 41 && code != 32 && code != 43 && code != 45 && code != 95 && code != 44 && code != 59 ) return true;
	}
    return false;
}
