function fetch_object(idname) {
	if (document.getElementById){ return document.getElementById(idname); }
	else if (document.all){	return document.all[idname]; }
	else if (document.layers){ return document.layers[idname]; }
	else { return null; }
}
function submitform(theForm,btn){
	if (validateFormOnSubmit(theForm)){
		theForm[btn].value = 1 ;
		theForm.submit();
		return true;
	}
	else return false;
}
function validateSendOnSubmit() {
var theForm = document.loadattachform;
var reason = "";
var mfield = new Object();
    mfield["fromname"] = "text";
    mfield["edu"] = "select";
    mfield["day"] = "text";
    mfield["mon"] = "select";
    mfield["yea"] = "select";
    mfield["phone"] = "text";
    err1 = '';
    for(i in mfield) {    
      theElem = theForm[i];
      err = "";
      if (mfield[i]=='select') {
        err = validateNoSelect(theElem);
      } else {
        err = validateEmpty(theElem);
      }
      if (err1=='' && err!='') err1 = i;
      reason +=  err;
    }
  if (reason != "") { window.location.hash = '#input'+err1;  return false;  }
  else { return true; }
}
function clearform() {
	document.loadattachform.fromname.value = '';
	document.loadattachform.fromemail.value = '';
	document.loadattachform.edu.selectedIndex = 0;
	document.loadattachform.day.value = '';
	document.loadattachform.mon.selectedIndex = 1;
	document.loadattachform.yea.selectedIndex = 1979;
	document.loadattachform.phone.value = '';
	document.loadattachform.mmessage.value = '';
	document.loadattachform.clearblock.value=1;
	document.loadattachform.submit();
	return true;
}
function validateFormOnSubmit(theForm) {   
var reason = "";
var mfield = new Object();
    mfield["name"] = "text";
    mfield["razdel"] = "select";
    mfield["rabotd"] = "text";
    mfield["city"] = "select";
    mfield["obyaz"] = "text";
    mfield["contlico"] = "text";
    mfield["contlicodolzhn"] = "text";
    mfield["phone"] = "text";
    err1 = '';
    for(i in mfield) {    
      theElem = theForm[i];
      err = "";
      if (mfield[i]=='select') {
        err = validateNoSelect(theElem);
      } else {
        err = validateEmpty(theElem);
      }
      if (err1=='' && err!='') err1 = i;
      reason +=  err;
    }
  if (reason != "") { window.location.hash = '#input'+err1;  return false;  }
  else { return true; }
}

function validateEmpty(fld) {
    var error = "";
    if (fld.value.length == 0) {
        fld.style.background = '#FFBFBF'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateNoSelect(fld) {
    var error = "";

    if (fld.selectedIndex == -1 || fld.options[fld.selectedIndex].value == 0) {
        
        fld.style.background = '#FFBFBF'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#FFBFBF';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#FFBFBF';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#FFBFBF';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#FFBFBF';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFBFBF';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFBFBF';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#FFBFBF';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#FFBFBF';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#FFBFBF';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = '#FFBFBF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FFBFBF'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#FFBFBF'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FFBFBF'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function load_city_by_region(regObj,cityObj) {
        JsHttpRequest.query(
            '/Include/ajax/loadcitybyregion.php', 
            {
                'reg': fetch_object(regObj).value
            },
            function(result, errors) {
                objSel = document.getElementById(cityObj);
                objSel.options.length = 0;
                for(i=0;i<result.length;i++){
                    objSel.options[objSel.options.length] = new Option(result[i][0], result[i][1]);
                }
            },
            false
        );
}
function load_resume_count_by_region(regObj,cntObj) {
	JsHttpRequest.query(
            '/Include/ajax/loadcntrbyregion.php', 
            {
                'reg': fetch_object(regObj).value
            },
            function(result, errors) {
                objSel = fetch_object(cntObj)
		if (result != 0 ) {
			objSel.style.color="#628e04";
			if (result>1000) { objSel.innerHTML = '~ ' + Math.ceil(result/1000) + ' òûñ. ðåçþìå'; }
			else { objSel.innerHTML = '~ '+(result)+' ðåçþìå'; }
		}
		else {
			objSel.style.color="#ff0000";
			objSel.innerHTML = 'íåò ðåçþìå';
		}
            },
            false
        );
}