/* Retreive URL Deatils from database...*/
function getURL_Details() {
  var URLTo = document.getElementById('URLTo');
  var tempID;
  if (URLTo.value == ""){
     URLTo.style.border="1px solid red";
	 // set the Path
	tempID = document.getElementById('Path');
	tempID.innerHTML = "";
  }else{
     URLTo.style.border="1px solid #CCC";
	 		try {
			//alert( "Ajax/AjaxGetSubCategories.asp?CID=" + intCategoryId );
			//return;		
			var oXmlHttp = zXmlHttp.createRequest();
			oXmlHttp.open("get", "Ajax/AjaxGetURL_Details.asp?URLTo=" + URLTo.value, true);
			// Function.....
			oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				
				//alert(oXmlHttp.responseText);
				//return;
				
				// convert the string to an XML object
				//var xmldoc = (new DOMParser()).parseFromString(oXmlHttp.responseText, "text/xml");
				//var xmldoc = oXmlHttp.responseXML;
				
				// I never could understand why in IE the responseXML doesn't work!!!!!
				// I had to copy this function from http://forum.mootools.net/viewtopic.php?pid=8598
				var xmldoc = strToxml(oXmlHttp.responseText);
				//var root = xmldoc.getElementsByTagName("categories");
	
				//alert(oXmlHttp.responseText);
				//alert( root.length );
				//alert(root.firstChild)
				//return;
	
				var strTest = "";
				var items = xmldoc.getElementsByTagName("link");
				
				//alert(items.length);
				//return;
				
				if (items.length > 0){

					//To completely clear a select box, do
					var siteCat = document.getElementById('SiteCategory');
					siteCat.options.length = 0;
					// the link is ony one!
					var i = 0;
					var item = items[i];
					
					// now we have the item object, time to get the contents
					
					// get the name of the item
					var name = item.getElementsByTagName("SiteCategory")[0].firstChild.nodeValue;
					// get the quantity
					var count = item.getAttribute("count");
					// get id:
					var cid = item.getAttribute("cid");
						
					// Add a new element in the select list:
					siteCat.options[i] = new Option(name + " (" + count + ")",cid);
					//alert(strTest);
					
					// set the EmailAddress
					tempID = document.getElementById('EmailAddress');
					tempID.value = item.getElementsByTagName("EmailAddress")[0].firstChild.nodeValue;
					
					// set the FullName
					tempID = document.getElementById('FullName');
					tempID.value = item.getElementsByTagName("FullName")[0].firstChild.nodeValue;
					
					// set the FullName
					tempID = document.getElementById('URLTo');
					tempID.value = item.getElementsByTagName("URLTo")[0].firstChild.nodeValue;
					
					// set the SiteTitle
					tempID = document.getElementById('SiteTitle');
					tempID.value = item.getElementsByTagName("SiteTitle")[0].firstChild.nodeValue;
					
					// set the SiteDescription
					tempID = document.getElementById('SiteDescription');
					tempID.value = item.getElementsByTagName("SiteDescription")[0].firstChild.nodeValue;
					
					// set the Keywords
					tempID = document.getElementById('Comments');
					tempID.value = item.getElementsByTagName("Keywords")[0].firstChild.nodeValue;
					
					// set the Path
					tempID = document.getElementById('Path');
					tempID.innerHTML = "<a target='_blank' href='" + item.getElementsByTagName("Path")[0].firstChild.nodeValue + "'> " + item.getElementsByTagName("Path")[0].firstChild.nodeValue + "</a>";
				}else{
				    // set the Path
					tempID = document.getElementById('Path');
					tempID.innerHTML = "Link not found.";
				}
			}
			
			} // End function...
			
			oXmlHttp.send(null);
		} catch (oException) {
			return true;
		}
  }
}

