var dtCh= "/";
var minYear=1900;
var maxYear=2004;

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++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
        // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
        for (var i = 1; i <= n; i++) {
                this[i] = 31
                if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
                if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
        var britedate=""
        var daysInMonth = DaysArray(12)
        var pos1=dtStr.indexOf(dtCh)
        var pos2=dtStr.indexOf(dtCh,pos1+1)
        var strMonth=dtStr.substring(pos1+1,pos2)
        var strDay=dtStr.substring(0,pos1)
        var strYear=dtStr.substring(pos2+1)
        strYr=strYear

        britedate = strYr + "/" + strMonth + "/" + strDay

        if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
        if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
        for (var i = 1; i <= 3; i++) {
                if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
        }
        month=parseInt(strMonth)
        day=parseInt(strDay)
        year=parseInt(strYr)
        if (pos1==-1 || pos2==-1){
        //        alert("The date format should be : dd/mm/yyyy")
                return false
        }
        if (strMonth.length<1 || month<1 || month>12){
        //        alert("Please enter a valid month")
                return false
        }
        if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        //        alert("Please enter a valid day")
                return false
        }
        if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        //        alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
                return false
        }
        if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isNumber(stripCharsInBag(dtStr, dtCh))==false){
        //        alert("Please enter a valid date")
                return false
        }

// document.Register.i_birthdate.value = britedate
return true
}

function isEmail(str) {
        var supported = 0;
        if (window.RegExp) {
                var tempStr = "a";
                var tempReg = new RegExp(tempStr);
                if (tempReg.test(tempStr)) supported = 1;
        }
        if (!supported)
                return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
        var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
        var r2 = new RegExp("^[a-zA-Z0-9\\-\\.\\_]+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
        return (!r1.test(str) && r2.test(str));
}

function valid_chars(valid,str) {
        var temp;
        for (var i=0; i<str.length; i++) {
                temp = "" + str.substring(i, i+1);
                flag = valid.indexOf(temp)+1 ;
                if (!flag) return false ;
        }
        return true ;
}

function valid_name(str) {
        return valid_chars(" -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZαβγδεζηθικλμνξοπρστυφχψωςΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέίύόήώϊϋΐΰ’ΈΊΎΏΉΈ",str);
}
function valid_addressGR(str) {
        return valid_chars(", -1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZαβγδεζηθικλμνξοπρστυφχψωςΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέίύόήώϊϋΐΰ’ΈΊΎΏΉΈ ",str);
}

function valid_tel(str) {
        return valid_chars("1234567890",str);
}

function valid_postcodeGR(str) {
        return valid_chars(" 1234567890",str);
}

function valid_postcodeINT(str) {
        return valid_chars("1234567890abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ",str);
}

function isNumber(str) {
        return valid_chars("1234567890",str);
}

function len(str) {
        return str.length;
}


function checkAge()
                {
                       /* the minumum age you want to allow in */
                        var min_age = 18;

                        /* change "age_form" to whatever your form has for a name="..." */
                        var year = parseInt(document.Register.byear.value);
                        var month = parseInt(document.Register.bmonth.value) - 1;
                        var day = parseInt(document.Register.bday.value);

                        var theirDate = new Date((year + min_age), month, day);
                        var today = new Date;

                        if ( (today.getTime() - theirDate.getTime()) < 0) {
                               // alert("Η είσοδος στο site επιτρέπεται μόνο σε άτομα άνω των 18 ετών");
                                return false;
                        }
                        else {
                                return true;
                        }
                }


function checkFields() {
        missinginfo = "";

/*
        if (document.Subscribe.gender.value == 3) {
                 missinginfo += "\n- Δεν έχετε συμπληρώσει Τίτλο";
                }
*/
        if (document.Subscribe.fname.value == "") {
                 missinginfo += "\n- Please enter your Name";
                } else {
                     if (!valid_name(document.Subscribe.fname.value)) { missinginfo += "\n- The Name you have entered is not valid"; }
                }

        if (document.Subscribe.sname.value == ""){
                 missinginfo += "\n- Please enter your Surname";
                } else {
                        if (!valid_name(document.Subscribe.sname.value)) { missinginfo += "\n- The Surname you have entered is not valid"; }
                }

        if (document.Subscribe.email.value == "" ) {
            missinginfo += "\n- Please enter your Email";
            }else{
            if (!isEmail(document.Subscribe.email.value)) { missinginfo += "\n- The Email you have entered is not valid"; }
            }

       /*   if (document.Subscribe.address.value == ""){
                 missinginfo += "\n- Please enter your Address";
                } else {
                        if (!valid_addressGR(document.Subscribe.address.value)) { missinginfo += "\n- The Address you have entered is not valid"; }
                }
*/
        if (len(document.Subscribe.postcode.value) > 0) {
                if (!valid_postcodeGR(document.Subscribe.postcode.value) || (document.Subscribe.postcode.value=="") || !(document.Subscribe.postcode.value.length==5)) {
                         missinginfo += "\n- The Postcode you have entered is not valid";
                }
             }else{
             missinginfo += "\n- Please enter your Postcode";
             }

/*
        if (len(document.Subscribe.mobile_phone.value) > 0) {
                if (!valid_tel(document.Subscribe.mobile_phone.value) || (document.Subscribe.mobile_phone.value=="") || !(document.Subscribe.mobile_phone.value.length > 9)) {
                         missinginfo += "\n- Το τηλέφωνο που έχετε συμπληρώσει δεν είναι αποδεκτό";
                }
        }
*/
 /*       if ( 
		(len(document.Subscribe.tel.value)==0) ||
                (!valid_tel(document.Subscribe.tel.value)) || 
		(document.Subscribe.tel.value=="") || 
		(!(document.Subscribe.tel.value.length > 9)) 
	) {

		missinginfo += "\n- The phone number you have entered is not valid.\n Please use only numbers.";
        }

        if ( (document.Subscribe.sms_optin.checked) && ( 
		(len(document.Subscribe.mobile.value)==0) ||
                (!valid_tel(document.Subscribe.mobile.value)) || 
		(document.Subscribe.mobile.value=="") || 
		(!(document.Subscribe.mobile.value.length > 9)) 
	) ) {
*/
		missinginfo += "\n- You need to enter your Cellphone number in order to receive SMS messages.";
        }

        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Please fill in correctly all mandatory fields:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}

