//
function $()
{
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
	var element = arguments[i];
	if (typeof element == 'string')
	element = document.getElementById(element);
        if (arguments.length == 1)
	return element;
	elements.push(element);
	}		
    return elements;
}

function addEvent(obj, evType, fn){
  //login button 
    if((obj!= $("login_btn"))  && (obj!= $("wanttoregister")) && (obj!= $("agree")) && (obj!= $("news"))&& (obj!= $("submit"))){
     if (obj.addEventListener){
        obj.addEventListener(evType, fn, true);
        return true;
     } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
     } else {
        return false;
     }
 } 
} 
addEvent(window, 'load', function() {
 var input, textarea,selectt;
 var inputs = document.getElementsByTagName('input');
 for (var i = 0; (input = inputs[i]); i++) {
   addEvent(input, 'focus', oninputfocus);
   addEvent(input, 'blur', oninputblur);
 }
 var textareas = document.getElementsByTagName('textarea');
 for (var i = 0; (textarea = textareas[i]); i++) {
   addEvent(textarea, 'focus', oninputfocus);
   addEvent(textarea, 'blur', oninputblur);
 }
 var selects = document.getElementsByTagName('select');
 for (var i = 0; (selectt = selects[i]); i++) {
   addEvent(selectt, 'focus', oninputfocus);
   addEvent(selectt, 'blur', oninputblur);
 }
});
function oninputfocus(e) {
 /* Cookie-cutter code to find the source of the event */
 if (typeof e == 'undefined') {
   var e = window.event;
 }
 var source;
 if (typeof e.target != 'undefined') {
    source = e.target;
 } else if (typeof e.srcElement != 'undefined') {
    source = e.srcElement;
 } else {
   return;
 }
 /* End cookie-cutter code */
// source.style.border='1px solid #000';
 source.style.background='#FFFFE6';
}
function oninputblur(e) {
 /* Cookie-cutter code to find the source of the event */
 if (typeof e == 'undefined') {
   var e = window.event;
 }
 var source;
 if (typeof e.target != 'undefined') {
    source = e.target;
 } else if (typeof e.srcElement != 'undefined') {
    source = e.srcElement;
 } else {
   return;
 }
 /* End cookie-cutter code */
 //source.style.border='1px solid #423724';
 //alert(source.id);
 if ((source.id== "loginForm_username")  || (source.id== "loginForm_password"))
 {
   source.style.background="#FFCC66";
 }
 else
 {
 source.style.background="#fff";
  }


 }
 
 //--------------------------------------------------------------//
 
 var pagePosition = "";
  
  function showLogin()
  {
    if (document.getElementById("loginScreen").style.display == 'none')
      {
        document.getElementById("loginLink").style.background = '#CCCCCC';
        document.getElementById("loginScreen").style.display = '';
        $("forgottenLoginLink").style.display='';
      }
      else
      {
        //hideLogin()
        /*document.getElementById("loginLink").style.background = '#CCCCCC';
                  document.getElementById("loginScreen").style.display = '';*/
      }
  }

 function hideLogin()
 {
    document.getElementById("loginScreen").style.display = 'none';
    document.getElementById("loginLink").style.background = '';
    document.getElementById("loginFieldsError").innerHTML = '';
    $("forgottenLoginLink").style.display='none';  
 } 

  function login()
  {
      if(document.getElementById("loginForm_username").value.length == 0)
      {
          document.getElementById("loginFieldsError").innerHTML = login_field_error;
          document.getElementById("loginForm_username").focus();
          return;
      }

      if(document.getElementById("loginForm_password").value.length == 0)
      {
          document.getElementById("loginFieldsError").innerHTML = login_field_error;
          document.getElementById("loginForm_password").focus();
          return;        
      } 
      
      var copyType = document.getElementById("loginForm_type").value;
      var copyId = document.getElementById("loginForm_id").value;
      
      if((copyType.length>0)&&(copyId.length>0))
      {    
        document.getElementById("loginForm_type").disabled = false;
        document.getElementById("loginForm_id").disabled = false;
      }

      document.getElementById("loginForm").submit();
  } 

 function changeLang(newLang)
 {
     document.getElementById("changePageForm_lang").value = newLang;
     document.getElementById("changePageForm_lang").disabled = false;  
     changePage('lang');
 }
 
 function logout()
 {
      var copyType = document.getElementById("changePageForm_type").value;
      var copyId = document.getElementById("changePageForm_id").value;
      
      if((copyType.length>0)&&(copyId.length>0))
      {    
        //useful for viewer >> same layer after logout  
        document.getElementById("changePageForm_type").disabled = false;
        document.getElementById("changePageForm_id").disabled = false;
      }     
     
     changePage('logout');
 }

 function changeToPagePosition(newPage, newAnchor)
 {
     pagePosition = newAnchor;
     changePage(newPage);
 }
 
 function changePage(newPage)
 {
      document.getElementById("changePageForm_page").value = newPage;
      submitChange();      
 }
 
 function submitChange()
 {
     if(pagePosition.length > 0)
     {
        document.getElementById("changePageForm").action = document.getElementById("changePageForm").action + "#" + pagePosition;
     }   
     
     document.getElementById("changePageForm").submit();
 }

