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 deleteLocation(storeID, directoryID) {
	/*  Put this in when we have signatures in place
	var sig = trim(document.getElementById('sig').value);
	if (sig.length < 8) {
		alert('A digital signature (password) is required\n');
		return false;
	}
	*/
	proceed =	confirm('Are you sure you want to delete this location?');
	if (proceed) window.location = 'deleteLocation.php?storeID=' + storeID + '&directoryID=' + directoryID;
	else return;
}

function confirm_entry() {
	proceed = confirm('Are you sure you want to completely delete this classified? This action cannot be reversed.');
	if (proceed) window.location = 'deleteClassifiedList_submit.php';
	else return;
}

function confirm_inactive() {
	proceed = confirm('Are you sure you want to make this classified inactive?');
	if (proceed) window.location = 'deleteClassifiedList_submit.php';
	else return;
}

function confirm_active() {
	proceed = confirm('Are you sure you want to make this classified active?');
	if (proceed) window.location = 'deleteClassifiedList_submit.php';
	else return;
}

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="../new/js/'+pImageSrc+'">';  
        }
    }
    if (arguments.length>=3) {  // if a targetElement ID is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="../new/js/'+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="../new/js/'+pImageSrc+'">';  
        }
    }
    if (arguments.length>=3) {  // if a targetElement ID is sent
        if (pImageSrc) {
						// load the image while waiting
            document.getElementById(pElementId).innerHTML='<img src="../new/js/'+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) {
	var img = document.getElementById("mainImageSpot");
	img.src = txt;
	img.width = w;
	img.height = h;
}

function submitSearch(state){
	var cat = '';  //var cat = document.getElementById('catMenu').value;
	var subcat = document.getElementById('subcatMenu').value;
	window.location='stateSearch.php?state=' + state + '&cat=' + cat + '&subcat=' + subcat;
}

function submitCSearch(state){
	var cat = '';  //var cat = document.getElementById('catMenu').value;
	var subcat = document.getElementById('subcatMenu').value;
	window.location='stateCSearch.php?state=' + state + '&cat=1&subcat=' + subcat;
}

function setSearch(which) {
	if (which == "state") {
		document.getElementById('zipSelected').value = '';
		document.getElementById('regionMenu').selectedIndex = 0;
	} else if (which == "region") {
		document.getElementById('zipSelected').value = '';
		document.getElementById('stateMenu').selectedIndex = 0;
	} else if (which == "zip") {
		document.getElementById('stateMenu').selectedIndex = 0;
		document.getElementById('regionMenu').selectedIndex = 0;
	}
}

function setCat(which) {
	if (which == "cat") {
		document.getElementById('subcatMenu').selectedIndex = 0;
	} else if (which == "subcat") {
		document.getElementById('catMenu').selectedIndex = 0;
	}
}

function validateMemberRegister() {
	var screename = document.getElementById('screenName').value;
	var email = document.getElementById('corpEmail').value;
	var zip = document.getElementById('corpZip').value;
	var password = document.getElementById('password').value;
	var cpassword = document.getElementById('confirm_pass').value;
	var msg = '';
	if (screename.length == 0) msg = msg + "A screen name is required\n";
	if (email.length == 0) msg = msg + "An email is required\n";
	if (zip.length == 0) msg = msg + "A zip code is required\n";
	if (password.length < 6) msg = msg + "A of at least 6 characters is required\n";
	else if (password != cpassword) 
			msg = msg + "The password and confirm password do not match\n";
			
	if (msg.length == 0) return true;
	else {
		alert(msg);
		return false;
	}
}