// JavaScript Document

// Check if the checkbox is checked or not to fill or empty the fields
function checkBox_ch(aCheck, st, fn, type_of)
{
	if (aCheck.checked == true)
	{
		fill_out_r_form(st, fn, type_of);
	}
	else
	{
		empty_fields(st, fn, type_of);
	}
}
	// Fill out the other info with the same as the Owner
	function fill_out_r_form(st, fn, type_of)
	{
		for (i=st; i<=fn; i++)
		{
			test = document.forms[0].elements[i].name;
			verify1 = type_of+"_name";
			verify2 = type_of+"_email";
			verify3 = type_of+"_city";
			verify4 = type_of+"_addr";
			verify5 = type_of+"_strno";
			verify6 = type_of+"_zip";
			verify7 = type_of+"_country";
			verify8 = type_of+"_tel";
			verify9 = type_of+"_fax";
			if (test==verify1){ document.forms[0].elements[i].value = document.form1.organisation_name.value;}
			else if (test==verify2){ document.forms[0].elements[i].value = document.form1.organisation_email.value;}
			else if (test==verify3){ document.forms[0].elements[i].value = document.form1.organisation_city.value;}
			else if (test==verify4){ document.forms[0].elements[i].value = document.form1.organisation_addr.value;}
			else if (test==verify5){ document.forms[0].elements[i].value = document.form1.organisation_strno.value;}
			else if (test==verify6){ document.forms[0].elements[i].value = document.form1.organisation_zip.value;}
			else if (test==verify7){ document.forms[0].elements[i].value = document.form1.organisation_country.value;}
			else if (test==verify8){ document.forms[0].elements[i].value = document.form1.organisation_tel.value;}
			else if (test==verify9){ document.forms[0].elements[i].value = document.form1.organisation_fax.value;}
		}
	}
	
	// Empty fields
	function empty_fields(st, fn, type_of)
	{
		for (i=st; i<=fn; i++)
		{
			document.forms[0].elements[i].value = "";
		}
	}

// Check if a field required is valid
function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please fill the field  "' + fieldLabel +'".');
		formField.focus();
		result = false;
	}
	
	return result;
}

// Check if the e-mail Adress is valid
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

// Check if the email is valid
function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please fill the field with a valid e-mail address.");
		formField.focus();
		result = false;
	}
   
  return result;

}

