// The real work.  Update the editable word and the select box.
function combo_keyup(element, select_box) {
	var element2 = document.getElementById(select_box);
	//element.options[0].value = combo_word;
	//element.options[0].text = combo_word;
	var combo_wlc = element.value.toLowerCase();
	var combo_select = 0;
	if(combo_wlc.length != 0) {
		for(i=1; i<element2.options.length; i++) {
			combo_sel = element2.options[i].text.toLowerCase();
			if(combo_wlc.length <= combo_sel.length) {
				combo_sel = combo_sel.substring(0, combo_wlc.length);
				if(combo_wlc == combo_sel) {
					combo_select = i;
					break;
				}
			}
		}
	}
	element2.selectedIndex = combo_select;
	element2.onchange();
	return false;
}

var queryString;

function prefill(num, state, suburb) {	
	if(suburb.length != 0)
		setQueryStr(document.getElementById(state+num).value, suburb);
	else {
		document.getElementById('subdiv_'+num).innerHTML = "<select name='suburbs_"+num+"' id='suburbs_"+num+"' size='5' onchange=\"prefill("+num+", 'state_', this.value)\"><option value='-1'>Loading Suburbs ...</option></select>";
		setQueryString(state);
	}
	var url="extra_admin/locality.php";
	httpRequest("POST",url,true, num, 1);
}

function checkLogin(login, company, button) {
	if(button.name.length != 0) {
		document.getElementById('btn').value = button.name;
		document.getElementById('back').value = 0;
	}
	setQueryStr(login, company);
	var url="extra_admin/avail_login.php";
	httpRequest("POST",url,true, "", 2);
}

function setQueryString(state) {
	queryString = "state="+encodeURIComponent(state);
}

function setQueryStr(state, suburb) {
	queryString = "suburb="+encodeURIComponent(suburb)+"&state="+encodeURIComponent(state);
}

function setString(id, type, member) {
	queryString = "id="+encodeURIComponent(id)+"&type="+encodeURIComponent(type)+"&member="+encodeURIComponent(member);
}

