﻿function settleValidators() {
   settleValidator("username","usernameRV");
   settleValidator("username","usernameV");
   settleValidator("email", "emailV");
   settleValidator("email", "emailAV");
   settleValidator("password","passwordV");
   settleValidator("passwordConfirm","passwordConfirmV");
   settleValidator("name","nameV");
   settleValidator("surname","surnameV");
   settleValidator("years","yearV");
   settleValidator("cities", "cityV");
   settleValidator("occupation", "occupationRV");
   settleValidator("invitationCode", "invitationV");
   settleValidator("invitationCode", "codeV");

   settleValidator("facebook", "facebookV");
   settleValidator("twitter", "twitterV");
   settleValidator("flickr", "flickrV");
   settleValidator("digg", "diggV");
   settleValidator("delicious", "deliciousV");
   settleValidator("lastfm", "lastfmV");
   settleValidator("friendfeed", "friendfeedV");
   settleValidator("stumbleupon", "stumbleV");
   settleValidator("myspace", "myspaceV");
}
function settleValidatorsForUpdate() {
   settleValidator("username","usernameRV");
   settleValidator("username","usernameV");
   settleValidator("email","emailV");
   settleValidator("password","passwordV");
   settleValidator("passwordConfirm","passwordConfirmV");
   settleValidator("name","nameV");
   settleValidator("surname","surnameV");
   settleValidator("years","yearV");
   settleValidator("cities","cityV");
   settleValidator("oldPassword","oldPasswordV");
   settleValidator("oldPassword","oldPasswordRV");

   settleValidator("facebook", "facebookV");
   settleValidator("twitter", "twitterV");
   settleValidator("flickr", "flickrV");
   settleValidator("digg", "diggV");
   settleValidator("delicious", "deliciousV");
   settleValidator("lastfm", "lastfmV");
   settleValidator("friendfeed", "friendfeedV");
   settleValidator("stumbleupon", "stumbleV");
   settleValidator("myspace", "myspaceV");
}
function settleValidator(id, vid) {
   var el = document.getElementById(id);
   var elv = document.getElementById(vid);
   if (el && elv) {
      var p = findPos(el);
      elv.style.left = (el.offsetWidth) + p[0] + "px";
      elv.style.top = p[1] + "px";
  }
}


function validateInvitation(src, args) {
    args.IsValid = args.Value.trim().length == 11;
}
function validateName(src, args) {
   args.IsValid = args.Value.trim().length > 0;
}
function validateSurname(src, args) {
   args.IsValid = args.Value.trim().length > 0;
}
function validatePassword(src, args) {
   args.IsValid = args.Value.length > 5;
}
function validatePasswordConfirm(src, args) {
   var pass = document.getElementById("password");
   args.IsValid = pass != null && args.Value == pass.value;
}
function validateOldPassword(src, args) {
   var pass = document.getElementById("password");
   if (pass == null || pass.value.length == 0) {
      args.IsValid = true;
      return;
   }
   args.IsValid = args.Value.length > 0;
}
function validatePasswordForUpdate(src, args) {
   var pass = document.getElementById("oldPassword");
   if (pass == null || pass.value.length == 0) {
      args.IsValid = args.Value.length == 0 || args.Value.length > 5;
      return;
   }
   args.IsValid = args.Value.length > 5;
}
function validatePasswordConfirmForUpdate(src, args) {
   var pass = document.getElementById("password");
   args.IsValid = pass != null && args.Value == pass.value;
}

function validateCity(src, args) {
   var cities = document.getElementById("cities");
   args.IsValid = cities != null && cities.selectedIndex > 0;}

function validateMusicType(src, args) {
    var mt = document.getElementById("musicType");
    args.IsValid = mt != null && mt.selectedIndex > 0;
}

function validateEmails(src, args) {
    var emails = args.Value.split("\n");
    if (emails != null && emails.length == 0) {
        args.IsValid = false;
        return;
    }
    for (var i = 0; i != emails.length; i++) {
        var email = emails[i].trim();
        if (email.length == 0) continue;
        var a = { Value: email, IsValid:false }
        validateEmail(src, a);
        if (!a.IsValid) {
            args.IsValid = false;
            return;
        }
    }
    args.IsValid = true;
}

function validateEmail(src, args) {
   var str = args.Value;
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) { args.IsValid = false; return; }
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) { args.IsValid = false; return; }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) { args.IsValid = false; return; }
   if (str.indexOf(at,(lat+1))!=-1) { args.IsValid = false; return; }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { args.IsValid = false; return; }
   if (str.indexOf(dot,(lat+2))==-1) { args.IsValid = false; return; }
   if (str.indexOf(" ")!=-1) { args.IsValid = false; return; }

   args.IsValid = true;				
}

function validateBirthday(src, args) {
   var days = document.getElementById("days");
   var months = document.getElementById("months");
   var years = document.getElementById("years");
   if (!days || !months || !years) {
      args.IsValid = false;
      return;
   }
   if (days.selectedIndex <= 0 || months.selectedIndex <= 0 || years.selectedIndex <= 0) {
      args.IsValid = false;
      return;
   }
   var day = days.selectedIndex;
   var month = months.selectedIndex;
   var year = years.selectedValue;
   var daysInMonth = DaysArray(12);
   if (month == 2 && day > daysInFebruary(year) || day > daysInMonth[month]) {
      args.IsValid = false;
      return;
   }
   args.IsValid = true;
}

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;
}
var fam = "facebook üye adresi";
var twm = "twitter profil adresi yada kullanıcı adı";
var flm = "profil adresi yada üye no (Örn:67424@N00)";
var dim = "digg profil adresi yada kullanıcı adı";
var dem = "delicious profil adresi yada kullanıcı adı";
var lam = "lastfm profil adresi yada kullanıcı adı";
var frm = "friendfeed profil adresi yada kullanıcı adı";
var stm = "stumbleupon adresi yada kullanıcı adı";
var mym = "myspace profil adresi yada kullanıcı adı";

