﻿
//Global XMLHTTP Request object
var XmlHttp;
var slcSehirClientID;
var slcBelediyeClientID;
var slcPartiClientID;
var txtBaskanClientID;
var txtBaskanAdDefValue = "Başkan Adı İle Arayınız...";


function ModalAc(Sayfa,En,Boy) {
	window.showModalDialog(Sayfa, "", "dialogHeight:"+Boy+"px; dialogWidth:"+En+"px;help:no;scroll:yes");
}

 
 
function OnTxtBaskanKeyPress( evt, frm )
{
    var keyCode = null;
    if( evt.which )
    {
        keyCode = evt.which;
    }
    else if( evt.keyCode )
    {
        keyCode = evt.keyCode;
    }
    if( 13 == keyCode )
    {        
        OnBtnSubmitClicked();
        return false;
    }
    return true;
}


function SetSearchBoxClientIDs(inputSlcSehirClientID, inputSlcBelediyeClientID, inputSlcPartiClientID, inputTxtBaskanClientID)
{
    slcSehirClientID = inputSlcSehirClientID; 
    slcBelediyeClientID = inputSlcBelediyeClientID;
    slcPartiClientID = inputSlcPartiClientID;
    txtBaskanClientID = inputTxtBaskanClientID;
}


function OnTxtBaskanAdFocus()
{  
    var txtBaskan = document.getElementById(txtBaskanClientID);
    
    if(txtBaskan.value == txtBaskanAdDefValue)
    {
        txtBaskan.value = "";           
    }
}


function OnTxtBaskanAdBlur()
{
    var txtBaskan = document.getElementById(txtBaskanClientID);
    if(txtBaskan.value == "")
    {
        txtBaskan.value = txtBaskanAdDefValue;           
    }    
}




function OnSlcPartiChanged()
{
    RedirectIfSehirAndPartiBothSelected();    
}

function RedirectIfSehirAndPartiBothSelected()
{
    var sehirList = document.getElementById(slcSehirClientID);
	//Getting the selected sehir
	var selectedSehirID = sehirList.options[sehirList.selectedIndex].value;	
	
	if(selectedSehirID != '')
	{	  
	    var partiList = document.getElementById(slcPartiClientID);
	    //Getting the selected parti
	    var selectedPartiID = partiList.options[partiList.selectedIndex].value;
	    
	    if(selectedPartiID != '')
	    {
	        document.location = "/Secim2009/secimpartidetay.aspx?sehirid=" + selectedSehirID + "&" + "partiid=" + selectedPartiID;
	    }	    		
	}	
}

////////////////
/////////////// Sehir Belediye ajax
//////////////
//Creating and setting the instance of appropriate XMLHTTP Request object to 
//a "XmlHttp" variable  
function CreateXmlHttp()
{
    //Creating object of XMLHTTP in IE
    try
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch(oc)
        {
            XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari 
    if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
    {
        XmlHttp = new XMLHttpRequest();
    }
}

function GetYaziGrafik(id)
{
    var requestUrl = "/Secim2009/api/YaziKarsilastirma.ashx" + "?sehirid=" + id;
    CreateXmlHttp();
     
    // If browser supports XMLHTTPRequest object
    if(XmlHttp)
    {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleSlcYaziGrafikChangedResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl,  true);

        //Sends the request to server
        XmlHttp.send(null);  
    }
}

function HandleSlcYaziGrafikChangedResponse()
 {
    if(XmlHttp.readyState == 4)
     {
        // To make sure valid response is received from the server, 
        // 200 means response received is OK
        if(XmlHttp.status == 200)
        {  
            document.getElementById('chartDivGenel2004').innerHTML = XmlHttp.responseText;
        }
        else
        {
            alert("Data alımında bir sorun oluştu. Tekarar deneyiniz!" );
        }
     }
 }