function httpRequest(reqType,url,asynch, num, option){
	//Mozilla-based browsers
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest(  );
	} 
	else if (window.ActiveXObject){
		request=new ActiveXObject("Msxml2.XMLHTTP");
		if (! request){
			request=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	//the request could still be null if neither ActiveXObject
	//initialization succeeded
	if(request){
		initReq(reqType,url,asynch, num, option);
	} 
	else {
		alert("Your browser does not permit the use of all of this application's features!");
	}
}

function initReq(reqType,url,isAsynch, num, option){
	if(option == 1) {
		request.onreadystatechange=function() {
			if(request.readyState == 4){
				if(request.status == 200){
					var vals = eval(request.responseText);
					if(vals.length == 1) {
						document.getElementById('pcode_'+num).value = vals[0];
					}
					else if(vals.length == 2) {
						document.getElementById('pcode_'+num).value = vals[0];
						document.getElementById('subdiv_'+num).innerHTML = "<select name='suburbs_"+num+"' id='suburbs_"+num+"' size='5' onchange=\"prefill('"+num+"', 'state_', this.value)\">"+vals[1]+"</select>";
					}
				} 
				else {
					alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
				}
			}//end outer if
		};
	}
	else if(option == 2) {
		request.onreadystatechange=function() {
			if(request.readyState == 4){
				if(request.status == 200){	
					var array = eval(request.responseText);
					formValidate(array);
				} 
				else {
					alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
				}
			}//end outer if
		};
	}
	else if(option == 3) {
		request.onreadystatechange=function() {
			if(request.readyState == 4){
				if(request.status != 200)
					alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
				else {
					if(request.responseText.length != 0)
						alert(request.responseText);
					else {
						document.getElementById('tagged').style.display = "none";
						document.getElementById('not_tagged').style.display = "block";
					}
				}
			}
		}
	}
	request.open(reqType,url,isAsynch);
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	request.send(queryString);	
}

function add_location(fields) {
	fields = fields.replace(/'/g, "\"");
	fields = fields.replace(/\\\"/g, "'");
	var div = document.getElementById('additional');
	var locations = document.getElementById('locations');
	var num = locations.value;
	locations.value = parseInt(num)+1;
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id", "loc_"+num);
	newdiv.innerHTML = '<h2>'+(3+parseInt(num))+'. Other Location</h2><div class="mainReg">'+fields.replace(/loc_[0-9]/g, "loc_"+num)+'</div>';
	div.appendChild(newdiv);
	div.style.display = "block";
}
	
function remove_location() {
	var div = document.getElementById('additional');
	var locations = document.getElementById('locations');
	var num = parseInt(locations.value)-1;
	var newdiv = document.getElementById('loc_'+num);
	div.removeChild(newdiv);
	if(num == 0)
		div.style.display = "none";
	else
		locations.value = num;
}

function count(button) {
	document.getElementById('btn').value = button.name;
	document.getElementById('back').value = 0;
	var services = 0;
	var theForm = document.expert_signup;
	for(var i = 0; i < theForm.length; i++) {
		if(theForm[i].type == "checkbox" && theForm[i].name != "section[]" && theForm[i].checked) {
			services = services + 1;
		}
	}
	if(services > 15)
		alert("Maximum number of Services allowed is 15");
	else if(services == 0)
		alert("Please select atleast one Service");
	else 
		theForm.submit();
}

function formValidate(errors) {
	var error = "";
	var expression;
	var name;
	if(document.getElementById('name')) {
		name = document.getElementById('name').value;
		if(name.length == 0)
			error += "Please enter Company Name\n";
		else if(errors[1] && errors[1].length != 0)
			error += errors[1]+"\n";
	}
	var web;
	if(document.getElementById('web')) {
		web = document.getElementById('web').value;
		if(web.length != 0) {
			expression = /^http:\/\/[A-Za-z0-9-]+\.[A-Za-z0-9]+/;
			if(!expression.test(web))
				error += "You have entered an invalid web address\n";
		}
	}
	var user = document.getElementById('user').value;
	if(user.length == 0)
		error += "Please enter a User name\n";
	else if(errors[0] && errors[0].length != 0)
		error += errors[0]+"\n";
	var lemail = document.getElementById('lemail').value;
	if(lemail.length == 0)
			error += "Please enter a Login Email address\n";
	else {
		expression = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!expression.test(lemail))
			error += "Please enter a valid Login Email Address\n";
	}
	var pass = document.getElementById('pass').value;
	if(pass.length == 0)
		error += "Please enter a Password\n";
	else {
		expression = /^(.*)([0-9])(.*)$/;
		if(pass.length < 6)
			error += "Password must be more than 6 characters\n";
		else if(!expression.test(pass))
			error += "Passwords must contain at least one numeric character\n";
	}
	if(document.getElementById('npass')) {
		var npass = document.getElementById('npass').value;
		if(npass.length != 0) {
			expression = /^(.*)([0-9])(.*)$/;
			if(npass.length < 6)
				error += "New Password must be more than 6 characters\n";
			else if(!expression.test(npass))
				error += "New Passwords must contain at least one numeric character\n";
			var cpass = document.getElementById('cpass').value;
			if(cpass.length == 0)
				error += "Please Confirm New Password\n";
			else if(cpass != npass)
				error += "Confirmation password doesn't match\n";
		}
	}
	else {
		var cpass = document.getElementById('cpass').value;
		if(cpass.length == 0)
			error += "Please Confirm Password\n";
		else if(cpass != pass)
			error += "Confirmation password doesn't match\n";
	}
	var locations;
	if(document.getElementById('locations')) {
		locations = document.getElementById('locations').value;
		for(var i = 0; i < locations; i++) {
			var address = document.getElementById('address_loc_'+i).value;
			if(address.length == 0)
				error += "Please enter your Address "+i+"\n";
			var state = document.getElementById('state_loc_'+i);
			if(state.value == -1)
				error += "Please select a State "+i+" from the list\n";
			var suburb = document.getElementById('suburbs_loc_'+i);
			if(suburb.value == "-1" || suburb.options[0].selected || suburb.value == "")
				error += "Please select a Suburb "+i+" from the list\n";
			var pcode = document.getElementById('pcode_loc_'+i).value;
			if(pcode.length == 0)
				error += "Please enter your Post Code "+i+"\n";
			else {
				expression = /^[0-9]+$/;
				if(!expression.test(pcode))
					error += "Post Code "+i+" must be numbers only\n";
			}
			var phone = document.getElementById('phone_loc_'+i).value;
			if(phone.length == 0)
				error += "Please enter a contact Phone Number "+i+"\n";
			else {
				expression = /^[0-9 ()]+$/;
				if(!expression.test(phone))
					error += "Please enter a valid Phone Number "+i+"\n";
			}
			var fax = document.getElementById('fax_loc_'+i).value;
			if(fax.length != 0) {
				expression = /^[0-9 ()]+$/;
				if(!expression.test(fax))
					error += "Please enter a valid Fax Number "+i+"\n";
			}
			var email = document.getElementById('email_loc_'+i).value;
			if(email.length == 0)
				error += "Please enter an Email address "+i+"\n";
			else {
				expression = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(!expression.test(email))
					error += "Please enter a valid Email Address "+i+"\n";
			}
		}
	}
	else {
		var pcode = document.getElementById('pcode').value;
		expression = /^[0-9]+$/;
		if(!expression.test(pcode) && pcode.length != 0)
			error += "Post Code must be numbers only\n";
	}
	if(error.length != 0)
		alert(error);
	else {
		for(var i = 0; i < locations; i++) 
			document.getElementById('suburb_loc_'+i).disabled = true;
		document.expert_signup.submit();
	}
}

function unselect(section, i, num) {
	var theForm = section.form;
	var end = 0;
	for(var j = 0; j < theForm.length; j++) {
		if(theForm[j].type == "checkbox" && theForm[j].name == "section[]" && theForm[j].value == i) {
			end = num + j;
			break;
		}
	}
	for(; j <= end; j++) {
		theForm[j].checked = section.checked;
	}
} 

function selectServ(section, i) {
	var theForm = section.form;
	for(var j = 0; j < theForm.length; j++) {
		if(theForm[j].type == "checkbox" && theForm[j].name == "section[]" && theForm[j].value == i) {
			theForm[j].checked = true;
			break;
		}
	}
}

function checkLocations(button) {
	document.getElementById('btn').value = button.name;
	document.getElementById('back').value = 0;
	var count = 0;
	var theForm = document.expert_signup;
	for(var i = 0; i < theForm.length; i++) {
		if(theForm[i].type == "checkbox") {
			if(theForm[i].checked == true)
				count++;
		}
	}
	var locations = document.getElementById('locations').value;
	if(count < locations)
		alert("Please verify all Locations");
	else if(count == locations)
		theForm.submit();
}

function goBack(button) {
	document.getElementById('btn').value = button;
	document.getElementById('back').value = 1;
	document.expert_signup.submit();
}

function shortlist(id, type) {
	document.getElementById('prod_id').value = id;
	document.getElementById('prod_type').value = type;
	document.shortlist.submit();
}

function openSearchAdvDiv(show) {
	for(var i = 0; i < searchDivs.length; i++) {
		if(searchDivs[i] == show) {
			document.getElementById('div_'+show).style.display = "none";
			document.getElementById('advdiv_'+show).style.display = "block";
			document.getElementById('search_text_'+show).style.display = "block";
			if(document.getElementById('advdiv_'+show+'_link'))
				document.getElementById('advdiv_'+show+'_link').style.display = "none";
		}
		else {
			document.getElementById('advdiv_'+searchDivs[i]).style.display = "none";
			document.getElementById('search_text_'+searchDivs[i]).style.display = "none";
		}
	}
}

function openSearchDiv(show, adv) {
	if(typeof(searchDivs) != "undefined") {
		var flag = 0;
		for(var i = 0; i < searchDivs.length; i++) {
			if(searchDivs[i] == show) {
				document.getElementById('div_'+show).style.display = "block";
				document.getElementById('search_text_'+show).style.display = "block";
				document.getElementById('searchSection_'+show).value = show;
				document.getElementById('searchLink_'+show).className ="active_tab";
				document.getElementById('advSearchSection_'+show).value = show;
				if(document.getElementById('advdiv_'+show+'_link'))
					document.getElementById('advdiv_'+show+'_link').style.display = "block";
				flag = 1;
			}
		}
		if(flag) {
			for(var i = 0; i < searchDivs.length; i++) {
				if(searchDivs[i] != show) {
					document.getElementById('div_'+searchDivs[i]).style.display = "none";
					document.getElementById('search_text_'+searchDivs[i]).style.display = "none";
					document.getElementById('searchLink_'+searchDivs[i]).className = "tab";
				}
			}
		}
		if(adv)
			openSearchAdvDiv(show);
		else {
			for(var i = 0; i < searchDivs.length; i++)
				document.getElementById('advdiv_'+searchDivs[i]).style.display = "none";
		}
	}
}

function openShortlistDiv(show) {
	if(typeof(shortDivs) != "undefined") {
		var flag = 0;
		for(var i = 0; i < shortDivs.length; i++) {
			if(shortDivs[i] == show) {
				document.getElementById('short_'+show).style.display = "block";
				document.getElementById('shortLink_'+show).style.color = "red";
				flag = 1;
			}
		}
		if(flag) {
			for(var i = 0; i < shortDivs.length; i++) {
				if(shortDivs[i] != show) {
					document.getElementById('short_'+shortDivs[i]).style.display = "none";
					document.getElementById('shortLink_'+shortDivs[i]).style.color = "";
				}
			}
		}
	}
}

function openTagDiv(show) {
	if(typeof(tagDivs) != "undefined") {
		var flag = 0;
		for(var i = 0; i < tagDivs.length; i++) {
			if(tagDivs[i] == show) {
				document.getElementById('tag_'+show).style.display = "block";
				document.getElementById('tagLink_'+show).style.color = "red";
				flag = 1;
			}
		}
		if(flag) {
			for(var i = 0; i < tagDivs.length; i++) {
				if(tagDivs[i] != show) {
					document.getElementById('tag_'+tagDivs[i]).style.display = "none";
					document.getElementById('tagLink_'+tagDivs[i]).style.color = "";
				}
			}
		}
	}
}

function comCount(compare) {
	var limit = 3;
	var selected = 0;
	var product;
	var button1;
	var button2;
	if(selected = document.getElementById('comp')) {
		button1 = document.getElementById('button1');
		button2 = document.getElementById('button2');
		if(compare.checked)
			selected.value = parseInt(selected.value)+1;
		else
			selected.value = parseInt(selected.value)-1;
		if(selected.value > limit) {
			if(selected.value-limit == 1)
				product = "product";
			else
				product = "products";
			alert("You have selected "+selected.value+" products and the limit is "+limit+".\nPlease diselect "+(selected.value-limit)+" "+product+" in order to continue.");
			button1.disabled = true;
			button2.disabled = true;
		}
		else if(selected.value == 0) {
			button1.disabled = true;
			button2.disabled = true;
		}
		else {
			button1.disabled = false;
			button2.disabled = false;
		}
	}
}

function tagArt(id, type, member) {
	setString(id, type, member);
	var url="extra_admin/tag_article.php";
	httpRequest("POST",url,true, "", 3);
}

var font_size = 0;

function changeElementFontSize(elem, dx, option)
{
	if (!elem)
		return;

	/* Do a depth-first traversal so that we don't get a multiplying
	 * font size effect. */
	for (var i = 0; i < elem.childNodes.length; ++i)
	{
		changeElementFontSize(elem.childNodes[i], dx, 0);
	}
	
	/* Only change font size for element nodes. */
	if (elem.nodeType == 1 /* ELEMENT_NODE */)
	{
		var size;

		/* IE and Mozilla have different methods for getting the current style. */
		if (window.getComputedStyle)
		{
			size = parseInt(window.getComputedStyle(elem, null).fontSize);
		}
		else if (elem.currentStyle)
		{
			size = parseInt(elem.currentStyle['fontSize']);
		}
		else
		{
			return;
		}
		if(font_size < 6 && font_size > -2) {
			size += dx;
			elem.style.fontSize = new String(size) + "px";
		}
	}
	if(option) {
		if(dx > 0 && font_size < 6)
			font_size+=1;
		else if(dx < 0 && font_size > -2)
			font_size-=1;
	}
}