function checkEcards() {
        missinginfo = "";


        if (document.preform.echeck.checked == 0) {
                 missinginfo += "\n- Δεν έχετε διαλέξει E CARD";
                }


          if (document.preform.smail.value == ""){
                 missinginfo += "\n- Δεν έχετε συμπληρώσει τo Email σας ";
                }else{
            if (!isEmail(document.preform.smail.value)) { missinginfo += "\n- Το Email που έχετε συμπληρώσει δεν είναι αποδεκτό"; }
            }

        if (document.preform.recmail.value == "" ) {
            missinginfo += "\n- Δεν έχετε συμπληρώσει το Email του παραλήπτη";
            }

        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Δεν έχετε συμπληρώσει σωστά όλα τα απαραίτητα στοιχεία:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}




function checkContact() {
        missinginfo = "";


        if (document.contact.subj.value == "") {
                 missinginfo += "\n- Δεν έχετε συμπληρώσει Θέμα";
                } else {
                     if (!valid_name(document.contact.subj.value)) { missinginfo += "\n- Το Θέμα που έχετε συμπληρώσει δεν είναι αποδεκτό"; }
                }
        if (document.contact.name.value != "") {
                    if (!valid_name(document.contact.name.value)) { missinginfo += "\n- Το Όνομά που έχετε συμπληρώσει δεν είναι αποδεκτό"; }
                }

        if (document.contact.email.value != "" ) {           
            if (!isEmail(document.contact.email.value)) { missinginfo += "\n- Το Email που έχετε συμπληρώσει δεν είναι αποδεκτό"; }
            }

 if (document.contact.msg.value == "") {
                 missinginfo += "\n- Δεν έχετε συμπληρώσει Μύνημα";
                }
     

        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Δεν έχετε συμπληρώσει σωστά όλα τα απαραίτητα στοιχεία:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}

