/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


/*
 * RPC
 */
function rpc_loadNetTerm() {
	var rpc = new XHConn();
	
	var p_loadNetTerm = function (oXML) {
		document.getElementById('terms').value=oXML.responseText;
	};
	
	var network = document.getElementById('network').options[document.getElementById('network').selectedIndex].value
	
	rpc.connect("index.php", "GET", "action=e_rpc&rpc=loadNetTerm&nid="+network, p_loadNetTerm);

} 

/**/
function check_regform_step1() {	
	var reLoginname = new RegExp("^([a-z0-9A-Z]+)([a-z0-9A-Z_-]*)([a-z0-9A-Z]+)$");
	
	if (document.forms.regform1.loginname.value.length<3) {
		alert('Uživatelské jméno musí mít alespoň 3 znaky.');
		document.forms.regform1.loginname.focus();
		return false;
	}
	
	if (!document.forms.regform1.loginname.value.match(reLoginname)) {
		alert('Chybný formát uživatelského jména.');
		document.forms.regform1.loginname.focus();
		return false;
	}
	
	if (document.forms.regform1.password.value != document.forms.regform1.password2.value) {
		alert('Zadaná hesla nejsou stejná.');
		document.forms.regform1.password.focus();
		return false;
	}
	
	if (document.forms.regform1.password.value.length<6) {
		alert('Minimální délka hesla je 6 znaků.');
		document.forms.regform1.password.focus();
		return false;
	}
	
	if (document.forms.regform1.network.selectedIndex==0){
		alert('Musí být vybrána síť.');
		document.forms.regform1.network.focus();
		return false;
	}
	
	if (!document.forms.regform1.agreement.checked) {
		alert('Pro registraci do vybrané sítě musíte akceptovat podmínky.');
		document.forms.regform1.agreement.focus();
		return false;
	}	
	return true;
}

function check_regform_step2() {
	var reZip = new RegExp("^\\d{3}[ ]?\\d{2}$");
	if (document.forms.regform2.firstname.value.length<1) {
		alert('Není zadáno křestní jméno.');
		document.forms.regform2.firstname.focus();
		return false;
	}
	
	if (document.forms.regform2.surname.value.length<1) {
		alert('Není zadáno příjmení.');
		document.forms.regform2.surname.focus();
		return false;
	}
	
	if (document.forms.regform2.street.value.length<1) {
		alert('Chybně zadaná ulice / Č.P. [min. 1 znak].');
		document.forms.regform2.street.focus();
		return false;
	}
	if (document.forms.regform2.city.value.length<2) {
		alert('Chybně zadáno město / obec [min. 2 znaky].');
		document.forms.regform2.city.focus();
		return false;
	}
	if (!document.forms.regform2.zipcode.value.match(reZip)) {
		alert('Chybně zadano PSČ.');
		document.forms.regform2.zipcode.focus();
		return false;
	}
	
	if ((document.forms.regform2.phone.value.length<1) && (document.forms.regform2.mobile.value.length<1)) {
		alert('Musí být zadám alespoň jeden telefonický kontakt.');
		document.forms.regform2.phone.focus();
		return false;
	}
	
	if (document.forms.regform2.company_name){
		if (document.forms.regform2.company_name.value.length<2) {
			alert('Chybně zadáno obchodní jméno [min. 2 znaky].');
			document.forms.regform2.company_name.focus();
			return false;
		}
		if (document.forms.regform2.ic.value.length<6) {
			alert('Chybně zadáno IČ [min. 6 znaků].');
			document.forms.regform2.ic.focus();
			return false;
		}
	} else {
		/*
		if (document.forms.regform2.rc.value.length<6) {
			alert('Chybně zadáno rodné číslo / č. občanského průkazu. [min. 6 znaků].');
			document.forms.regform2.rc.focus();
			return false;
		}
		*/
	}
	
	return true;
}

function check_regform_step3() {
	if (!document.forms.regform3.activationcodeY.checked && !document.forms.regform3.activationcodeN.checked) {
		alert('Musíte vybrat, zda-li máte již vygenerován aktivační kód.');
		return false;
	}
	return true;
}

