// JScript File
var READYSTATE_UNINITIALIZED = 0;
var READYSTATE_LOADING = 1;
var READYSTATE_LOADED = 2;
var READYSTATE_INTERACTIVE = 3;
var READYSTATE_COMPLETE = 4;

function CreateXmlHttpRequestObject(){
    if (window.XMLHttpRequest){
        xmlHttpObj = new XMLHttpRequest();
    }else{

        try{
            xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
        }
		if (window.ActiveXObject){
			try{
				xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
			}
		}else{
			xmlHttpObj = new XMLHttpRequest();
		}
    }
	return xmlHttpObj;
}

function getAjaxResponse(type){
	var url = "";
	var d = new Date()
	if(type==1){
		resetDD("searchspeciality");
		id = document.searchform.searchindustry.options[document.searchform.searchindustry.selectedIndex].value;
		url = "http://www.businessnetworkinggroup.com.au/ajax.asp?query=1&id=" + id + "&t=" + d.getTime();
		childNode = "Speciality";
		ddName = "searchspeciality"
	}else if(type==2){
		resetDD("searchlocation");
		id = document.searchform.searchstate.options[document.searchform.searchstate.selectedIndex].value;
		url = "http://www.businessnetworkinggroup.com.au/ajax.asp?query=2&id=" + id + "&t=" + d.getTime();
		childNode = "Location";
		ddName = "searchlocation"
	}else if(type==3){
		resetDD("ddspeciality");
		var obj = document.getElementById("ddindustry");
		id = obj.options[obj.selectedIndex].value;
		url = "http://www.businessnetworkinggroup.com.au/ajax.asp?query=1&id=" + id + "&t=" + d.getTime(); 
		childNode = "Speciality";
		ddName = "ddspeciality"
	}else if(type==4){
		resetDD("ddlocation");
		var obj = document.getElementById("ddstate");
		id = obj.options[obj.selectedIndex].value;
		url = "http://www.businessnetworkinggroup.com.au/ajax.asp?query=2&id=" + id + "&t=" + d.getTime(); 
		childNode = "Location";
		ddName = "ddlocation"
	}
	
	if(id != "0"){
		var xmlHttpObj = CreateXmlHttpRequestObject();
		if (xmlHttpObj){
			xmlHttpObj.open("GET",url, true);
			xmlHttpObj.onreadystatechange = function(){
				if ( xmlHttpObj.readyState == READYSTATE_COMPLETE ){
				// If the request was ok (ie. equal to a Http Status code of 200)
					if (xmlHttpObj.status == 200){
						if (window.ActiveXObject){
							xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
							xmlDoc.async="false";
							xmlDoc.loadXML(xmlHttpObj.responseText);
						}else{
							 var parser=new DOMParser();
							 var xmlDoc=parser.parseFromString(xmlHttpObj.responseText,"text/xml");
						}
						
						if(xmlDoc){
							var ctrl = document.getElementById(ddName);
							if(xmlDoc.getElementsByTagName(childNode)){
								if(xmlDoc.getElementsByTagName(childNode).length > 0){
									for(var i=0; i<xmlDoc.getElementsByTagName(childNode).length;i++){
										// get the name
										var htmlCode = document.createElement('option');
										ctrl.options.add(htmlCode);
										htmlCode.text =  xmlDoc.getElementsByTagName(childNode)[i].getAttribute('name');
										htmlCode.value =  xmlDoc.getElementsByTagName(childNode)[i].getAttribute('id');
									}
								}
							}
						}
					}
				}
			}
			xmlHttpObj.send(null);
		}
	}
}

function resetDD(ddName){
	var dd = document.getElementById(ddName);
	dd.options.length = 1;
}

function emailvalid(nChamp) {
  if (nChamp.value.length > 0) {
    if ((nChamp.value.lastIndexOf(".") < nChamp.value.indexOf("@")+3 
      || nChamp.value.lastIndexOf(".") > nChamp.value.length-3 
      || nChamp.value.indexOf("@") < 1 ) 
      || nChamp.value.indexOf("@") != nChamp.value.lastIndexOf("@")) {
         alert("Invalid email address.");
      nChamp.focus();
      nChamp.select();
    }
  }
}

function limitText(limitField, limitCount, limitNum, charobj) {
	obj = document.getElementById(charobj);
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
		if(obj){obj.innerHTML = "0"}
	} else {
		limitCount.value = limitNum - limitField.value.length;
		if(obj){obj.innerHTML = limitCount.value}
	}
}

function limitLines(limitField, limitCount, limitNum, charobj,limitLines){
	obj = document.getElementById(charobj);
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
		//if(obj){obj.innerHTML = "0"}
	} else {
		limitCount.value = limitNum - limitField.value.length;
		//if(obj){obj.innerHTML = limitCount.value}
	}
	var val=limitField.value.replace(/\r/g,'').split('\n');
	if(val.length>limitLines){
		//alert('You can not enter\nmore than '+limitLines+' lines');
		limitField.value=val.slice(0,-1).join('\n')
	}else{
		obj.innerHTML = 7-val.length;
	}
}