function checkFieldsContact() {
        missinginfo = "";

/*
        if (document.Subscribe.gender.value == 3) {
                 missinginfo += "\n- Δεν έχετε συμπληρώσει Τίτλο";
                }
*/
        if (document.Subscribe.fname.value == "") {
                 missinginfo += "\n- Δεν έχετε συμπληρώσει Όνομα";
                } else {
                     if (!valid_name(document.Subscribe.fname.value)) { missinginfo += "\n- Το Όνομά που έχετε συμπληρώσει δεν είναι αποδεκτό"; }
                }

        if (document.Subscribe.sname.value == ""){
                 missinginfo += "\n- Δεν έχετε συμπληρώσει Επώνυμο";
                } else {
                        if (!valid_name(document.Subscribe.sname.value)) { missinginfo += "\n- Το Επίθετο που έχετε συμπληρώσει δεν είναι αποδεκτό"; }
                }

        if (
		(document.Subscribe.email.value == "" ) &&
		(document.Subscribe.tel.value == "" ) &&
		(document.Subscribe.mobile.value == "" ) 
	){
            missinginfo += "\n- Δεν έχετε συμπληρώσει Email, Τηλέφωνο ή Κινητό";
        }
        if ( (document.Subscribe.email.value!="") && !isEmail(document.Subscribe.email.value)) { missinginfo += "\n- Το Email που έχετε συμπληρώσει δεν είναι αποδεκτό"; }

       /* if (len(document.Subscribe.postcode.value) > 0) {
                if (!valid_postcodeGR(document.Subscribe.postcode.value) || (document.Subscribe.postcode.value=="") || !(document.Subscribe.postcode.value.length==5)) {
                         missinginfo += "\n- Ο Τ.Κ. που έχετε συμπληρώσει δεν είναι αποδεκτός";
                }
        }
*/
        if ( (document.Subscribe.tel.value!="" ) && ( 
                (!valid_tel(document.Subscribe.tel.value)) || 
		(!(document.Subscribe.tel.value.length > 9)) ) 
	){

		missinginfo += "\n- Το τηλέφωνο που έχετε συμπληρώσει δεν είναι αποδεκτό.\n Χρησιμοποιήστε μόνο ψηφία.";
        }

        if ( (document.Subscribe.mobile.value!="" ) && ( 
                (!valid_tel(document.Subscribe.mobile.value)) || 
		(!(document.Subscribe.mobile.value.length > 9)
	) ) ) {

		missinginfo += "\n- Το κινητό που συμπληρώσατε έχει σφάλμα.";
        }

        if ( len(document.Subscribe.usrinput.value)>255   )
	{

		missinginfo += "\n- Το κείμενο των σχολίων δεν πρέπει να υπερβαίνει τους 255 χαρακτήρες.";
        }


        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Please fill in correctly all mandatory fields:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}


function checkFieldsContactShort() {
        missinginfo="";


        if (document.shortform.sname.value=="") {
                 missinginfo += "\n- Please enter your Name and Surname";
                } else {
                     if (!valid_name(document.shortform.sname.value)) { missinginfo += "\n- The Name and Surname you have entered is not valid"; }
                }       

       if(document.shortform.tel.value==""){
	missinginfo += "\n- Please enter your Telephone number";
	}
        if ( (document.shortform.tel.value!="" ) && ( 
                (!valid_tel(document.shortform.tel.value)) || 
		(!(document.shortform.tel.value.length > 9)) ) 
	){

		missinginfo += "\n- The Telephone number you have entered is not valid.\n Please use only numbers.";
        }
       

        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Please fill in correctly all mandatory fields:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}




