//OPEN POP-UP ON CENTER OF SCREEN
function popup(page,w,h,scroll) {
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;

    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    scroll = scroll ? ',scrollbars=yes' : '';
    windowprops = "resizable=yes,height="+h+",width="+w+",top="+ wint +",left="+ winl+scroll;
    var mypop = window.open(page, "CostaOnline", windowprops);
    mypop.focus();
}

//submit form
function submitF(formT){
    var name = document.getElementById('Name');
    var email = document.getElementById('Email');
    var phone = document.getElementById('Telephone');
    var matchEmail =/email/i
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    
    if(formT == 'newsletter'){
        name = document.getElementById('Name2');
        email = document.getElementById('Emai2');
        phone = document.getElementById('Telephone2');
    }
    
    if( name.value == 'Name' ){ 
        alert('Name is required');
        name.style.backgroundColor='#E3F0FF'; 
        return false; 
    } else { name.style.backgroundColor='#FFFFFF'; }
    
    if( !filter.test(email.value) ) {
        alert('Invalid Email Address');
        email.style.backgroundColor='#E3F0FF'; 
        return false; 
    } else { email.style.backgroundColor='#FFFFFF'; }
    
    if( phone.value == 'Telephone' ){ 
        alert('Telephone is required');
        phone.style.backgroundColor='#E3F0FF'; 
        return false; 
    } else { phone.style.backgroundColor='#FFFFFF'; }
}

/* V A L I D A T E    F O R M */
function validateForm(validate) {
    var curr_field, fieldName, filterList, element, errno, tvalue, len;
    var missinginfo = '';
    var matchEmail =/email/i
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    var matchField = /([a-zA-Z0-9]{1}[\w_]+[a-zA-Z0-9]{1})\[([\S]{0,})\]/i

    /*filters used*/
    var filter1 = /len/i //maximum length
    var filter2 = /numeric/i //numeric fields
    var filter3 = /date/i //date format

    var checkList = validate.split(",");
    for( var z=0; z < checkList.length; z++ ) {
        //get the field name
        fieldName = checkList[z].replace(matchField,"$1");
        //get the field criterias
        curr_field = checkList[z].replace(matchField, "$2");
        
        if(document.getElementById(fieldName)){
        
            element = document.getElementById(fieldName);
            
            //check if this field is the email field to use different filter
            if(matchEmail.test(fieldName)) {
                if( !filter.test(element.value) ) { errno = 3;  } else { errno = 0;  }
                break;
            } else {
                //check if field is blank
                if( element.value == '' ) {
                    errno = 1;
                } else {
                    filterList = curr_field.split(";");
                    for( var f=0; f < filterList.length; f++ ) {
                        
                        tvalue = filterList[f].split("=");
                        if(filter1.test(filterList[f])) {//check field length
                            
                            if(element.value.length > tvalue[1] ) { errno = 2; len=tvalue[1]; } else { errno = 0; }
                            
                        } else if(filter2.test(filterList[f])){//check if field is numeric
                        
                            if(!IsNumeric(element.value)) { errno = 4; } else { errno = 0; }
                            
                        } else if(filter3.test(filterList[f])){//date format dd/mm/YYYY
                            
                            //check length
                            var dt = element.value.split("/");
                            var d = new Date();
                            if( dt[0] ){
                                if( IsNumeric(dt[0]) ){
                                    if( dt[0].length == 2 ){ errno = 0; } else { errno = 5; }//if day is more than 2 digits
                                    if( dt[0] > 0 && dt[0] < 32 ){ errno = 0; } else { errno = 5; }//if day is between 1 and 31
                                } else {
                                    errno = 5; //not numeric field
                                }
                            } else {
                                errno = 5; //not numeric field
                            }
                            
                            if( dt[1] && dt[1] != '' ){
                                if( IsNumeric(dt[1]) ){
                                    if( dt[1].length == 2 ){ errno = 0; } else { errno = 5; }//if month is more than 2 digits
                                    if( dt[1] > 0 && dt[1] < 13 ){ errno = 0; } else { errno = 5; }//if month is between 1 and 12
                                } else {
                                    errno = 5; //not numeric field
                                }
                            } else {
                                errno = 5; //not numeric field
                            }
                            
                            if( dt[2] && dt[2] != '' ){
                                if( IsNumeric(dt[2]) ){ 
                                    if( dt[2].length != 4 ){ errno = 5; } else { errno = 0; }//if year is not 4 digits
                                    if( dt[2] < d.getFullYear() ){ errno = 5; } else { errno = 0; }//if Year is equal or more than this Year
                                }                       
                            } else {
                                errno = 5; //not numeric field
                            }
                        }
                    }
                }
                
            }
            
        } else {
            errno = 1;
            break;
        }
        //clear underscores from field name
        fieldName = fieldName.replace(/[_' ']/g," ");

        //ERRORS messages type
        if(errno==0 || !errno){ //field is valid
            element.style.backgroundColor='#FFFFFF';
            
        } else if(errno==1) {//field is blank
            missinginfo += '\n - '+ capitalize(fieldName)+ ' is required';
            element.style.backgroundColor='#E3F0FF';
            
        } else if(errno==2) {//max characters permitted
            missinginfo += '\n - '+ capitalize(fieldName)+ ' Max. characters permitted = '+len;
            element.style.backgroundColor='#E3F0FF';
            
        } else if(errno==3) {//invalid email
            missinginfo += '\n - '+ capitalize(fieldName)+ ' is not valid';
            element.style.backgroundColor='#E3F0FF';
            
        } else if(errno==4) {//numeric fields
            missinginfo += '\n - '+ capitalize(fieldName)+ ' is a numeric field';
            element.style.backgroundColor='#E3F0FF';
            
        } else if(errno==5) {//validate date
            missinginfo += '\n - '+ capitalize(fieldName)+ ' is not a valid date format(dd/mm/YYYY)';
            element.style.backgroundColor='#E3F0FF';
        }
    }
    if (missinginfo != '') {
        missinginfo = '_____________________________\n' + 'Please fill in correctly the following information:\n' +
        missinginfo + '\n_____________________________';
        alert(missinginfo);
        return false;
    } else {
        return true;
    }
    return false;
}

/*C O N V E R T   F I R S T   L E T T E R   T O    U P P E R C A S E*/
function capitalize(str) {
  var arr=str.split("");
  arr[0]=arr[0].toUpperCase();
  var s=arr.join("");
  return s;
}

/*C H E C K   I F   A   F I E L D   I S   N U M E R I C*/
function IsNumeric(sText){
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
   return IsNumber;

}
function ajax(fragment_url,element_id) {

    var element = document.getElementById(element_id);

    document.body.style.cursor = "progress";
    
    element.innerHTML = '<font size=1>Loading...</font>';
    
    if (window.XMLHttpRequest) { //for all browers but IE
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { //for IE
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    }
    
    xmlhttp.open("GET", fragment_url+ '&r='+Math.random());
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.body.style.cursor = "auto";
            element.innerHTML = (unescape(xmlhttp.responseText)).replace(/\+/gi," ");
        }
    }
    xmlhttp.send(null);
}