function SelectCategory() {
	var catSelID = document.getElementById('MainCategory');
	var intSelectedIndex = catSelID.selectedIndex;
	//alert(intSelectedIndex);
	if (intSelectedIndex > 0) {
		var intCategoryId = catSelID.options[intSelectedIndex].value;
		//alert(intCategoryId);
		//return;
		
		var TRid = document.getElementById("LINK_TYPE");
        if( (TRid.checked == false && intCategoryId > 20) || TRid.checked ){
		
		try {
			//alert( "Ajax/AjaxGetSubCategories.asp?CID=" + intCategoryId );
			//return;
		
			var oXmlHttp = zXmlHttp.createRequest();
			oXmlHttp.open("get", "Ajax/AjaxGetSubCategories.asp?CID=" + intCategoryId, true);
			
			// Function.....
			oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				
				//alert(oXmlHttp.responseText);
				//return;
				
				// convert the string to an XML object
				//var xmldoc = (new DOMParser()).parseFromString(oXmlHttp.responseText, "text/xml");
				//var xmldoc = oXmlHttp.responseXML;
				
				// I never could understand why in IE the responseXML doesn't work!!!!!
				// I had to copy this function from http://forum.mootools.net/viewtopic.php?pid=8598
				var xmldoc = strToxml(oXmlHttp.responseText);
				//var root = xmldoc.getElementsByTagName("categories");
	
				//alert(oXmlHttp.responseText);
				//alert( root.length );
				//alert(root.firstChild)
				//return;
	
				var strTest = "";
				var items = xmldoc.getElementsByTagName("category");
				
				//alert(items.length);
				//return;
				
				//To completely clear a select box, do
				var siteCat = document.getElementById('SiteCategory');
				var msgDiv = document.getElementById('msgDiv');
				//var suggestCat = document.getElementById('suggestCat');
				siteCat.options.length = 0;
				if ( items.length == 0 ){
				   siteCat.options[0] = new Option("No subcategories available!","");
				   msgDiv.innerHTML = "Sorry there no subcategories available for the main category selected, please select another main category or <a href='suggest.asp' target='_self'>suggest</a> your own category.";
				   //suggestCat.className = 'show4Now';
				   msgDiv.className = 'show4Now';
				}else{
				   //suggestCat.className = 'hide4Now';
				   msgDiv.className = 'hide4Now';
				}
				
				for (var i = 0 ; i < items.length ; i++) {
					// get one item after another
					var item = items[i];
					// now we have the item object, time to get the contents
					// get the name of the item
					var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
					// get the quantity
					var count = item.getElementsByTagName("count")[0].firstChild.nodeValue;
					// get id:
					var cid = item.getAttribute("id");
					strTest =  strTest + "category #" + cid + ": name=" + name + " count=" + count;
					
					// Add a new element in the select list:
					siteCat.options[i] = new Option(name + " (" + count + ")",cid);
				}
				//alert(strTest);
				
			}
			
			} // End function...
			
			oXmlHttp.send(null);
		} catch (oException) {
			return true;
		}
		
		}else {
		   alert('You can select this category only for a featured link!');
		   var siteCat = document.getElementById('SiteCategory');
		   siteCat.options.length = 0;
		   siteCat.options[0] = new Option("This directory is only for a featured links!","");
	    }
		
		
	} else {
		alert('Please select a category!');
	}
	return false;
}

function SetFeatured(){
   var TRid = document.getElementById("LINK_TYPE");
   TRid.checked = true;
   ResetTR(false);
}

function ResetTR(flag){
   var Tableid = document.getElementById("Table1");
   var TRid = document.getElementById("TR1");
   if(flag){
      TRid.className = 'Show4Now';
	  Tableid.className = 'Show4Now';
	  var siteCat = document.getElementById('SiteCategory');
	  siteCat.options.length = 0;
	  siteCat.options[0] = new Option("Please select FIRST the Main Category..","");
   }else{
      TRid.className = 'Hide4Now';
	  Tableid.className = 'Hide4Now';
   }
}

// In the onSuccess method, call a function to convert the text response in xml
function strToxml(str) {
	var xml = null;
	if(window.ActiveXObject) {
	   xml = new ActiveXObject("Microsoft.XMLDOM");
	   xml.loadXML(str);
	} else {
	   var parser = new DOMParser();
	   xml = parser.parseFromString(str, "text/xml");
	}
	return xml;
}

function addrow(tablename, arr) {
   var tbl = document.getElementById(tablename);
   var lastRow = tbl.rows.length;
   var row = tbl.insertRow(lastRow);
	  for (r = 0; r < arr.length; r++) {   
		 var cell = row.insertCell(r);
		 cell.innerHTML = arr[r];
	  }
}
