﻿var xmlHttp;
var xmlDoc ;
 
function Ajax_GetHttp() 
{
	try 
	{
		xmlHttp = new XMLHttpRequest();
	} 
	catch (trymicrosoft) 
	{
		try 
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (othermicrosoft) 
		{
			try 
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed) 
			{
				xmlHttp = false;
			}
		}
	}

	if ( xmlHttp == null)
	{
		throw new Error("XMLHttpRequest Init Error!");
		return null;
	}
	else
	{
		return xmlHttp ;
	}
}


function Ajax_CheckState( request )
{
	if ( request == null )
	{
		throw new Error( "XMLHTTPRequest Is Null!");
		return false;
	}
	if (request.readyState == 4) 
	{
		if ( request.status == 200 ) return true ;
		if (request.status == 404) 
		{
			throw new Error ("Request is not found.");
 		} 
		else if (request.status == 403) 
		{
			throw new Error("Access is denied.");
 		} 
		else
		{
			throw new Error("Error , Status: " + request.status);
		}
	}
	return false;
}


function callServer(ToServerUrl , CallBackUrl) 
{
	// Open a connection to the server
	xmlHttp.open("GET", ToServerUrl, true);
	xmlHttp.setRequestHeader("Content-Type","text/xml");
	xmlHttp.setRequestHeader("Content-Type","utf-8");
	xmlHttp.setRequestHeader("pragma","no-cache");
	xmlHttp.setRequestHeader("Cache-Control","no-cache, must-revalidate");
	xmlHttp.setRequestHeader("If-Modified-Since","0");
	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = CallBackUrl;

	// Send the request
	xmlHttp.send(null);
}

function GetXmlDom()
{
	xmlDoc = null;

	try
	{
		xmlDoc = new DOMParser() ;
	}
	catch(f)
	{
		try
		{
			xmlDoc =  new ActiveXObject("Microsoft.XMLDOM") ;
		}
		catch(s)
		{
			xmlDoc = new ActiveXObject("Msxml2.DOMDocument"); //ie5.5+,CreateObject("Microsoft.XMLDOM") 
		}
	}
	if ( xmlDoc == null ) 
	{
		throw new Error("Create XMLDOM Error!");
	}
	xmlDoc.async = true;
//	xmlDoc.loadXML( XmlContent ) ;
//	xmlDoc.load("Data.xml");
//	if (xmlDoc.parseError.errorCode != 0) 
//	{ 
// 		throw new Error("You have error " + xmlDoc.parseError.reason);
//	}
	return true ;
}

function LoadXml( TextContent )
{
	try
	{
		var res = xmlDoc.parseFromString( TextContent , "text/xml");
		return res ;
	}
	catch ( exp )
	{
		xmlDoc.loadXML( TextContent ) ;
		return xmlDoc ;
	}
}

function xml()
{

	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	var currNode;
	xmlDoc.async = false;
	xmlDoc.load("Data.xml");
	if (xmlDoc.parseError.errorCode != 0) 
	{ 
		var myErr = xmlDoc.parseError;
		document.write("You have error " + myErr.reason);
	}
	else
	{
		document.write("<hr size=1>");
		xmlDoc.setProperty("SelectionLanguage", "XPath");
		currNode = xmlDoc.documentElement.childNodes[1]; 
		alert(currNode.childNodes[0].attributes[0].value); 

	for(i=0;i<currNode.childNodes.length;i++) 
	{
		for(j=0;j<currNode.childNodes[i].attributes.length;j++) 
		{
			document.write(currNode.childNodes[i].attributes[j].name + " : " + currNode.childNodes[i].attributes[j].value + "<BR>");
		}
		document.write("<hr size=1>");
		}
	}
}

function InitData( DropDownList , XmlValueList )
{
    var i=0;

    if(DropDownList.name == "ShopSearch1$BigType"&& DropDownList.options.length==1)
    {

	}
	else
	{
	    DropDownList.options.length = 0 ;
	}
	
    for(i;i<XmlValueList.length;i++)
	{
		DropDownList.options[ DropDownList.options.length ] = new Option( 
			XmlValueList[i].getAttribute("name")  ,
			XmlValueList[i].getAttribute("id")  
			) ;
	}
}

function CopyData( DropDownListFrom ,DropDownListTo )
{
	DropDownListTo.options.length = 0 ;
	for(var i= 0;i<DropDownListFrom.options.length;i++)
	{
		DropDownListTo.options[ DropDownListTo.options.length ] = new Option( 
			DropDownListFrom.options[i].text ,
			DropDownListFrom.options[i].value 
			) ;
	}
}

function GetConID(ConID)
{
/*
	var theCon  ;
	var arr = document.body.getElementsByTagName("*") ;
	for ( var i = 0 ;i< arr.length ;i++)
	{
		var con = arr[i] ;
		if ( con.nodeType == 1 && con.getAttribute("ConID") == ConID )
		{
			theCon = con;
			break;
		}
	}
	return theCon ;
*/

	var theCon ;
	
 	theCon = GetSubConID ( document.body.getElementsByTagName("input") ,ConID ) ;
	if ( theCon != null ) return theCon ;
	
	theCon = GetSubConID ( document.body.getElementsByTagName("select") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("table") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("tbody") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("tr") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("td") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("div") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("a") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("font") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("span") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("strong") ,ConID ) ;
	if ( theCon != null ) return theCon ;

	theCon = GetSubConID ( document.body.getElementsByTagName("label") ,ConID ) ;
	if ( theCon != null ) return theCon ;
	
	theCon = GetSubConID ( document.body.getElementsByTagName("*") ,ConID ) ;
	if ( theCon != null ) return theCon ;
	
	return document.getElementById( ConID ) ;
}

function GetTagConID(TagName ,ConID)
{
	return GetSubConID ( document.body.getElementsByTagName( TagName ) ,ConID ) ;
}

function GetSubConID( ControlArray , ConID )
{
 	for ( var i = 0 ;i< ControlArray.length ;i++)
	{
		var con = ControlArray[i] ;
		if ( con.nodeType == 1 && con.getAttribute("ConID") == ConID )
		{
			 return con ;
		}
	}
	return null; 
}

function CheckConID()
{
	var theAll = String();
	var theImpt = String() ;
	theAll = "these controls has id but not ConID:\r\n\r\n";
	theImpt = " these important controls has name but not id and ConID:\r\n";
	var arr = document.getElementsByTagName("*") ;
	for ( var i = 0 ;i< arr.length ;i++)
	{
		var con = arr[i] ;
		if (
		con.nodeType == 1 
		&& con.id != null 
		&& con.id != "" 
 		&& con.getAttribute("ConID") == null
		 )
		{
			theAll += con.id +"\r\n";
 		}
 		
 		if ( con.nodeName == "input" && con.getAttribute("ConID") == null )
 		{
 			if ( con.id == null && con.name != null )
 			{
 				theImpt += con.name + "\r\n" ;
 			}
 			else if ( con.id == null && con.name ==  null )
 			{
 				theImpt += "XXXXXX	Exception Control,neither id nor name!	XXXXXX\r\n";
 			}
 		}
	}
	theAll += "\r\n\r\n" + theImpt ;
	
	alert( theAll );
}