function check_regenteractivationcodeform() {
	var reActivationcode = new RegExp("^([a-zA-Z0-9]){20}$");
	var reActivationcode2 = new RegExp("^(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4})$");
	if (!document.forms.regenteractivationcodeform.activationcode.value.match(reActivationcode) && !document.forms.regenteractivationcodeform.activationcode.value.match(reActivationcode2)) {
    	alert('Chybně zadán aktivační kód.');
		document.forms.regenteractivationcodeform.activationcode.focus();
		return false;
  	}
}


function check_regform_step5() {
	var reAmount = new RegExp("^\\d+$|^\\d+\\.\\d\\d$");
	if (document.forms.regform5.paymenttype.selectedIndex == 0) {
		alert('Musí být vybrán způsob úhrady.');
		document.forms.regform5.paymenttype.focus();
		return false;
	}
	if (!document.forms.regform5.amount.value.match(reAmount)) {
		alert('Chybný formát částky.');
		document.forms.regform5.amount.focus();
		return false;
	}
}


function check_login() {
	if ((document.forms.login.login.value.length<1)||(document.forms.login.password.value.length<1)) {
		alert('Vyplňte uživatelské jméno a heslo.');
		document.forms.login.login.focus();
		return false;
	}
}


function check_editaddress() {
	var reZip = new RegExp("^\\d{3}[ ]?\\d{2}$");
	if (document.forms.editaddress.street.value.length<1) {
		alert('Chybně zadaná ulice / Č.P. [min. 1 znak].');
		document.forms.editaddress.street.focus();
		return false;
	}
	if (document.forms.editaddress.city.value.length<2) {
		alert('Chybně zadáno město / obec [min. 2 znaky].');
		document.forms.editaddress.city.focus();
		return false;
	}
	if (!document.forms.editaddress.zipcode.value.match(reZip)) {
		alert('Chybně zadáno PSČ.');
		document.forms.editaddress.zipcode.focus();
		return false;
	}
	return true;	
}


function check_editcontacts() {
	var rePhoneNumber = new RegExp("^[\+]?[0-9]{9,}$");
	var reEmail = new RegExp("^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$");
	if (!document.forms.editcontacts.phone.value.match(rePhoneNumber)&&!document.forms.editcontacts.mobile.value.match(rePhoneNumber)) {
		alert('Musí být zadán alespoň jeden telefonický kontakt.');
		document.forms.editcontacts.phone.focus();
		return false;
	}
	if (document.forms.editcontacts.fax.value.length>1 && !document.forms.editcontacts.fax.value.match(rePhoneNumber)) {
		alert('Chybně zadáno faxové číslo.');
		document.forms.editcontacts.fax.focus();
		return false;
	}
	if (!document.forms.editcontacts.email.value.match(reEmail)) {
		alert('Chybně zadán email.');
		document.forms.editcontacts.email.focus();
		return false;
	}
	return true;	
}


function check_inputactivationcodeform() {
	var reActivationcode = new RegExp("^([a-zA-Z0-9]){20}$");
	var reActivationcode2 = new RegExp("^(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4} )(([a-zA-Z0-9]){4})$");
	if (!document.forms.inputactivationcodeform.activationcode.value.match(reActivationcode) && !document.forms.inputactivationcodeform.activationcode.value.match(reActivationcode2)) {
    	alert('Chybně zadán aktivační kód.');
		document.forms.inputactivationcodeform.activationcode.focus();
		return false;
  	}
}

function check_password() {
	if (document.forms.changepassword.password_new.value.length<6) {
		alert('Chybně zadáno nové heslo [min. 6 znaků].');
		document.forms.changepassword.password_new.focus();
		return false;
	}
	
	if (document.forms.changepassword.password_new.value != document.forms.changepassword.password_new_confirm.value) {
		alert('Zadaná nová hesla nejsou stejná.');
		document.forms.changepassword.password_new.focus();
		return false;
	}
	
	if (document.forms.changepassword.password_new.value == document.forms.changepassword.password_old.value) {
		alert('Staré a nové heslo je totožné.');
		document.forms.changepassword.password_new.focus();
		return false;
	}
}

function md5form() {
	document.forms.login.hpassword.value = hex_hmac_md5(document.forms.login.password.value,document.forms.login.challenge.value);
	document.forms.login.password.disabled = true;
	document.forms.login.submit();
	document.forms.login.password.disabled = false;
	return false;
}