var fa, tw, fl, di, de, la, fr, st, my;
function init() {
    fa = document.getElementById("facebook");
    tw = document.getElementById("twitter");
    fl = document.getElementById("flickr");
    di = document.getElementById("digg");
    de = document.getElementById("delicious");
    la = document.getElementById("lastfm");
    fr = document.getElementById("friendfeed");
    st = document.getElementById("stumbleupon");
    my = document.getElementById("myspace");
}

function checkSiteTexts() {
    if (fa.value == "" || fa.value == fam) {
        fa.value = fam;
        fa.className = "g it";
    }
    if (tw.value == "" || tw.value == twm) {
        tw.value = twm;
        tw.className = "g it";
    }
    if (fl.value == "" || fl.value == flm) {
        fl.value = flm;
        fl.className = "g it";
    }
    if (di.value == "" || di.value == dim) {
        di.value = dim;
        di.className = "g it";
    }
    if (de.value == "" || de.value == dem) {
        de.value = dem;
        de.className = "g it";
    }
    if (la.value == "" || la.value == lam) {
        la.value = lam;
        la.className = "g it";
    }
    if (fr.value == "" || fr.value == frm) {
        fr.value = frm;
        fr.className = "g it";
    }
    if (st.value == "" || st.value == stm) {
        st.value = stm;
        st.className = "g it";
    }
    if (my.value == "" || my.value == mym) {
        my.value = mym;
        my.className = "g it";
    }
}

function beforeSubmit() {
    if (!document.getElementById("facebook"))
        return true;
    if (fa.value == fam)
        fa.value = "";
    if (tw.value == twm)
        tw.value = "";
    if (fl.value == flm)
        fl.value = "";
    if (di.value == dim)
        di.value = "";
    if (de.value == dem)
        de.value = "";
    if (la.value == lam)
        la.value = "";
    if (fr.value == frm)
        fr.value = "";
    if (st.value == stm)
        st.value = "";
    if (my.value == mym)
        my.value = "";
        
    return true;
}
function getElText(el) {
    if (el == fa) return fam;
    if (el == tw) return twm;
    if (el == fl) return flm;
    if (el == di) return dim;
    if (el == de) return dem;
    if (el == la) return lam;
    if (el == fr) return frm;
    if (el == st) return stm;
    if (el == my) return mym;
    
}
function onFocus(el) {
    var t = getElText(el);
    if (!t) return;
    if (el.value == t)
        el.value = "";
    el.className = "b it";
}
function onBlur(el) {
    if (el.value.length == 0) {
        el.className = "g it";
        var t = getElText(el)
        el.value = t;
    }
}
function validateMsg(src, args) {
    args.IsValid = args.Value.trim().length > 5;
}
function validateSelect(src, args) {
    args.IsValid = args.Value.length() > 0
}

var tabs = null;
function initTabs() {
    tabs = [
        { tab: document.getElementById('tabPersonalInfo'), activeHtml: 'Üyelik Bilgilerin', passiveHtml: '<a href="javascript:openTab(0)">Üyelik Bilgilerin</a>', panel: document.getElementById('personalInfo') },
        { tab: document.getElementById('tabLink'), activeHtml: 'Linklerin', passiveHtml: '<a href="javascript:openTab(1)">Linklerin</a>', panel: document.getElementById('links') },
        { tab: document.getElementById('tabFavori'), activeHtml: 'Favorilerin', passiveHtml: '<a href="javascript:openTab(2)">Favorilerin</a>', panel: document.getElementById('favorites') },
        { tab: document.getElementById('tabAvatar'), activeHtml: 'Avatarın', passiveHtml: '<a href="javascript:openTab(3)">Avatarın</a>', panel: document.getElementById('avatar') },
        { tab: document.getElementById('tabTema'), activeHtml: 'Tema', passiveHtml: '<a href="javascript:openTab(4)">Tema</a>', panel: document.getElementById('themes') }
    ];
}
function openTab(index) {
    if (tabs == null) initTabs();
    var i = 0;
    for (i = 0; i < tabs.length; i++) {
        var tab = tabs[i];
        if (i == index) {
            tab.panel.style.display = 'block';
            tab.tab.innerHTML = tab.activeHtml;
            tab.tab.className = "r a";
        }
        else {
            tab.panel.style.display = 'none';
            tab.tab.innerHTML = tab.passiveHtml;
            tab.tab.className = "r d";
        }
    }
}
function rcheck(el) {
    el.checked = true;
    el.onclick = function() {
        runcheck(el);
    }
}
function runcheck(el) {
    el.checked = false;
    el.onclick = function() {
        rcheck(el);
    }
}
function validateUsernameOrEmail(src, args) {
    var username = document.getElementById("username");
    var email = document.getElementById("email");

    if (username != null) {
        if (username.trim().length() > 0)
            args.IsValid = true;
        else if (email != null)
            args.IsValid = email.trim().length() > 0;
        else
            args.IsValid = false;
    }
    else if (email != null)
        args.IsValid = email.trim().length() > 0;
    else args.IsValid = false;
}