function goToDetails(type, id)
{
    document.getElementById("changePageForm_type").value = type;
    document.getElementById("changePageForm_type").disabled = false;
    document.getElementById("changePageForm_id").value = id;
    document.getElementById("changePageForm_id").disabled = false;
    changePage('meta');
}

function goBackToOverview()
{
    pagePosition = document.getElementById("changePageForm_type").value + "_id_" + document.getElementById("changePageForm_id").value;
    changePage('services');
}

function goToPreview(type, id)
{
    document.getElementById("changePageForm_type").value = type;
    document.getElementById("changePageForm_type").disabled = false;
    document.getElementById("changePageForm_id").value = id;
    document.getElementById("changePageForm_id").disabled = false;
    changePage('viewer');
}

function showRegistrationFields(checkbox)
{
    if(checkbox.checked)
    {
        //document.getElementById("registrationFields").style.display = '';  
        $('username').disabled=false;
        $('password').disabled=false;
        //$('username').className="enabled";
        $('username').style.background="#FFFFFF";      
        //$('password').className="enabled";
        $('password').style.background="#FFFFFF";   
        
        $('news').disabled=false;
     //   $('news').style.background="#FFFFFF";          
        $('agree').disabled=false;
     //   $('agree').style.background="#FFFFFF";          
    }
    else
    {
        //document.getElementById("registrationFields").style.display = 'none';  
        $('username').disabled="disabled";
        $('password').disabled="disabled";
        //$('username').className="disabled";
        $('username').style.background="#C9CDCD";
        //$('password').className="disabled";
        $('password').style.background="#C9CDCD";
        
        $('news').disabled="disabled";
        //$('news').style.background="#C9CDCD";
        $('agree').disabled="disabled";
        //$('agree').style.background="#C9CDCD2";
        
    }
}

function showChangePasswordFields(checkbox)
{
    if(checkbox.checked)
    {
        //document.getElementById("changePasswordFields").style.display = ''; 
        $('password_old').disabled=false;
        $('password_new').disabled=false;
        $('password_old').style.background="#FFFFFF";
        $('password_new').style.background="#FFFFFF";
        
    }
    else
    {
        //document.getElementById("changePasswordFields").style.display = 'none'; 
        $('password_old').disabled="disabled";
        $('password_new').disabled="disabled";
        $('password_old').style.background="#C9CDCD";
        $('password_new').style.background="#C9CDCD";       
    }     
}

//---------------------------------------//
//contact forms

// Removes leading whitespaces
function LTrim( value )
{
 var re = /\s*((\S+\s*)*)/;
 return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value )
{
 var re = /((\s*\S+)*)\s*/;
 return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value )
{
 return LTrim(RTrim(value));
}

function verifyAlpha(AZ) {
    var CorrChar = new Array;
    var pos = 0;
    CorrChar[pos] = 10;
    pos++;
    CorrChar[pos] = 13;
    pos++;
    for (var l = 32; l <= 34; l++) {
        CorrChar[pos] = l;
        pos++;
    }
    for (var l = 38; l <= 125; l++) {
        CorrChar[pos] = l;
        pos++;
    }
    for (var l = 192; l < 255; l++) {
        CorrChar[pos] = l;
        pos++;
    }
    var validChar = false;
    if (AZ.length != 0) {
        for (var i = 0; i < AZ.length; i++) {
            validChar = false;
            for (var j = 0; j < CorrChar.length; j++) {
                if (AZ.charCodeAt(i) == CorrChar[j]) {
                    validChar = true;
                    break;
                }
            }
            if (!validChar) {
                AZ = AZ.replace(AZ.substring(i, i + 1), " ");
                i--;
            }
        }
    }
    return AZ;
}

