function MM_reloadPage(init) 
{  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);

function validate_passwords(theForm){
	var pass = theForm.pass.value.trim();
	var rpass = theForm.rpass.value.trim();
	var apass = theForm.apass.value.trim();
        if ( !alphanumericString( pass ) )
        {
                alert( "Genel kullanım şifresi sadece alfanümerik karakterler içerebilir" );
                return;
        }
        if ( !alphanumericString( rpass ) )
        {
                alert( "Salt okunur şifresi sadece alfanümerik karakterler içerebilir" );
                return;
        }
        if ( !alphanumericString( apass ) )
        {
                alert( "Yönetici şifresi sadece alfanümerik karakterler içerebilir" );
                return;
        }
        
        // check to make sure all passwords are unique
        //if ( $pass == $rpass || $pass == $apass || $rpass == $apass )
        if ( pass == rpass || pass == apass || rpass == apass )
        {
                alert( "Bütün şifreler değişik olmak zorunda." );
                return;
        }
	
	theForm.submit();
}

function changeBGC(targetID,bgcolor){
        if (document.getElementById) {
                document.getElementById(targetID).style.backgroundColor = bgcolor;
        }
        
        else if(document.all){
                document.all(targetID).style.backgroundColor = bgcolor;
        }
        else if(document.layers){
                document.layers[targetID].bgColor=bgcolor;
        }
}

String.prototype.trim = function()
{
  var x=this;

  x=x.replace( /^\s*/, "" );
  x=x.replace( /\s*$/, "" );

  return x;
}

function alphanumericString( checkStr )
{
        var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        var allValid = true;
        
        for ( var i = 0; i < checkStr.length; i++ )
        {
                ch = checkStr.charAt(i);
                for ( var j = 0;  j < checkOK.length;  j++ )
                        if ( ch == checkOK.charAt(j) )
                                break;
                                
                if ( j == checkOK.length )
                {
                        allValid = false;
                        break;
                }
        }
        return allValid;
}


function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} 

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
   return "";
}

/** Same as validate but for marriages only (no gender check). */
function validateMarriage( theForm )
{
        // combine date fields
        for( i = 0; i < theForm.elements.length; i++ )
        {
                if ( theForm.elements[i].name.indexOf( "_date_1" ) > 0 )
                {
                        var month = theForm.elements[i];
                        var day = theForm.elements[i + 1];
                        var year = theForm.elements[i + 2];
                        
                        theForm.elements[i + 3].value = year.value + "-" + month.value + "-" + day.value;
                        
                        var test = theForm.elements[i + 3];
                        
                        if ( test.value == "-0-0" || test.value == "0-0-0" )
                                test.value = "";
                        
                        month.value = "";
                        day.value = "";
                        year.value = "";
                        
                        i = i + 3;
                }
        }
        document.theForm.submitForm.value = "1";
        document.theForm.submit();
        return true;
}

function validateSpouseSelection( myForm )
{
        // myForm.ID = spouse selection from drop-list
        if ( myForm.ID.selectedIndex == 0 ) {
                alert( "Lütfen listeden kişi seçin." );
        }
        else {
                myForm.submitForm.value=1;
                myForm.submit();
        }
}

