var link_nohandler= window.clientInformation ? null : window.undefined;

function link_down() {
    this.onfocus= this.blur;
}

function link_up() {
    this.onfocus= link_nohandler;
}

function link_bind() {
    for (var i= document.links.length; i-->0;) {
        document.links[i].onmousedown= link_down;
        document.links[i].onmouseup= link_up;
    }
}

function changeContent(id, content) {
	// alert(id + ' ' + content);
	if ( content ) {
		document.getElementById(id).innerHTML = content;
	}
}

function showmap(id, link) {
	document.getElementById(id).style.display = 'block';
	
	if ( document.getElementById(id).style.left == '0px' ) {
		document.getElementById(link).innerHTML = 'View map of our location';
		document.getElementById(id).style.left = '-5000px';
		document.getElementById(id).style.display = 'none';
	} else {
		document.getElementById(link).innerHTML = 'Hide map';
		document.getElementById(id).style.left = '0px';
		document.getElementById(id).style.position = 'relative';
	}

}

function removeFromShortlist(id)
{
	var divid = 'sh_' + id;

	document.getElementById(divid).style.display = 'none';
	
	$("#data").load("http://www.primmerolds.co.uk/contact/removeFromShortlist/", {  theid: id })
	
	// window.location.href = "http://www.primmerolds.co.uk/contact/removeFromShortlist/" + id + '/'
}

function updateImageInDB(itemid,imageid,uid,pageid) {
	
	// alert(itemid + ' ' + imageid + ' ' + uid + ' ' + pageid);
	
	src = 'http://www.primmerolds.co.uk/public/images/pages/photo_'  + imageid + '.jpg';
	id = 'photo_' + itemid;
	

	changeImgSrc(id,src);

	jQuery(document).trigger('close.facebox');

	window.location.href = "http://www.primmerolds.co.uk/admin/updateImageInDB/" + itemid + '/' + imageid + '/' + uid + '/' + pageid;
	
	// $.post
	// $("#data").load("http://www.primmerolds.co.uk/admin/updateImageInDB/" + itemid + '/' + imageid + '/' + uid + '/' + pageid);
	
	// $.post("http://www.primmerolds.co.uk/public/includes/updateImageInDB.php?itemid=" . itemid . '&imageid=' . imageid);
}

function clearLibraryImage(id) {
	
	// alert(id );
		
	// alert('start: ' + id);
	
	if ( $("#data").load("http://www.primmerolds.co.uk/includes/clearLibraryImage.php", {  theid: id }) ) {
		window.location.reload();
	}

	// window.location.reload();
	// alert('end: ' + id);

}

function clearBackgroundImage(divid, id) {
	
	// alert(id );
	
	document.getElementById(divid).style.background = "";
	
	// alert('start: ' + id);
	
	$("#data").load("http://www.primmerolds.co.uk/includes/clearBackgroundImage.php", {  theid: id });

	// window.location.reload();
	// alert('end: ' + id);

}

function getreq() { // returns false if exists
	if(window.ActiveXObject) { // if IE
		try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
		return new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
		return;
			}
		}
	} else if(window.XMLHttpRequest) { // if Mozilla, Safari, etc.
		return new XMLHttpRequest();
		}
}

function testExists(imagepath) {
   	// alert( imagepath );
       
    req = getreq();
    req.onreadystatechange = imagexists;
	req.open("GET", imagepath, false);
    req.send(null);  
    
	if(req.status == 200) {	// image exists
		nB = true;
	}
	else if(req.status==404) {	// image doesn't exist
		nB = false;
	}
	else {
		// all other error codes will set this
		 nB = null;
	}
	
	// alert('test here ' + nB);
	
	return nB ;
    
    // return req.onreadystatechange;   
}

function imagexists(){
	
	// alert('new test here ' + req.readyState );
	
	if(req.readyState == 4) {

		// alert('test here ' + req.status);
		
		if(req.status == 200) {	// image exists
			nB = true;
		}
		else if(req.status==404) {	// image doesn't exist
			nB = false;
		}
		else {
			// all other error codes will set this
			 nB = null;
		}
		
		return nB ;
		
	}
}

function openURL( form ) { 

	// alert(form);
	
	var newIndex = form.our_services.selectedIndex; 

	if ( newIndex == 0 ) { 

		alert( "Please select a menu item!" ); 

	} else { 

		cururl = form.our_services.options[ newIndex ].value; 

		window.location.assign( cururl ); 

	} 

}

function changeImgSrc(id,src) {
	// alert(id + ' ' + src);
	
	var el = document.getElementById(id);
	if (el) el.setAttribute('src',src);
}

