// JavaScript Document
var whitespace = " \t\n\r";
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

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 daysInMonth = DaysArray(12);
    var pos1=dtStr.indexOf(dtCh);
    var pos2=dtStr.indexOf(dtCh,pos1+1);
    var strDay=dtStr.substring(0,pos1);
    var strMonth=dtStr.substring(pos1+1,pos2);
    var strYear=dtStr.substring(pos2+1);
    strYr=strYear;
    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 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
        alert("Please, check if selected dates are valid.");
        return false;
    }
return true;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isEmailChar (c)
{
    return ((c == ".") || (c == "_") || (c == "@") || (c == "-"))
}

function hasValidChars (s)
{
    var i;

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) || isEmailChar(c)) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}

function isEmail (s)
{
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    if (!hasValidChars(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function checkApplyForm() 
{
    var errorColor="#FFFF00";
    var color="#FFFFFF";
    var index;
    document.getElementById("strUsername").style.background=color;
    document.getElementById("strPassword").style.background=color;
    document.getElementById("strRepeatPassword").style.background=color;
    document.getElementById("strName").style.background=color;
    document.getElementById("strSurname").style.background=color;
    document.getElementById("strDateOfBirth").style.background=color;
    document.getElementById("strEmail").style.background=color;
    document.getElementById("strTelephone").style.background=color;
    document.getElementById("strCity").style.background=color;
    document.getElementById("strUniversity").style.background=color;
    document.getElementById("strFaculty").style.background=color;
    document.getElementById("strDepartment").style.background=color;
    document.getElementById("strPassportNumber").style.background=color;
    document.getElementById("strPassportExpiry").style.background=color;
    document.getElementById("strIssuedByCountry").style.background=color;
    document.getElementById("strDateOfArrival").style.background=color;
    document.getElementById("strDepartureBy").style.background=color;
    document.getElementById("strDateOfDeparture").style.background=color;
    document.getElementById("strTitle").style.background=color;
    document.getElementById("strFieldOfMedicine").style.background=color;
    document.getElementById("strAuthors").style.background=color;
    document.getElementById("strMentors").style.background=color;
    document.getElementById("strAbstractText").style.background=color;
    document.getElementById("strContactNameAndSurname").style.background=color;
    document.getElementById("strContactNumber").style.background=color;
    document.getElementById("strContactEmail").style.background=color;
    
    if (document.getElementById("strUsername").value == "") {
        alert("Mandatory field: Username.");
        document.getElementById("strUsername").style.background=errorColor;
        document.getElementById("strUsername").focus();
        return false;
    }

    if (document.getElementById("strPassword").value == "") {
        alert("Mandatory field: Password.");
        document.getElementById("strPassword").style.background=errorColor;
        document.getElementById("strPassword").focus();
        return false;
    }

    if (document.getElementById("strPassword").value.length < 6) {
        alert("Your password should be at least 6 characters long.");
        document.getElementById("strPassword").focus();
        document.getElementById("strPassword").style.background=errorColor;
        return false;
    }

    if (document.getElementById("strRepeatPassword").value == "") {
        alert("Mandatory field: Repeat Password.");
        document.getElementById("strRepeatPassword").style.background=errorColor;
        document.getElementById("strRepeatPassword").focus();
        return false;
    }

    if (document.getElementById("strPassword").value != document.getElementById("strRepeatPassword").value)
    {
       alert("Passwords do not match.");
       document.getElementById("strRepeatPassword").focus();
       document.getElementById("strRepeatPassword").style.background=errorColor;
       return false;
    }

    if (document.getElementById("strName").value == "") {
        alert("Mandatory field: First name.");
        document.getElementById("strName").style.background=errorColor;
        document.getElementById("strName").focus();
        return false;
    }

    if (document.getElementById("strSurname").value == "") {
        alert("Mandatory field: Last name.");
        document.getElementById("strSurname").style.background=errorColor;
        document.getElementById("strSurname").focus();
        return false;
    }
    
    if (document.getElementById("strDateOfBirth").value == "") {
        alert("Mandatory field: Date Of Birth.");
        document.getElementById("strDateOfBirth").style.background=errorColor;
        document.getElementById("strDateOfBirth").focus();
        return false;
    }

    if (isDate(document.getElementById("strDateOfBirth").value)==false)
    {
        document.getElementById("strDateOfBirth").style.background=errorColor;
        document.getElementById("strDateOfBirth").focus();
        return false;
    }
    
    if (document.getElementById("strEmail").value == "") {
        alert("Mandatory field: E-mail.");
        document.getElementById("strEmail").style.background=errorColor;
        document.getElementById("strEmail").focus();
        return false;
    }
    
    if (!isEmail(document.getElementById("strEmail").value)) {
        alert("Enter valid Email address.");
        document.getElementById("strEmail").style.background=errorColor;
        document.getElementById("strEmail").focus();
        return false;
    }

    if (document.getElementById("strTelephone").value == "") {
        alert("Mandatory field: Telephone.");
        document.getElementById("strTelephone").style.background=errorColor;
        document.getElementById("strTelephone").focus();
        return false;
    }

    if (document.getElementById("strCity").value == "") {
        alert("Mandatory field: City.");
        document.getElementById("strCity").style.background=errorColor;
        document.getElementById("strCity").focus();
        return false;
    }

    if (document.getElementById("strUniversity").value == "") {
        alert("Mandatory field: University.");
        document.getElementById("strUniversity").style.background=errorColor;
        document.getElementById("strUniversity").focus();
        return false;
    }

    if (document.getElementById("strFaculty").value == "") {
        alert("Mandatory field: Faculty.");
        document.getElementById("strFaculty").style.background=errorColor;
        document.getElementById("strFaculty").focus();
        return false;
    }

    if (document.getElementById("strDepartment").value == "") {
        alert("Mandatory field: Department.");
        document.getElementById("strDepartment").style.background=errorColor;
        document.getElementById("strDepartment").focus();
        return false;
    }

    if (document.getElementById("strPassportNumber").value == "") {
        alert("Mandatory field: Passport Number.");
        document.getElementById("strPassportNumber").style.background=errorColor;
        document.getElementById("strPassportNumber").focus();
        return false;
    }

    if (document.getElementById("strPassportExpiry").value != "") {
        if (isDate(document.getElementById("strPassportExpiry").value)==false)
        {
            document.getElementById("strPassportExpiry").style.background=errorColor;
            document.getElementById("strPassportExpiry").focus();
            return false;
        }
    }

    if (document.getElementById("strIssuedByCountry").value == "") {
        alert("Mandatory field: Passport Issued By Country.");
        document.getElementById("strIssuedByCountry").style.background=errorColor;
        document.getElementById("strIssuedByCountry").focus();
        return false;
    }

    if (document.getElementById("strDateOfArrival").value == "") {
        alert("Mandatory field: Date Of Arrival.");
        document.getElementById("strDateOfArrival").style.background=errorColor;
        document.getElementById("strDateOfArrival").focus();
        return false;
    }

    if (isDate(document.getElementById("strDateOfArrival").value)==false)
    {
        document.getElementById("strDateOfArrival").style.background=errorColor;
        document.getElementById("strDateOfArrival").focus();
        return false;
    }

    if (document.getElementById("strDepartureBy").value == "") {
        alert("Mandatory field: Departure By.");
        document.getElementById("strDepartureBy").style.background=errorColor;
        document.getElementById("strDepartureBy").focus();
        return false;
    }

    if (document.getElementById("strDateOfDeparture").value == "") {
        alert("Mandatory field: Date Of Departure.");
        document.getElementById("strDateOfDeparture").style.background=errorColor;
        document.getElementById("strDateOfDeparture").focus();
        return false;
    }

    if (isDate(document.getElementById("strDateOfDeparture").value)==false)
    {
        document.getElementById("strDateOfDeparture").style.background=errorColor;
        document.getElementById("strDateOfDeparture").focus();
        return false;
    }

    index = document.applyform.strTypeOfApplication.selectedIndex;

    if (document.applyform.strTypeOfApplication[index].value == "active") {
    if (document.getElementById("strTitle").value == "") {
        alert("Mandatory field: Title.");
        document.getElementById("strTitle").style.background=errorColor;
        document.getElementById("strTitle").focus();
        return false;
    }

    if (document.getElementById("strFieldOfMedicine").value == "") {
        alert("Mandatory field: FieldOfMedicine.");
        document.getElementById("strFieldOfMedicine").style.background=errorColor;
        document.getElementById("strFieldOfMedicine").focus();
        return false;
    }

    if (document.getElementById("strAuthors").value == "") {
        alert("Mandatory field: Authors.");
        document.getElementById("strAuthors").style.background=errorColor;
        document.getElementById("strAuthors").focus();
        return false;
    }

    if (document.getElementById("strMentors").value == "") {
        alert("Mandatory field: Supervisors.");
        document.getElementById("strMentors").style.background=errorColor;
        document.getElementById("strMentors").focus();
        return false;
    }

    if (document.getElementById("strAbstractText").value == "") {
        alert("Mandatory field: Abstract Text.");
        document.getElementById("strAbstractText").style.background=errorColor;
        document.getElementById("strAbstractText").focus();
        return false;
    }
    }

    if (document.getElementById("strContactNameAndSurname").value == "") {
        alert("Mandatory field: Contact Name And Surname.");
        document.getElementById("strContactNameAndSurname").style.background=errorColor;
        document.getElementById("strContactNameAndSurname").focus();
        return false;
    }

    if (document.getElementById("strContactNumber").value == "") {
        alert("Mandatory field: Contact Number.");
        document.getElementById("strContactNumber").style.background=errorColor;
        document.getElementById("strContactNumber").focus();
        return false;
    }

    if (document.getElementById("strContactEmail").value == "") {
        alert("Mandatory field: Contact E-mail.");
        document.getElementById("strContactEmail").style.background=errorColor;
        document.getElementById("strContactEmail").focus();
        return false;
    }

    if (!isEmail(document.getElementById("strContactEmail").value)) {
        alert("Enter valid Email address.");
        document.getElementById("strContactEmail").style.background=errorColor;
        document.getElementById("strContactEmail").focus();
        return false;
    }
}

function changeType() {
    var index;
    
    index = document.applyform.strTypeOfApplication.selectedIndex;
    
    if (document.applyform.strTypeOfApplication[index].value == "active") {
        /* enable fields */
        document.getElementById("strTypeOfPresentation").disabled = false;
        document.getElementById("strTitle").disabled = false;
        document.getElementById("strFieldOfMedicine").disabled = false;
        document.getElementById("strAuthors").disabled = false;
        document.getElementById("strMentors").disabled = false;
        document.getElementById("strAbstractText").disabled = false;
    } else {
        /* disable fields */
        document.getElementById("strTypeOfPresentation").selectedIndex = 0;
        document.getElementById("strTitle").value = "";
        document.getElementById("strFieldOfMedicine").value = "";
        document.getElementById("strAuthors").value = "";
        document.getElementById("strMentors").value = "";
        document.getElementById("strAbstractText").value = "";

        document.getElementById("strTypeOfPresentation").disabled = true;
        document.getElementById("strTitle").disabled = true;
        document.getElementById("strFieldOfMedicine").disabled = true;
        document.getElementById("strAuthors").disabled = true;
        document.getElementById("strMentors").disabled = true;
        document.getElementById("strAbstractText").disabled = true;
    }
}