// Check if all the data are numbers
function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}
// Check valid number
function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!allDigits(formField.value))
 		{
 			alert('Please fill the Field "' + fieldLabel +'" with numbers.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

// Check if the telephone of format (+XX.XXXXXXXXXX) is valid
function isPhoneNumber(aTextField, fieldLabel, required)
{		//if (document.forms[0][aTextField].value.search(/\+?\d{2}\)? ?\[-.]\d{20}/) != -1)
	var result = true;
	if (required && !validRequired(aTextField,fieldLabel))
		result = false;
		
	if (aTextField.value.length>0 && aTextField.value.search(/^\+[0-9]{1,3}\.[0-9]{1,14}$/) == -1) //if match failed
	{
		alert('Please fill the field "' + fieldLabel +'"\nwith a valid telephone number like this "+XX.XXXXXXXXXX".');
		aTextField.focus();
		result = false;
	}
	
	return result;
}

// Start Check IP Address ----------->
// check if the IP Address is valid v4 address
function isValidIPAddress(aTextField, fieldLabel, required) {
	
	var result = true;
	
	if (required && !validRequired(aTextField,fieldLabel))
		result = false;
	
	if (aTextField.value.length>0 && !isValidIPv4Address(aTextField.value))
	{
		alert('Please fill the field "' + fieldLabel +'"\nwithe a valid IP address.');
		aTextField.focus();
		result = false;
	}
	return result;
	
}//end

function isValidIPv4Address(IPvalue) {
	var ipV4Pattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipV4Pattern); 
	
	if (IPvalue == "0.0.0.0" ||
		IPvalue == "255.255.255.255" ||
		IPvalue == "127.0.0.1" ||
		ipArray == null ||
		(ipArray[1] == 10) ||
		(ipArray[1] == 172 && (ipArray[2]>15 && ipArray[2]<32)) ||
		(ipArray[1] == 192 && ipArray[2]==168)
		
		)
			return false;
	else {
		for (i = 0; i < 5; i++) {
			thisSegment = ipArray[i];
			if (thisSegment > 255 ||
				(i==1 && thisSegment==0) ||
				(i==4 && (thisSegment==0 || thisSegment==255))) {
				return false;
				i = 5;
			}//if
	   }//for
	}//else
	
	return true;
}//end

// not working yet
/*function isValidIPv6Address(ipaddr) {
// check if there is a '::' in the field
	var parts = ipaddr.split("::");
	
	if(parts.length == 1) {
		// there is NO occurence of '::'
		//var re11 = /^[A-Fa-f0-9]{1,4}(:[A-Fa-f0-9]{1,4}){7,7}?$/;
		var re11 = /^([0]\:){6}\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/\d{1,2})?$/;
		if (re11.test(ipaddr)) {
		// IPv4 address in IPv6 address 0:0:0:0:0:0:n.n.n.n/nn e.g. 0:0:0:0:0:0:10.10.10.10/24
		var ipv4parts = ipaddr.split(":");
		return isValidIPv4Address(ipv4parts[6]);
	}
	
	// Pure IPv6 address
	var re12 = /^[A-Fa-f0-9]{1,4}\:[A-Fa-f0-9]{1,4}\:[A-Fa-f0-9]{1,4}\:[A-Fa-f0-9]{1,4}\:[A-Fa-f0-9]{1,4}\:[A-Fa-f0-9]{1,4}\:[A-Fa-f0-9]{1,4}\:[A-Fa-f0-9]{1,4}(\/\d{1,2})?$/;
	if (re12.test(ipaddr)) return false;
	else return true;
	}
	
	if(parts.length == 2) {
	
		// there is one occurence of '::'
		if (parts[0] == "") {
		// IPv4 address in IPv6 address ::n.n.n.n/nn e.g. :10.10.27.127/24
		return isValidIPv4Address(parts[1]);
	}
	
	// Pure IPv6 address
	
	// check first part
	var re21 = /^[A-Fa-f0-9]{1,4}(\:[A-Fa-f0-9]{1,4}){0,5}$/;
	if(!re21.test(parts[0])) {
	return true
	}
	
	// check second part
	var re22 = /^([A-Fa-f0-9]{1,4}\:){0,5}[A-Fa-f0-9]{1,4}(\/\d{1,2})?$/
	if(!re22.test(parts[1])) {
		return true;
	}
		return false;
	}
	
	// there is more that one occurence of '::'
	return true;

}*/

// <------------------- End Check IP Address

// Check Domain Servers Name // prepei na vro to string match
function checkDomainName(aTextField, fieldLabel, required)
{
	var result = true;
	
	if (required && !aTextField.value.match(DomainNamePattern))
	{
		alert ('The field "' + fieldLabel +'" is not valid.');
		aTextField.focus();
		result = false;
	}
	else if (aTextField.value.length>0 && !aTextField.value.match(DomainNamePattern))
	{
		alert ('The field "' + fieldLabel +'" is not valid.');
		aTextField.focus();
		result = false;
	}
	
	return result;
}

// Check Password
function checkPassword(aTextfield,bTextfield,afieldLabel,bfieldLabel) {
	var result = true;
	
	if (aTextfield.value.length < 6 )
	{
		alert('The field "' + afieldLabel +'" must have at list 6 characters.');
		aTextfield.focus();
		result = false;
	}
	else if (aTextfield.value != bTextfield.value)
	{
		alert('The fields "' + afieldLabel +'" and "'+ bfieldLabel +'" are not the same.');
		bTextfield.focus();
		result = false;
	}
	
	return result;
		
}

// Check if the user Agrees with the rules
function checkAgreement(aCheckBox, fieldLabel)
{
	var result = true;
	
	if (aCheckBox.checked == false)
	{
		alert('You must choose "' + fieldLabel + '" in order to continue.');
		aCheckBox.focus();
		result = false;
	}
	
	return result;
}

// Validate type of Company
function validateCompanyType(aSelectBox, aTextField, fieldLabel)
{
	var result = true;
	
	if (aSelectBox.value == "IDIOTIS" || aSelectBox.value == "ELEPAGELMATIAS")
	{
		//alert('Πρέπει να συμπληρώσετε "' + fieldLabel + '" για να συνεχίσετε.');
		//result = false;
		//validateIDNo(aTextField, fieldLabel);
		if (!validRequired(aTextField,fieldLabel))
			result = false;
	}
	else
	{
		result = true;
	}
	
	return result;
}

// Validate IDNo
/*function validateIDNo(aTextField, fieldLabel)
{
	
}*/

// Validate the Vat No
function validateVAT(aSelectBox, aTextField, fieldLabel, aTextArea, fieldArea)
{
	var result = true;
	
	if (aSelectBox.value == "XX_")
	{
		result = true;
	}
	else if (aTextField.value == "999999999")
	{
		if (!validRequired(aTextArea, fieldArea))
			result = false;
	}
	else
	{
		if (!validNum(aTextField,fieldLabel,true))
			result = false;
	}
	
	return result;
}

// Form validation
function validateForm(theForm)
{
	// Start Validation ----------------->
	if (!validRequired(theForm.domain_name, "Domain(s) Name(s)"))
		return false;
	
	if (!validRequired(theForm.gen_comnts_2, "Content Description"))
		return false;
	
	if (!validRequired(theForm.primary_ns_name, "Name for the 1st DNS Server"))
		return false;
		
	if (!isValidIPAddress(theForm.primary_ns_addr, "IP for the 1st DNS Server", true))
		return false;
	
	if (!validRequired(theForm.sec1_ns_name, "Name for the 2nd DNS Server"))
		return false;
		
	if (!isValidIPAddress(theForm.sec1_ns_addr, "IP for the 2nd DNS Server", true))
		return false;
	
	if (!isValidIPAddress(theForm.sec2_ns_addr, "IP for the 3th DNS Server", false))
		return false;
	
	if (!isValidIPAddress(theForm.sec3_ns_addr, "IP for the 4th DNS Server", false))
		return false;
	
	if (!checkPassword(theForm.password1,theForm.password2,"Password","Password Confirmation"))
		return false;
	
	if (!validRequired(theForm.organisation_name,"Owner Name or Company Name"))
		return false;

	if (!validEmail(theForm.organisation_email,"e-mail",true))
		return false;

	if (!validateCompanyType(theForm.organisation_cat, theForm.organisation_id_no, "ID Number"))
		return false;
	
	if (!validateVAT(theForm.afm_country, theForm.organisation_afm,"V.A.T. (number)", theForm.gen_comnts, "General Comments"))
		return false;
	
	if (!validRequired(theForm.organisation_city, "Owners City"))
		return false;
	
	if (!validRequired(theForm.organisation_addr, "Owners Address"))
		return false;
	
	if (!validRequired(theForm.organisation_strno, "Owners Address Number", true))
		return false;
	
	if (!validRequired(theForm.organisation_zip, "Owners Postal Code", true))
		return false;
	
	if (!isPhoneNumber(theForm.organisation_tel,"Owners Telephone",true))
		return false;
	
	if (!isPhoneNumber(theForm.organisation_fax,"Owners Fax",false))
		return false;
		
	// <--------- End of Owner Validation	
	
	// Start Postal Validation ------------>
	
	/*if (!validRequired(theForm.post_name,"Recipient Name"))
		return false;

	if (!validEmail(theForm.post_email,"Recipient e-mail",true))
		return false;
	
	if (!validRequired(theForm.post_city, "Recipients City"))
		return false;
	
	if (!validRequired(theForm.post_addr, "Recipients Address"))
		return false;
	
	if (!validRequired(theForm.post_strno, "Recipients Address number", true))
		return false;
	
	if (!validRequired(theForm.post_zip, "Recipients Postal Code", true))
		return false;
	
	if (!isPhoneNumber(theForm.post_tel,"Recipients Telephone",true))
		return false;
	
	if (!isPhoneNumber(theForm.post_fax,"Recipients Fax",false))
		return false;*/
	
	// <--------- End Postal Validation
	
	// Start Managerial Validation ------------>
	
	if (!validRequired(theForm.manager_name,"Administrative Officer Name"))
		return false;

	if (!validEmail(theForm.manager_email,"Administrative Officer e-mail",true))
		return false;
	
	if (!validRequired(theForm.manager_city, "Administrative Officer City"))
		return false;
	
	if (!validRequired(theForm.manager_addr, "Administrative Officer Address"))
		return false;
	
	if (!validRequired(theForm.manager_strno, "Administrative Officer Address number", true))
		return false;
	
	if (!validRequired(theForm.manager_zip, "Administrative Officer Postal Code", true))
		return false;
	
	if (!isPhoneNumber(theForm.manager_tel,"Administrative Officer Telephone",true))
		return false;
	
	if (!isPhoneNumber(theForm.manager_fax,"Administrative Officer Fax",false))
		return false;
	
	// <--------- End Managerial Validation
	
	// Start Technical Validation ------------>
	
	if (!validRequired(theForm.tech_manager_name,"Technical Officer Name"))
		return false;

	if (!validEmail(theForm.tech_manager_email,"Technical Officer e-mail",true))
		return false;
	
	if (!validRequired(theForm.tech_manager_city, "Technical Officer City"))
		return false;
	
	if (!validRequired(theForm.tech_manager_addr, "Technical Officer Address"))
		return false;
	
	if (!validRequired(theForm.tech_manager_strno, "Technical Officer Address number", true))
		return false;
	
	if (!validRequired(theForm.tech_manager_zip, "Technical Officer Postal Code", true))
		return false;
	
	if (!isPhoneNumber(theForm.tech_manager_tel,"Technical Officer Telephone",true))
		return false;
	
	if (!isPhoneNumber(theForm.tech_manager_fax,"Technical Officer Fax",false))
		return false;
	
	// <--------- End Technical Validation
	
	if (!checkAgreement(theForm.agree, "Agree with the Greek Law 1599/86"))
		return false;
	
	// <--------- End Validation
	
	return true;
}

// Credit card Validation

function validateCCForm(theForm)
{
	if (!validRequired(theForm.custName, "Full Name", true))
		return false;
	if (!validNum(theForm.ccNumber, "Credit Card Number", true))
		return false;
	if (!validNum(theForm.expDate1, "Date Of Expiry (month)", true))
		return false;
	if (!validNum(theForm.expDate2, "Date Of Expiry (year)", true))
		return false;		
	if (!validNum(theForm.cvccvv, "CVC/CVV No", true))
		return false;
	if (!validRequired(theForm.why, "Reason", true))
		return false;	
	return true;
}

// Popup in a new windows the register rules
function popup(){
	window.open('kanonismos_diaxeirisis_n.html','rules','location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,width='+(screen.width-10)+',height='+(screen.height-60)+'')
}

