// JavaScript Document
/************************************************/
/* Function makeRequest                         */
/* Author: Trevor Llewellyn                     */
/* Credit to: 									*/
/* http://developer.mozilla.org/en/docs/AJAX:Getting_Started
/* for Ajax connection string					*/
/* Date: 11 Mar 06                              */
/* Successfully Tested on IE6, Firefox 1.5      */
/* Usage: Dynamically update subcategory        */
/*        dropdown menus using Ajax scripting   */
/* Params: url - the address of the php script  */
/*               that is running the sql query  */
/*         params - GET string (id of selected) */
/*		   source - the calling select list     */
/*		   target - the subcategory select list */
/************************************************/

function makeRequest(url, params, source, target) {
	
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { alertContents(http_request, source, target); };
	http_request.open('GET', url + params, true);
	http_request.send(null);
}

function alertContents(http_request, source, target) {
	//empty the target if the "none" value is selected in the source	
	if(source.options == 99999)
	{
		for(var q = target.options.length; q >= 0; q--)
		{
			target.options[q] = null;
		}
	}
	if (http_request.readyState == 4) {//if the server return has completed
		
		if (http_request.status == 200) { // if the return is OK
			//first clear the target box of existing values
			for(var q = target.options.length; q >= 0; q--)
			{
				target.options[q] = null;
			}						
			myarr = http_request.responseText.split('|'); // split the returned string into separate string/values since the returned string is in the format "ID1->name1|ID2->name2" etc
			for(var i = 0; i < myarr.length -1; i++)
			{
				mystr = myarr[i].split('->'); //divide each subarray into text / value
				var myoption = new Option(mystr[1], mystr[0]); // create the new option object						
				var selLength = target.options.length; //determine the current target length					
				target.options[selLength] = myoption; //add the option to the target
			}
			
		} else {
			alert('There was a problem with the request.');
		}
	}

}

function image_preview(pic, width, height)
{
	var targetName = "previewpic";	
	pic = "<? echo $root; ?>uploads/" + pic;
	var preview = new Image();
	preview.src = pic;

	var maxWidth=width; 
	var maxHeight=height;
	var hScale = preview.height / maxHeight;
	var wScale = preview.width / maxWidth;
	if((hScale > 1) || (wScale > 1))
	{
	scale = (hScale > wScale)?hScale:wScale;
	}
	else
	{
	scale = 1;
	}
	newWidth = Math.round(preview.width / scale);
	newHeight = Math.round(preview.height / scale);
	document.images[targetName].width=newWidth;
	document.images[targetName].height=newHeight;
	document.images[targetName].src = pic;
}


var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) 
{
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) 
	{
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) 
	{
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
		else
		index++;
		return found;
	}
	function getIndex(input) 
	{
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	return true;
}

function info_window(id)
{	
	var filename = 'info_window.php?repID=' + id;
	window.open(filename, "_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=310, height=400");
}

function deletelisting(id, type)
{
	var filename = 'deletelisting.php?listingID=' + id + '&type=' + type;
	var answer = confirm("Are you sure you wish to delete this listing?");
	if(answer)
	{
		window.location=filename;
	}
	else
	{
	}
}

function deletemember(id)
{
	var filename = 'deletemember.php?clientid=' + id;
	var answer = confirm("Are you sure you wish to delete this member?");
	if(answer)
	{
		window.location=filename;
	}
	else
	{
	}
}

function deleterep(id)
{
	var filename = 'deleterep.php?repid=' + id;
	var answer = confirm("Are you sure you wish to delete this representative?");
	if(answer)
	{
		window.location=filename;
	}
	else
	{
	}
}

function phonelog(url)
{
	window.open(url, "phonelog", "width=490, toolbar=no, menubar=no, fullscreen=no, height=560, left=50, top=50, location=no, scrollbars=yes");
}