﻿var normalHoursFrom = ["08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00"];
var normalHoursTo = ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00"];
var weekendHoursFrom = ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"];
var weekendHoursTo = ["10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00"];

var errorMessages = currentLangId == 1 ?
             ["Επώνυμο","Όνομα","Όνομα Πατρός","Τηλέφωνο Κινητό","Τηλέφωνο Σταθερό","Ημέρες/Ώρες Επικοινωνίας"]:
             ["Family Name","Name","Father's Name","Mobile Number","Home or Office Telephone Number","Contact Days/Hours"];

var captchaErrorMessage = currentLangId == 1 ?
             ["Παρακαλούμε πληκτρολογήστε τους χαρακτήρες που περιλαμβάνονται στην παραπάνω εικόνα!","Παρακαλούμε ανανεώστε τη σελίδα ..."]:
             ["Please enter the symbols you see on the panel above!","Please refresh the page ..."];

var err = currentLangId == 1 ? "Παρακαλούμε, συμπληρώστε το πεδίο: " : "Please, fill in the field: ";




function validateForm()
{
    if (clearErrMsgs() && checkObligatoryFields() && checkComments()){
		if (document.getElementById('sendForm').imagecheck.value == ''){
			alert(captchaErrorMessage[0]);
			document.getElementById('sendForm').imagecheck.focus();
			return false;
		}
		return true;				
	}
	return false;        
}



function checkObligatoryFields()
{
    var errCounter = 0;   
				
	if (document.getElementById('sendForm').LastName.value==''){				
		document.getElementById('formValidationError0').innerHTML = err + errorMessages[0];
		errCounter++;
	}					
	if (document.getElementById('sendForm').FirstName.value==''){				
		document.getElementById('formValidationError1').innerHTML = err + errorMessages[1];
		errCounter++;
	}					
	if (document.getElementById('sendForm').MiddleName.value==''){				
		document.getElementById('formValidationError2').innerHTML = err + errorMessages[2];
		errCounter++;
	}					
	if (document.getElementById('sendForm').PhoneMobileNo.value==''){				
		document.getElementById('formValidationError3').innerHTML = err + errorMessages[3];
		errCounter++;
	}
	if (document.getElementById('sendForm').PhoneNo.value==''){				
		document.getElementById('formValidationError4').innerHTML = err + errorMessages[4];			
		errCounter++;
	}						
	if (document.getElementById('sendForm').ContactDays.selectedIndex==0 ){				
		document.getElementById('formValidationError5').innerHTML = err + errorMessages[5] + "<br/>";			
		errCounter++;
	}	
		
	if (errCounter>0) 
	    return false;
	else
		return true;	
}



function checkComments()
{
	if(document.getElementById('custComments').value.length>300){
		if(document.getElementById('sendForm').langID.value==1)
			document.getElementById('custCommentsValidator').innerHTML = "Παρακαλώ εισάγετε μέχρι 300 χαρακτήρες.";
		else
		    document.getElementById('custCommentsValidator').innerHTML = "Please fill in the area with only 300 characters";
		    
		return false;
	}
	else
	    document.getElementById('custCommentsValidator').innerHTML="";		
	
	return true;							
}



function clearErrMsgs()
{		
	for(var i=0;i<16;i++)
	    if(document.getElementById('formValidationError'+i))
		    document.getElementById('formValidationError'+i).innerHTML = "";			
    
    return true;
}
	



    

function contactDaysChanged(){  
    
    var contactDaysSelectedIndex = document.getElementById('ContactDays').selectedIndex;
    if(contactDaysSelectedIndex == 0){
        document.getElementById('ContactHoursFrom').options.length = 0;        
        document.getElementById('ContactHoursTo').options.length = 0;
        
        document.getElementById('ContactHoursFrom').disabled = true;
        document.getElementById('ContactHoursTo').disabled = true;
    }
    else{
        if(contactDaysSelectedIndex != 7){
            fillHours(document.getElementById('ContactHoursFrom'), normalHoursFrom);
            fillHours(document.getElementById('ContactHoursTo'), normalHoursTo);            
        }
        else{
            fillHours(document.getElementById('ContactHoursFrom'), weekendHoursFrom);
            fillHours(document.getElementById('ContactHoursTo'), weekendHoursTo);
        }
        
        document.getElementById('ContactHoursFrom').selectedIndex = 0;
        document.getElementById('ContactHoursTo').selectedIndex = 0;
        
        document.getElementById('ContactHoursFrom').disabled = false;
        document.getElementById('ContactHoursTo').disabled = false;
    }

}


function fillHours(selectEl, hoursArray){
    selectEl.options.length = 0;
    for(i=0; i<hoursArray.length; i++)
        selectEl.options[i] = new Option(hoursArray[i], hoursArray[i]);
}


function hoursFromChanged(){
    var hoursFromIndex = document.getElementById('ContactHoursFrom').selectedIndex;    
    fillHours(document.getElementById('ContactHoursTo'), (document.getElementById('ContactDays').selectedIndex != 7 ? normalHoursTo : weekendHoursTo));
   
    var oldOptions = document.getElementById('ContactHoursTo').options;
    for(i=hoursFromIndex; i<oldOptions.length; i++)
        document.getElementById('ContactHoursTo').options[i-hoursFromIndex] = new Option(oldOptions[i].text, oldOptions[i].value);

    document.getElementById('ContactHoursTo').options.length = i - hoursFromIndex;
    document.getElementById('ContactHoursTo').selectedIndex = 0;
}

function initializeContactSelects(contactDays, contactHoursFrom, contactHoursTo){

    document.getElementById('ContactDays').selectedIndex = contactDays;
    contactDaysChanged();
    
    for(i=0; i<document.getElementById('ContactHoursFrom').options.length; i++)
        if( document.getElementById('ContactHoursFrom').options[i].text == contactHoursFrom){
            document.getElementById('ContactHoursFrom').selectedIndex = i;
            break;
        }
    
    hoursFromChanged();
    
    for(i=0; i<document.getElementById('ContactHoursTo').options.length; i++)
        if( document.getElementById('ContactHoursTo').options[i].text == contactHoursTo){
            document.getElementById('ContactHoursTo').selectedIndex = i;
            break;
        }
}