function validateForm(loggedIn, contact_type) 
{
    setValidateRemark('');
    var last_name, first_name, street, number, postal_code, city, country, email, remarks, company_name, username, password, password_old, password_new;

    if(contact_type == 'feedback')
    {
      remarks = verifyAlpha( trim(document.contactForm.remarks.value) );
      document.contactForm.remarks.value = remarks;
       
      if(remarks != "") 
      {
          var isNav = navigator.appName.toLowerCase().indexOf("netscape") >= 0;
          var splitString = remarks.split("\n");
          remarks = "";
          
          for(var i = 0; i < splitString.length; i++) 
          {
              if(isNav) 
              {
                  remarks += splitString[i] + " \n";
              } 
              else 
              {
                  if(splitString[i].charAt(0) != " ") 
                  {
                      remarks += " " + splitString[i];
                  } 
                  else 
                  {
                      remarks += splitString[i];
                  }
              }
          }
          
          document.contactForm.remarks.value = remarks;
      } 
      else 
      {
          setValidateRemark(fill_in_remark);
          document.contactForm.remarks.focus();
          return false;
      }       
    }    
    
    if( (!loggedIn) || (loggedIn && (contact_type == 'profile')))
    { 
      if(contact_type != 'forgotten_login')
      {    
        last_name = verifyAlpha( trim(document.contactForm.last_name.value) );
        document.contactForm.last_name.value = last_name;

        if(last_name == "") 
        {
          setValidateRemark(fill_in_last_name);  
          document.contactForm.last_name.focus();
          return false;
        }  

        first_name = verifyAlpha( trim(document.contactForm.first_name.value) );
        document.contactForm.first_name.value = first_name;

        if(first_name == "") 
        {
            setValidateRemark(fill_in_first_name);
            document.contactForm.first_name.focus();
            return false;
        } 

        street = verifyAlpha( trim(document.contactForm.street.value) );
        document.contactForm.street.value = street;  

        number = verifyAlpha( trim(document.contactForm.number.value) );
        document.contactForm.number.value = number;


        postal_code = verifyAlpha( trim(document.contactForm.postal_code.value) );
        document.contactForm.postal_code.value = postal_code;

        city = verifyAlpha( trim(document.contactForm.city.value) );
        document.contactForm.city.value = city;

        country = verifyAlpha( trim(document.contactForm.country.value) );
        document.contactForm.country.value = country;

        if(country == "") 
        {
          setValidateRemark(fill_in_country);
          document.contactForm.country.focus();
          return false;
        } 

        company_name = verifyAlpha( trim(document.contactForm.company_name.value) );
        document.contactForm.company_name.value = company_name; 
      }
      
      email = verifyAlpha( trim(document.contactForm.email.value) );
      document.contactForm.email.value = email;
      
      if(email == "") 
      {
        setValidateRemark(fill_in_email);  
        document.contactForm.email.focus();
        return false;
      }
      else
      {
        if( email.indexOf("@") < 1 ||
            email.indexOf("@") > email.length - 4 ||
            email.indexOf("@") != email.lastIndexOf("@") ||
            email.indexOf(".") == 0 ||
            email.lastIndexOf(".") < email.indexOf("@") + 2 ||
            email.lastIndexOf(".") > email.length - 2) 
        {
            setValidateRemark(invalid_email);
            document.contactForm.email.focus();
            return false;
        }          
      }      
    }

    if( (!loggedIn)&&(contact_type != 'forgotten_login') ) 
    { 
      if((contact_type == 'feedback') && (!document.contactForm.wanttoregister.checked))
      {   
          document.contactForm.username.value = "";
          document.contactForm.password.value = "";
		  document.contactForm.news.checked = false;
      }
      else
      {   
        username = verifyAlpha(trim(document.contactForm.username.value));
        document.contactForm.username.value = username;
        
        if(username == '')
        {
            setValidateRemark(fill_in_username);
            document.contactForm.username.focus();
            return false;
        }    
        
        password = verifyAlpha(trim(document.contactForm.password.value));
        document.contactForm.password.value = password;
        
        if(password == '')
        {
            setValidateRemark(fill_in_password);
            document.contactForm.password.focus();
            return false;
        }
          
        if(!document.contactForm.agree.checked) 
        {
          setValidateRemark(check_agree);
          document.contactForm.agree.focus();
          return false;
        }
      }        
    }
    else
    {
        if(contact_type == 'profile')
        {
           if(document.contactForm.wanttochangepassword.checked) 
           {
               password_old = verifyAlpha(trim(document.contactForm.password_old.value));
               document.contactForm.password_old.value = password_old;
               
               if(password_old == '')
               {
                setValidateRemark(fill_in_old_password);   
                document.contactForm.password_old.focus();
                return false;
               }               
               
               password_new = verifyAlpha(trim(document.contactForm.password_new.value));
               document.contactForm.password_new.value = password_new;
               
               if(password_new == '')
               {
                setValidateRemark(fill_in_new_password);     
                document.contactForm.password_new.focus();
                return false;
               } 

               if(password_old == password_new)
               {
                 setValidateRemark(fill_in_other_password);     
                 document.contactForm.password_new.focus();
                 return false;  
               }    
           }
           else
           {
               document.contactForm.password_old.value = "";
               document.contactForm.password_new.value = "";
           }    
        }   
    }    

    return true;
}

function setValidateRemark(message)
{
    document.getElementById("validateRemark").innerHTML = message;
}