function checkFieldsMembership() {
        missinginfo = "";

/*
        if (document.Subscribe.gender.value == 3) {
                 missinginfo += "\n- Please enter your name";
                }
*/
        if (document.Subscribe.fname.value == "") {
                 missinginfo += "\n- Name";
                } else {
                     if (!valid_name(document.Subscribe.fname.value)) { missinginfo += "\n- Name is not acceptable"; }
                }

        if (document.Subscribe.sname.value == ""){
                 missinginfo += "\n- Surname";
                } else {
                        if (!valid_name(document.Subscribe.sname.value)) { missinginfo += "\n- Surname is not acceptable"; }
                }

        if (document.Subscribe.fathername.value == ""){
                 missinginfo += "\n- Your father's name";
                } else {
                        if (!valid_name(document.Subscribe.fathername.value)) { missinginfo += "\n- Your father;'s name has invalid characters"; }
                }

          if (document.Subscribe.address.value == ""){
                 missinginfo += "\n- address";
                } else {
                        if (!valid_addressGR(document.Subscribe.address.value)) { missinginfo += "\n- Address is not acceptable"; }
                }
          if (document.Subscribe.address2.value == ""){
                 missinginfo += "\n- Area";
                } else {
                        if (!valid_addressGR(document.Subscribe.address2.value)) { missinginfo += "\n- Area is not acceptable"; }
                }

  /*      if (len(document.Subscribe.postcode.value) > 0) {
                if (!valid_postcodeGR(document.Subscribe.postcode.value) || (document.Subscribe.postcode.value=="") || !(document.Subscribe.postcode.value.length==5)) {
                         missinginfo += "\n- Postcode is not acceptable";
                }
             }else{
             missinginfo += "\n- Postcode";
             }
*/
		if ((len(document.Subscribe.mobile.value)==0) && (len(document.Subscribe.tel.value)==0) )
	        {
			missinginfo += "\n- You must submit at least tel or mobile \n";
		}
		else
		{
			if(len(document.Subscribe.tel.value)>0){
			if ( 			
        		        (!valid_tel(document.Subscribe.tel.value)) || 
				 
				(!(document.Subscribe.tel.value.length > 9))   
			)
			{
				missinginfo += "\n- Tel is not acceptable.\n   Use digits only";
        		} 
			
			}
			if (len(document.Subscribe.mobile.value)>0){
		
	        		if ( 
				
               			 (!valid_tel(document.Subscribe.mobile.value)) || 
				 
				(!(document.Subscribe.mobile.value.length > 9))  
				) 
			 	{

					missinginfo += "\n- Mobile is not acceptable.\n   Use digits only.";	
	        		}
			}

	}

        if (document.Subscribe.email.value == "" ) {
            missinginfo += "\n- Email";
            }else{
            if (!isEmail(document.Subscribe.email.value)) { missinginfo += "\n- Your email is not acceptable"; }
            }

// Check the "work" fields only for corporate registrations
if (document.Subscribe.reg_type[1].checked==true) {

          if (document.Subscribe.company.value == ""){
                 missinginfo += "\n- Company";
          }
          if (document.Subscribe.business_addr.value == ""){
                 missinginfo += "\n- Company details";
                } else {
                        if (!valid_addressGR(document.Subscribe.business_addr.value)) { missinginfo += "\n- Company Address is not acceptable"; }
                }
          if (document.Subscribe.business_address2.value == ""){
                 missinginfo += "\n- Area (company details)";
                } else {
                        if (!valid_addressGR(document.Subscribe.business_address2.value)) { missinginfo += "\n- Company address is not acceptable"; }
                }

        if (len(document.Subscribe.business_postcode.value) > 0) {
                if (!valid_postcodeGR(document.Subscribe.business_postcode.value) || (document.Subscribe.business_postcode.value=="") || !(document.Subscribe.business_postcode.value.length==5)) {
                         missinginfo += "\n- Postcode (company details) is not acceptable";
                }
             }else{
             missinginfo += "\n- Postcode (company details)";
             }
        if ( 
		(len(document.Subscribe.business_tel.value)==0) ||
                (!valid_tel(document.Subscribe.business_tel.value)) || 
		(document.Subscribe.business_tel.value=="") || 
		(!(document.Subscribe.business_tel.value.length > 9)) 
	) {
		missinginfo += "\n- Tel is not acceptable (company details)";
        }

        if (document.Subscribe.business_email.value == "" ) {
            missinginfo += "\n- Email (company details)";
            }else{
            if (!isEmail(document.Subscribe.business_email.value)) { missinginfo += "\n- Email (company details) is not acceptable"; }
            }
        if (document.Subscribe.job.value == "" ) {
            missinginfo += "\n- Job title";
        }
}

        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Please fill in all required details:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}

function checkEcards() {
        missinginfo = "";


        if (document.preform.echeck.checked == 0) {
                 missinginfo += "\n- Δεν έχετε διαλέξει E CARD";
                }


          if (document.preform.smail.value == ""){
                 missinginfo += "\n- Δεν έχετε συμπληρώσει τo Email σας ";
                }else{
            if (!isEmail(document.preform.smail.value)) { missinginfo += "\n- Το Email που έχετε συμπληρώσει δεν είναι αποδεκτό"; }
            }

        if (document.preform.recmail.value == "" ) {
            missinginfo += "\n- Δεν έχετε συμπληρώσει το Email του παραλήπτη";
            }

        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Δεν έχετε συμπληρώσει σωστά όλα τα απαραίτητα στοιχεία:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}


function checkTerms() {
        missinginfo = "";
        if ( !document.Subscribe.accept_terms.checked) {
            missinginfo += "\n- Για να ολοκληρωθεί η διαδικασία θα πρέπει να αποδέχεστε τους όρους και τους κανονισμούς.\n  Παρακαλούμε τσεκάρετε την σχετική επιλογή.";
	}

        if (missinginfo != "") {
                missinginfo ="_____________________________________________________\n" +
                "Δεν έχετε συμπληρώσει σωστά όλα τα απαραίτητα στοιχεία:\n" +
                missinginfo ;

                alert(missinginfo);
                return false;
        }else true
}