function validateLogin( theForm )
{
        var newUsername = theForm.newUsername.value.trim();
        var newPassword = theForm.newPassword.value.trim();
        var readOnlyPassword = theForm.readOnlyPassword.value.trim();
        var adminPassword = theForm.adminPassword.value.trim();
        var email = theForm.email.value.trim();
        var aname = theForm.aname.value.trim();
        var dname = theForm.dname.value.trim();
        
        // modify the form contents to reflect the trim
        theForm.newUsername.value = newUsername;
        theForm.newPassword.value = newPassword;
        theForm.readOnlyPassword.value = readOnlyPassword;
        theForm.adminPassword.value = adminPassword;
        theForm.email.value = email;
        theForm.aname.value = aname;
        theForm.dname.value = dname;

        // check to make sure login passwords contain no special characters, etc.
        if ( newUsername == "" )
        {
                alert( "Lütfen bir kullanıcı adı belirtin." );
                return;
        }        
        if ( newPassword == "" )
        {
                alert( "Lütfen genel kullanım şifresi belirtin." );
                return;
        }
        if ( readOnlyPassword == "" )
        {
                alert( "Lütfen salt okunur şifresi belirtin." );
                return;
        }
        if ( adminPassword == "" )
        {
                alert( "Lütfen yönetici şifresi belirtin." );
                return;
        }

        if ( aname == "" )
        {
                alert( "Lütfen yönetici adı belirtin." );
                return;
        }

        if ( dname == "" )
        {
                alert( "Lütfen soy ağacını tanımlayın." );
                return;
        }
        if ( email == "" )
        {
                alert( "Lütfen yönetici e-posta adresi belirtin." );
                return;
        }
        // check for non-alphanumeric characters
        if ( !alphanumericString( newUsername) )
        {
                alert( "Kullanıcı adı sadece alfanümerik karakterler içerebilir" );
                return;
        }        
        if ( !alphanumericString( newPassword ) )
        {
                alert( "Genel kullanım şifresi sadece alfanümerik karakterler içerebilir" );
                return;
        }
        if ( !alphanumericString( readOnlyPassword ) )
        {
                alert( "Salt okunur şifresi sadece alfanümerik karakterler içerebilir" );
                return;
        }
        if ( !alphanumericString( adminPassword ) )
        {
                alert( "Yönetici şifresi sadece alfanümerik karakterler içerebilir" );
                return;
        }
        
        // check to make sure all passwords are unique
        //if ( $newPassword == $readOnlyPassword || $adminPassword == $readOnlyPassword || $newPassword == $adminPassword )
        if ( newPassword == readOnlyPassword || adminPassword == readOnlyPassword || newPassword == adminPassword )
        {
                alert( "Bütün şifreler değişik olmak zorunda." );
                return;
        }
        
        theForm.newTree.value = 1;
        theForm.submit();
}

function validate( theForm )
{
        // combine date fields
        for( i = 0; i < theForm.elements.length; i++ )
        {
                if ( theForm.elements[i].name.indexOf( "_date_1" ) > 0 && theForm.elements[i+3].value.length == 0)
                {
                        var month = theForm.elements[i];
                        var day = theForm.elements[i + 1];
                        var year = theForm.elements[i + 2];
                        
                        theForm.elements[i + 3].value = year.value + "-" + month.value + "-" + day.value;
                        
                        var test = theForm.elements[i + 3];
                        
                        // skip empty or invalid dates
                        if ( test.value == "-0-0" || test.value == "0-0-0" || test.value == "--" )
                                test.value = "";
                        
                        month.value = "";
                        day.value = "";
                        year.value = "";
                        
                        i = i + 3;
                }
        }
        
        var name = theForm.name.value.trim();
        var gender = getSelectedRadioValue( theForm.sex );
        var surn = theForm.surn.value.trim();
        
        // if gender is a hidden field, get the value
        
        if ( !gender || gender == "undefined" )        
                gender = theForm.sex.value;
        
        if ( name == "" ) {
                alert( "Geçerli bir isim belirtmelisiniz (ilk & orta)." );
                return false;
        }
        else if ( surn == "" ) {
                alert( "Geçerli bir soyadı belirtmelisiniz." );
                return false;
        }
        else if ( gender == "" || !gender ) {
                alert( "Cinsiyet belirtmelisiniz." );
                return false;
        }
        else {
                document.theForm.submitForm.value = "1";
                document.theForm.submit();
                return true;
        }
}

function launchCentered( url, width, height ) {
  launchCentered( url, width, height, '' );
}
 
function launchCentered( url, width, height, options) {
  var left = ( screen.width - width ) / 2;
  var top = ( screen.height - height ) / 2;
  var options = "top="+top+",left="+left+",width="+width+",height="+height+","+options;
  launch( url, options );
}
 
function launch( url, params ) {
  self.name = 'opener';
  var remote = window.open( url, 'remote', params );
}

String.prototype.toTitleCase = function() {
  var words = this.split(" ");
  for(var i = 0; i < words.length; ++i)
    words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1).toLowerCase();
  return words.join(" ");
}
