/*Response.Write("<script language=\"javascript\">parent.window.object.Add */
function Sign()
{

    
    this.Signs = new Array();
    this.SignIndex = -1;
	this.TableName = new String("");
	
	this.Name = new String("");
	this.SortType = new String("");
	
	this.ActiveSign = new String("");

	this.myName = new String("");
	
	
	this.Edit = false;
	this.Delete = false;
	this.AddNew = false;
	//this.People = new Person();
	
	
	
    this.Add  = CSign_Add;
    this.GetSign = CSign_GetPeople;
    this.SelectSign = CSign_SelectSign;
    this.FindSign   = CSign_Find;
    this.ExistsSign = CSign_Exists;
    this.GenerateTable = CSign_ShowPeople;
    this.Sorting = CSign_SortSignPeople;
}

function CSigns(signName,sortType)
{
	this.Name = new String(signName);
	this.SortType = new String(sortType);
}


function CSign_Add(signName)
{
    
    var getID = this.FindSign(signName,true);
    
    if(getID == null)
    
    {
	    var newSign = new CSigns(signName,"");
	    this.Signs.push(newSign);
	    getID = this.Signs.length-1;
	    var person = new Person();
	    this.Signs[getID].People = person;
	}
	return getID;
    
}

function CSign_Exists(signName)
{
    return (null != this.FindSign(signName,false));
}

function CSign_Find(signName, isReturnIndex)
{
    
    for (var Index = 0; Index < this.Signs.length ; Index++)
    {
        if (this.Signs[Index].Name == signName)
        {
            if (isReturnIndex)
            {
                return Index;
            }
            else
            {
                return this.Signs[Index];
            }
        }
    }
    return null;
}

function CSign_GetPeople(sign)
{
    var HTML = "";
	try
	{	
		this.ActiveSign  = sign;
		this.GenerateTable(this.SelectSign(sign));
	}
	catch(x)
	{
	    if (this.AddNew)
	    {
	    HTML  = "<TABLE ALIGN=CENTER CLASS=\"BlackBorderedTable\" CELLSPACING=\"0\" CELLPADDING=\"3\" border=\"0\"><TR><TD class=\"BlackBorderedColHeader\">" +
	            "<center><a href ='#' ONCLICK=\"action('add',0);\">Yeni Ekle</a></center></TD></TR></TABLE>"
	    }
	    document.getElementById(this.TableName).innerHTML = HTML;
	};
    
}

function CSign_ShowPeople(selectedSign)
{
	var peopleObject = selectedSign.People;
	
	var editField = "";
	var colSize = 2;
	//var tmp = "<TD WIDTH=75 class=\"BlackBorderedColHeader\">Ad</TD><TD WIDTH=75 class=\"BlackBorderedColHeader\">Soyad</TD><TD WIDTH=75 ALIGN=RIGHT class=\"BlackBorderedColHeader\">Doğum Tarihi</TD>";
	var tmp = "<TD WIDTH=75 class=\"BlackBorderedColHeader\">Ad Soyad</TD><TD WIDTH=75 ALIGN=RIGHT class=\"BlackBorderedColHeader\">Doğum Tarihi</TD>";
	if (this.Edit)
	{
        editField = "<TD ALIGN=CENTER class=\"BlackBorderedCol\"><a href=\"#\" ONCLICK=\"action('update',||ID||)\">Güncelle</A></TD>"
        tmp += "<TD WIDTH=50 class=\"BlackBorderedColHeader\"></TD>";
	    colSize ++;
    }
    
    var deleteField = "";
	if (this.Delete)
	{
        deleteField = "<TD ALIGN=CENTER class=\"BlackBorderedCol\"><a href=\"#\" ONCLICK=\"action('delete',||ID||)\">Sil</A></TD>"
        tmp += "<TD WIDTH=50 class=\"BlackBorderedColHeader\"></TD>";
	    colSize ++;
    }
	
	var header = "<TABLE CLASS=\"BlackBorderedTable\" CELLSPACING=\"0\" CELLPADDING=\"3\" border=\"0\">" ;
	
	var ef = ""; var df="";
	
	
	if (peopleObject.Person.length==0) {return false;}

	var halfLength = (peopleObject.Person.length/2);
	var isAdded = false;
	
	HTML = "<TABLE CLASS=\"InvisibleTable\" CELLSPACING=\"1\" CELLPADDING=\"2\" border=\"0\" align=\"center\" HEIGHT=\"300\">" +
	        "<TR><TD valign=\"TOP\">";
	HTML += header + "<TR WIDTH=225 >" + tmp + "</TR>";
	
	for (Index = 0; Index < peopleObject.Person.length ; Index++)
	{
		
		if (editField.length>0) {ef = editField.replace("||ID||",peopleObject.Person[Index].ID);}
		if (deleteField.length>0) {df = deleteField.replace("||ID||",peopleObject.Person[Index].ID);}
		if ((Index >= halfLength ) && !isAdded)
		{
			HTML += "</TABLE></TD><TD valign=\"TOP\" >" + header + "<TR WIDTH=225>" + tmp + "</TR>";
			isAdded = true;
		}
		HTML += "<TR><TD class=\"BlackBorderedCol\">" + peopleObject.Person[Index].Name + " " +
		        " " + peopleObject.Person[Index].LastName + "</TD>"
        if ((peopleObject.Person[Index].BirthDay)+(peopleObject.Person[Index].BirthMonth)+(peopleObject.Person[Index].BirthYear) == 0)
        {
            HTML += "<TD class=\"BlackBorderedCol\" ALIGN=RIGHT>Bilinmiyor</TD>"
        }
        else
        {
            HTML += "<TD class=\"BlackBorderedCol\" ALIGN=RIGHT>" + peopleObject.Person[Index].BirthDay + "." + peopleObject.Person[Index].BirthMonth + "." + peopleObject.Person[Index].BirthYear + "</TD>"
        }
        HTML += ef + df + "</TR>" ;
	}

	
	HTML += "</TABLE></TD></TR>"
	if (this.AddNew)
	{
	    HTML += "<TR HEIGHT=30><TD align=right VALIGN=BOTTOM Colspan=" + (isAdded ? 2:1) + ">" + header + "<TR><TD WIDTH=130 ALIGN=\"CENTER\" class=\"BlackBorderedColHeader\">" +
	            "<a href ='#' ONCLICK=\"action('add',0);\">Yeni Ekle</a></TD></TR></TABLE></TD></TR>"
	 }
	
	HTML += "<TR HEIGHT=30><TD align=right VALIGN=BOTTOM Colspan=" + (isAdded ? 2:1) + ">" + header + "<TR><TD WIDTH=130 ALIGN=\"CENTER\" class=\"BlackBorderedColHeader\">" +
	        "<a href ='#' onclick=" + this.myName + ".Sorting(" + this.myName + ".ActiveSign);>"
	HTML += ((selectedSign.SortType == "byName")?" İsme Göre Sırala":"Tarihe Göre Sırala" );
	HTML += "</a></TD></TR></TABLE></TD></TR></TABLE>";
	
	
	document.getElementById(this.TableName).innerHTML = HTML;
	

}