function OnSlcSehirChanged()
{    
    RedirectIfSehirAndPartiBothSelected();   
    var sehirList = document.getElementById(slcSehirClientID);
	//Getting the selected sehir
	var selectedSehirID = sehirList.options[sehirList.selectedIndex].value;
	ClearBelediyeListItems();
	
	if(selectedSehirID == '')
	{	  
	    return;	
	}
	
    var belediyeList = document.getElementById(slcBelediyeClientID);
           
    var requestUrl = "/Secim2009/api/belediyelist.ashx" + "?sehirid=" + selectedSehirID;
    CreateXmlHttp();
     
    // If browser supports XMLHTTPRequest object
    if(XmlHttp)
    {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleSlcSehirChangedResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl,  true);

        //Sends the request to server
        XmlHttp.send(null);  
    }
 }
   
 function HandleSlcSehirChangedResponse()
 {
    if(XmlHttp.readyState == 4)
     {
        // To make sure valid response is received from the server, 
        // 200 means response received is OK
        if(XmlHttp.status == 200)
        {  
            ClearAndSetBelediyeListItems(XmlHttp.responseText);
        }
        else
        {
            alert("There was a problem retrieving data from the server." );
        }
     }
 }
 
 
 function ClearBelediyeListItems()
 {
    var belediyeList = document.getElementById(slcBelediyeClientID);
    
    //Clears the belediye combo box contents.
    for (var count = belediyeList.options.length-1; count >0; count--)
    {
        belediyeList.options[count] = null;
    }
 }
 
 function ClearAndSetBelediyeListItems(responseText)
 {
    var belediyeList = document.getElementById(slcBelediyeClientID);        
    var belediyeNodes = responseText.split(";");       
    var ValueAndText;
    var selectValue;
    var selectText;
    var optionItem;
    
    //Add new belediye list to the state combo box.
    for (var count = 0; count < belediyeNodes.length; count++)
    {
        ValueAndText = (belediyeNodes[count]).split("#");
        selectValue = ValueAndText[0];
        selectText = ValueAndText[1];
                
        optionItem = new Option(selectText, selectValue ,  false, false);        
        belediyeList.options[belediyeList.length] = optionItem;
    }
 }
 
//////////////
////////////// End - Sehir Belediye ajax
/////////////
 
 
function OnSlcBelediyeChanged()
{
    var belediyeList = document.getElementById(slcBelediyeClientID);
	//Getting the selected sehir
	var selectedBelediyeID = belediyeList.options[belediyeList.selectedIndex].value;
			
	
	if(selectedBelediyeID != '')
	{
	    var sehirList = document.getElementById(slcSehirClientID);
	    var selectedSehirID = sehirList.options[sehirList.selectedIndex].value;	      
	    document.location = "/Secim2009/secimildetay.aspx?belediyeid=" + selectedBelediyeID;
	}	
}

function OnBtnSubmitClicked()
{
    var txtBaskanAd = document.getElementById(txtBaskanClientID);
    //Secilen sehir
    var sehirList = document.getElementById(slcSehirClientID);
	var selectedSehirID = sehirList.options[sehirList.selectedIndex].value;
	
	var partiList = document.getElementById(slcPartiClientID);
	//Getting the selected sehir
	var selectedPartiID = partiList.options[partiList.selectedIndex].value;
    
    
    if (txtBaskanAd.value == txtBaskanAdDefValue || txtBaskanAd.value == "" )
    {        
        if(selectedSehirID != "")
        {
            document.location = "/Secim2009/secimildetay.aspx?sehirid=" + selectedSehirID;
            return;
        }
        
        if(selectedPartiID != "")
        {        
            document.location = "/Secim2009/secimpartidetay.aspx?partiid=" + selectedPartiID;
            return;                    
        }
        window.alert("Lütfen arama kriteri seçin.")               
    }
    else
    {
        //free text search        
        document.location="/Secim2009/baskanaramasonuc.aspx?baskanad=" + txtBaskanAd.value + "&sehirid=" + selectedSehirID + "&partiid=" +selectedPartiID;
    }
}

function OpenPopupWithOutReturn(pageLink,width,height,positionTop,positionLeft,parUseScrollBar) 
{
    popupWin=window.open(pageLink,'myNewWin','menubar=1,resizable=1,width='+width+',height='+height+',top='+positionTop+',left='+positionLeft+',scrollbars='+parUseScrollBar)
    }

