// JavaScript Document
window.onload = newWinLinks;

function newWinLinks(){
	for(var i=0; i<document.links.length; i++){
		if(document.links[i].className == "newWin"){
			document.links[i].onclick = newWindow;
		}
		
	}
}

function newWindow(){
	var imageWindow = window.open(this.href, "ImagesWin", "resizable=no, width=900, height=500, left=270, top=150");
	imageWindow.focus();
	return false;
}

/*
 * CC Form Handlers
 */

var canSubmitCC = false;

function checkCCForm() {
	return canSubmitCC;
}

function submitCCForm() {
	/*
	if (document.getElementById('action').value == "0") {
		alert("Please select an action");
		return;
	}
	*/
	canSubmitCC = true;
	document.forms.outform.submit();	
}


// Search for CCID
function checkCCID(f) {
	var q = document.getElementById(f).value;
	ajaxCall(window.location.href+'?f='+f+'&q='+q, "ccid_callback");
	//http://www.outboundservices.com.au/logged/loggedin_ccfeedbk.php?f=123&q=
}

function ccid_callback(txt) {
	if (txt.length == 0) return;

	var row = eval("("+txt+")");
	document.getElementById('ccid').value = row['CCID'];
	document.getElementById('pcode').value = row['PCODE'];
	document.getElementById('coname').value = row['CONAME'];
	document.getElementById('name1').value = row['NAME1'];
	document.getElementById('name2').value = row['NAME2'];
	document.getElementById('phone').value = row['PHONE'];
	document.getElementById('phone2').value = row['PHONE'];
	document.getElementById('coaddr').value = row['COADDR'];
	document.getElementById('coaddr2').value = row['COADDR2'];
	document.getElementById('sales_stage').innerHTML = row['SALES_STAGE'];
}


// Make an AJAX object, cross-browser compatible
function initAjax() { 
	var ajaxObj = null;
	if (window.XMLHttpRequest) {
		ajaxObj = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
	}	
	return ajaxObj;
}

function ajaxReady(ajaxObj) { return (ajaxObj.readyState == 4 || ajaxObj.readyState == "complete"); }

/*
 * Jordon's Cool Function.
 */
function ajaxCall(url, callback, postData, x) {
	var ajaxObj = initAjax();
	if (ajaxObj == null) return false;

    ajaxObj.onreadystatechange = function() {
		if (!ajaxReady(ajaxObj)) return;
		if (ajaxObj.status >= 400) {
			alert('(AJAX Error) Server returned '+ajaxObj.status+' '+ajaxObj.statusText, 10000);
		} else {
			eval(callback + "(ajaxObj.getResponseHeader('Content-Type').substr(0, 8).toLowerCase() == 'text/xml' ? ajaxObj.responseXML : ajaxObj.responseText, x);");
		}
    }
	
	if ((typeof(postData) != "undefined") || (postData == null)) {
		ajaxObj.open("POST", url, true);
		ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxObj.send(postData);
	} else {
		ajaxObj.open("GET", url, true);
		ajaxObj.send(null);
	}	
}


// Generic AddEvent Function
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false); 
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

