// Declare Global Variables
var platform = navigator.platform.substr(0,3);
var browser = navigator.appName;
var shortVersion = navigator.appVersion.substr(0,1);
var longVersion = navigator.appVersion;


// Variables needed for Rollovers
var over = 'no';
var whichOne = ' ';
var styleSheetElement;
var oldElement;

// Find out if we need to serve the live site or the dev site
str = document.location + "";
re = /dev/i; 
if(str.search(re) != -1) {
	var whichEnv = '/dev/';
} else {
	var whichEnv = '/';
}

// Function to Assign the relative path to images and links
if (whichLevel == 0) {
	var path = '';
} else if (whichLevel == 1) {
	var path = '../';
} else if (whichLevel == 2) {
	var path = '../../';
}

// Function to fix png transparency in browsers that don't support it
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	document.writeln('<style type="text/css">img, input.image { visibility:hidden; } </style>');
	window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			fnFixPng(img);
			img.attachEvent("onpropertychange", fnPropertyChanged);
		}
		img.style.visibility = "visible";
	}

	var nl = document.getElementsByTagName("INPUT");
	for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
		if (e.className && e.className.match(/\bimage\b/i) != null) {
			if (e.src.match(/\.png$/i) != null) {
				fnFixPng(e);
				e.attachEvent("onpropertychange", fnPropertyChanged);
			}
			e.style.visibility = "visible";
		}
	}
}

function fnPropertyChanged() {
	if (window.event.propertyName == "src") {
		var el = window.event.srcElement;
		if (!el.src.match(/x\.gif$/i)) {
			el.filters.item(0).src = el.src;
			el.src = "images/x.gif";
		}
	}
}

function dbg(o) {
	var s = "";
	var i = 0;
	for (var p in o) {
		s += p + ": " + o[p] + "\n";
		if (++i % 10 == 0) {
			alert(s);
			s = "";
		}
	}
	alert(s);
}

function fnFixPng(img) {
	var src = img.src;
	img.style.width = img.width + "px";
	img.style.height = img.height + "px";
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
	img.src = "images/x.gif";
}

function randOrd(){
return (Math.round(Math.random())-0.5); }



function toggle_visibility(id) {
var e = document.getElementById(id);
if(id == 'pic') {
	e.innerHTML = contents;
	show_photo(1);
}
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}

function close_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'none';
}

function show_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'block';
else
e.style.display = 'block';
}

function show_photo(id) {
var e = document.getElementById('largePhoto');
var photoTag = '<input type="image" src="' + picArray[id] + '" />';
e.innerHTML = photoTag;


}

function toggle_enlarge(id) {

	var enlarge = document.getElementById(id);
	if(state == "off") {
		enlarge.innerHTML = contents;
		show_photo(1);
		enlarge.style.display = "block";
		state = "on";
	} else {
		enlarge.style.display = "none";
		state = "off";
	}
}

// Ajax Variables
var request;
var dest;
var totalImages = 0;


// Function to load photo manager file
function loadPhotoManager(id) {
	
	var contentElement = document.getElementById('reassignLead');
	var URL = whichEnv + 'photo_manager.php?id=' + id;
	loadHTML(URL, 'photoManager');
}

// Function to load upload iFrame
function loadUploadFrame(id) {
	// alert('called');
	var iframe = document.getElementById('uploadFrame');
	iframe.src = 'upload.php?id=' + id;
}

// Function to remove photo from photo manager
function removePhoto(ad,id) {
	var contentElement = document.getElementById('reassignLead');
	var URL = whichEnv + 'photo_manager.php?id=' + ad + '&photoId=' + id + '&action=remPhoto';
	loadHTML(URL, 'photoManager');
	// alert(totalImages);
	if(totalImages == 3) {
		--totalImages;
		// reload upload iFrame
		setTimeout('loadUploadFrame(' + ad + ')', 1000);
	} else {
		--totalImages;
	} 
	
}

// Function to process state changes
function processStateChange(){
    if (request.readyState == 4){
        contentDiv = document.getElementById(dest);
        if (request.status == 200){
            response = request.responseText;
            contentDiv.innerHTML = response;
        }  else {
            contentDiv.innerHTML = "Error: Status "+request.status;
        }
    }
}

// Function to load new page with ajax
function loadHTML(URL, destination){
    dest = destination;
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
        request.onreadystatechange = processStateChange;
        request.open("GET", URL, true);
        request.send(null);
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
            request.onreadystatechange = processStateChange;
            request.open("GET", URL, true);
            request.send();
        }
    }
}

// Function to request confirmation and delete an ad
function deleteAd(formName) {
	if (confirm("Are you sure you want to delete your ad?")) { 
 		// If yes, submit form
 		eval('document.delete' + formName + '.submit()');
	}
}

// Function to request confirmation and renew an ad
function renewAd(formName) {
	if (confirm("Are you sure you want to renew your ad?")) { 
 		// If yes, submit form
 		eval('document.renew' + formName + '.submit()');
	}
}

// Function to request confirmation and delete an ad
function deleteAccount(formName) {
	if (confirm("Are you sure you want to delete this user?")) { 
 		// If yes, submit form
 		eval('document.delete' + formName + '.submit()');
	}
}