function NewWindow(Sayfa,En,Boy) {
	window.open(Sayfa, 'Baslik', "height="+Boy+",width="+En+",top=200,left=200,status=yes,toolbar=no,menubar=no,location=no,scrollbars=no");
}
function ChangeCityDetail(obj)
{
    document.location = "/Secim2009/secimildetay.aspx?sehirid="+obj.options[obj.selectedIndex].value;
}

function GetIstatistik(id)
{
    var requestUrl = "/Secim2009/api/SehirBazliAcilanSandikOrani.ashx" + "?sehirid=" + id;
    CreateXmlHttp();
     
    // If browser supports XMLHTTPRequest object
    if(XmlHttp)
    {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleSlcIstatistikChangedResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl,  true);

        //Sends the request to server
        XmlHttp.send(null);  
    }

}

function HandleSlcIstatistikChangedResponse()
 {
    if(XmlHttp.readyState == 4)
     {
        // To make sure valid response is received from the server, 
        // 200 means response received is OK
        if(XmlHttp.status == 200)
        {  
            document.getElementById('sehirBazliIstatistik').innerHTML = XmlHttp.responseText;
        }
        else
        {
            alert("Data alımında bir sorun oluştu. Tekarar deneyiniz!" );
        }
     }
 }
 
 function GetIllerdeOyOraniPartiList(sehirId,ilGenel)
{
    var requestUrl = "/Secim2009/api/ChartIllerdeOyOrani.ashx" + "?sehirid=" + sehirId +"&ilGenel="+ilGenel+"&ResponseType=1";
    CreateXmlHttp();
     
    // If browser supports XMLHTTPRequest object
    if(XmlHttp)
    {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleSlcGetIllerdeOyOraniPartiListChangedResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl,  true);

        //Sends the request to server
        XmlHttp.send(null);  
    }

}

function HandleSlcGetIllerdeOyOraniPartiListChangedResponse()
 {
    if(XmlHttp.readyState == 4)
     {
        // To make sure valid response is received from the server, 
        // 200 means response received is OK
        if(XmlHttp.status == 200)
        {  
            document.getElementById('partiKolon').innerHTML = XmlHttp.responseText;
        }
        else
        {
            alert("Data alımında bir sorun oluştu. Tekarar deneyiniz!" );
        }
     }
 }
 
 
 function GetBelediyeAdKarsilastirma(id)
{
    var requestUrl = "/Secim2009/api/BelediyeAd.ashx" + "?sehirid=" + id;
    CreateXmlHttp();
     
    // If browser supports XMLHTTPRequest object
    if(XmlHttp)
    {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleSlcBelediyeAdKarsilastirmaChangedResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl,  true);

        //Sends the request to server
        XmlHttp.send(null);  
    }

}

function HandleSlcBelediyeAdKarsilastirmaChangedResponse()
 {
    if(XmlHttp.readyState == 4)
     {
        // To make sure valid response is received from the server, 
        // 200 means response received is OK
        if(XmlHttp.status == 200)
        {  
            document.getElementById('h3KarsilastirmaBaslik').innerHTML = XmlHttp.responseText+" İçin Merkez Sonuçları";
        }
        else
        {
            alert("Data alımında bir sorun oluştu. Tekarar deneyiniz!" );
        }
     }
 }
 
 function queryStringGetValue(param) 
{
if(location.href.toUpperCase().indexOf(param.toUpperCase())>=0)
{
   var URL = location.href
   if(param!=null) {
   var regEx = new RegExp("(\\?|&)("+param+"=)(.*?)(&|$|#)","i")
   var exec = regEx.exec(URL)
   var Sonuc = RegExp.$3
} 
else 
{
   var regEx = new RegExp("(\\?)(.*?)($)","i")
   var exec = regEx.exec(URL)
   var Sonuc = RegExp.$2
}
}
else
{
var Sonuc='';
}
   return(Sonuc)
}
function AddChar(parleft,parCharAmount,parText,parChar2Add)
{
    while (parText.length<parCharAmount)
    {
        if(parleft==true)
        {
        parText = parChar2Add+parText;
        }
        else
        {
        parText = parText+parChar2Add;
        }

    }
    return parText;
}
