var tc = false;
try {
    tc = true;
} catch(f) { }


var xmlHttpError = 'XML HTTP Request: OK';  // for trouble shooting

function getRequestObject() {
    var objRequest;
    if (window.ActiveXObject) {
        if (tc) {
            try {
                objRequest = new ActiveXObject('Msxml2.XMLHTTP');  // IE 6+
            }
            catch(e) {
                try {
                    objRequest = new ActiveXObject('Microsoft.XMLHTTP');  // IE 5.5
                }
                catch(f) { }
            }
        } else {
            objRequest = new ActiveXObject('Microsoft.XMLHTTP');  // ? IE 5.0 ?
        }
    } else if (window.XMLHttpRequest) {
        objRequest = new XMLHttpRequest();  // All other browsers, inc. IE 7 and DOM 3
    }
    return objRequest;
}

function includeFile(pUrl,pElementId,pImageSrc) {
    if (arguments.length==3) {  // if an image source is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }
    if (arguments.length>=3) {  // if a targetElement ID is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }

    var objRequest = getRequestObject();  // create a new request object

    if (typeof(objRequest)=='object') {  // if we have an object, and
        if (objRequest.readyState>=0) {  // if the object supports the correct properties...proceed
						// specifiy the function to handle the results
						 // the correct request object is passed for us.
            objRequest.onreadystatechange = 
								function() { 
									handleHttpResponse(objRequest, 
									pElementId); 
								};            
						objRequest.open('GET', pUrl, true);
            objRequest.send(null);
        } else {
            xmlHttpError = 'XML HTTP Request Object Unavailable';
            return false;
        }
    } else {
        xmlHttpError = 'XML HTTP Request Object Not Supported';
        return false;
    }
}

function doAJAX(pUrl,pElementId,pImageSrc) {
    if (arguments.length==3) {  // if an image source is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }
    if (arguments.length>=3) {  // if a targetElement ID is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="'+pImageSrc+'">';  
        }
    }

    var objRequest = getRequestObject();  // create a new request object

    if (typeof(objRequest)=='object') {  // if we have an object, and
        if (objRequest.readyState>=0) {  // if the object supports the correct properties...proceed
						// specifiy the function to handle the results
						 // the correct request object is passed for us.
            objRequest.onreadystatechange = 
								function() { 
									handleHttpResponse(objRequest, 
									pElementId); 
								};            
						objRequest.open('POST', pUrl, true);
            objRequest.send(null);
        } else {
            xmlHttpError = 'XML HTTP Request Object Unavailable';
            return false;
        }
    } else {
        xmlHttpError = 'XML HTTP Request Object Not Supported';
        return false;
    }
}

function handleHttpResponse(pObjRequest, pElementId) {
    if (pObjRequest.readyState==4) {
        if (pObjRequest.status==200) {
						// load the results into the element
						var resp = postProcess(pObjRequest.responseText);
            document.getElementById(pElementId).innerHTML = resp;  
            pObjRequest=null;  // dispose of the now un-needed object.
        }
    }
}

function newSubCat(obj) {
	var subCategory = document.getElementById('subcategory');
	var choice = obj.value;
	//var pieces = choice.split(':');
	//var catno = pieces[0];
	includeFile('includes/subcat_' + choice + '.inc.php', 'subcategory');
}

function getRecipe(obj, pUrl, pElementId) {
	var tgt = pUrl + '?recipe=' + obj.value;
	doAJAX(tgt, pElementId);
}

function newCategory(obj) {
	var tgt = "tmpl2.php?pagename=displayRecipe&cat=" + obj.value;
	window.location = tgt;
}

function editRecipe() {
	var id = document.getElementById('recipes').value;
	var tgt = 'prepareEdit.php?id=' + id;
	window.location = tgt;
}

function postProcess(txt) {
	var hideIt = txt.substring(0,1);
	var editButton = document.getElementById('showEdit');
	if (hideIt == 0) 
		editButton.style.display = 'none';
	else
		editButton.style.display = '';
	return txt.substring(1);
}

function validateRegister() {
		
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var password_confirm = document.getElementById('password_confirm').value;
	var email = document.getElementById('email').value;
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('firstname').value;

		
	var msg = '';
	if (username.length < 6) msg = msg + 'A username of at least 6 characters is required\r\n';
	if (password.length < 6) msg = msg + 'A password of at least 6 characters is required\r\n';
	if (password_confirm.length < 6) msg = msg + 
								'A password confirmation of at least 6 characters is required\r\n';
	if (email.length == 0) msg = msg + 'An email is required\r\n';
	if (firstname.length == 0) msg = msg + 'A first name is required\r\n';
	if (lastname.length == 0) msg = msg + 'A last name is required\r\n';

	
	if (msg.length > 0) {
		alert(msg);
		return false;
	} else {
		if (password != password_confirm) {
			alert('The password and confirm password do not match');
			return false;
		}
		else if (password.length < 6) {
			alert('The password must be at least 6 characters');
			return false;
		}
			
		return true;
	}
} 

function changeImage(txt, w, h) {
	doTimer();
	var img = document.getElementById("change");
	img.src = txt;
	img.width = w;
	img.height = h;
}
