﻿
var xmlHttp;
var ddlName;

function fetchSubCat(str,paraDDL)
{ 
    ddlName = paraDDL;  
    
    removeAll();
    
    if(str.length < 1)
    {        
        return;
    }
    
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    }

    //removeOptionLast();
    var url="<%=GetAjaxUrlSubCat() %>";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);   
}

function stateChanged() 
{ 
    if (xmlHttp.readyState==4)
    {
        var xmlDoc = xmlHttp.responseXML.documentElement;
        //var elOptNew = document.createElement('option');
        //elOptNew.text = "";
        //elOptNew.value = ""; 
        //var elSel = document.getElementById(ddlName);
        //  try {
        //    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        //  }
        //  catch(ex) {
        //    elSel.add(elOptNew); // IE only
        //  }
  
        if(xmlDoc != null)
        {
            var x=xmlDoc.getElementsByTagName('subCat');        
            for (i=0;i<x.length;i++)
            {
                appendOptionLast(x[i].childNodes[0].nodeValue);     
            }      
        }
    }
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function insertOptionBefore(num)
{
  var elSel = document.getElementById(ddlName);
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = 'Insert' + num;
    elOptNew.value = 'insert' + num;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
}

function appendOptionLast(val)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = val;
  elOptNew.value = val; 
  var elSel = document.getElementById(ddlName);
  elSel.disabled = false;
  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function removeAll()
{
    var elSel = document.getElementById(ddlName);
    elSel.length = 0;
    elSel.disabled = true;
}

function removeOptionLast()
{
    var elSel = document.getElementById(ddlName);
    var count = elSel.length;
    if(count > 0)
    {
        elSel.remove(count-1);
    }
}


function CheckAgree(chk,lblError)
{
    var chkbox = document.getElementById(chk);
    var errorLabel = document.getElementById(lblError);
    if(chkbox.checked == false)
    {
       errorLabel.innerHTML = "You need to agree Terms and Conditions";
       event.returnValue = false; 
       return false;
    }else
    {
       errorLabel.innerHTML = "";
       return true; 
    }
}

function SaveToSeverControl(txtVal,hiddenVal)
{
    var txtVal = document.getElementById(txtVal);
    var hfVal = document.getElementById(hiddenVal);
    hfVal.value = txtVal.value;
    //alert(hfVal.value);
}