function CSign_SelectSign(sign)        
{
    
    return this.FindSign(sign,false);
    
    
}


function CSign_SortSignPeople(sign)        
{
    
    var signs = this.FindSign(sign,false);
    
    signs.SortType = signs.People.Sort(signs.SortType);
    
    this.GetSign(sign);
    
    
}


function Person()
{
    //properties
    this.Person    = new Array();
    this.PersonIndex = new Array();
    this.ID         = 0;
    this.Name      = new String("");
    this.LastName  = new String("");
    this.BirthDay  = 0;
    this.BirthMonth= 0;
    this.BirthYear = 0;
    
    this.Const_SortByDate = "byDate";
	this.Const_SortByName = "byName";
    
    //methods
    this.Add        = CPerson_Add;
    this.Sort       = CPerson_Sort;
    this.Exists     = CPerson_Exists;
    this.SortByName = CPerson_SortByName;
    this.SortByDate = CPerson_SortByDate;
}

function CPerson(id, name, lastname, birthDay, birthMonth, birthYear)
{
    this.ID         = (id);
    this.Name      = new String(name);
    this.LastName  = new String(lastname);
    this.BirthDay  = (birthDay);
    this.BirthMonth= (birthMonth);
    this.BirthYear = (birthYear);

}

function CPerson_Add(id, name, lastName, bDay, bMonth, bYear)
{
    if (this.Exists(name,lastName)==null)
    {
        var newPerson = new CPerson(id, name, lastName, bDay, bMonth, bYear);
        this.PersonIndex.push (name + lastName);
        this.Person.push (newPerson);
        //this.Sort("byName");
    }
    
}

function CPerson_Exists(name,lastName)
{
    for (var Index = 0 ; Index < this.PersonIndex.length ; Index++)
    {
        if (this.PersonIndex[Index] == name + lastName )
        {
            return Index;
        }
    }
    return null;
}


function CPerson_Sort(state)
{

	switch (state)
    {
 
        case this.Const_SortByName :
        this.SortByName();
        return this.Const_SortByDate;
        break;
        
        default:
        this.SortByDate();
        return this.Const_SortByName;
        break;
    }
}

function CPerson_SortByName()
{
    
    var tmpArray = new Array();
    
    for (var Index = 0 ; Index<this.Person.length;Index++)
    {
        tmpArray.push (this.Person[Index].Name + "|" + this.Person[Index].LastName + "|" + this.Person[Index].BirthDay + "|" + this.Person[Index].BirthMonth + "|" + this.Person[Index].BirthYear + "|" + this.Person[Index].ID )

    }
    
    tmpArray.sort();
    
    for (Index = 0 ; Index<this.Person.length ; Index++)
    {
    
        tmpConcatArray = tmpArray[Index].split("|")
        this.Person[Index] = new CPerson(tmpConcatArray[5], tmpConcatArray[0], tmpConcatArray[1], tmpConcatArray[2], tmpConcatArray[3], tmpConcatArray[4])
        
    }
    tmpArray = null;
}

function CPerson_SortByDate()
{
    
    var tmpArray = new Array();
    
    for (var Index = 0 ; Index<this.Person.length;Index++)
    {
        tmpArray.push (this.Person[Index].BirthMonth + "|" + this.Person[Index].BirthDay + "|" + this.Person[Index].Name + "|" + this.Person[Index].LastName + "|" + this.Person[Index].ID + "|" + this.Person[Index].BirthYear)
    }
    
    tmpArray.sort();
    
    for (Index = 0 ; Index<this.Person.length ; Index++)
    {
    
        tmpConcatArray = tmpArray[Index].split("|")
        this.Person[Index] = new CPerson(tmpConcatArray[4], tmpConcatArray[2], tmpConcatArray[3], tmpConcatArray[1], tmpConcatArray[0], tmpConcatArray[5])
        
    }
    tmpArray = null;
}