﻿// JScript File
var imageContainer;
var currentCommentsDivId;

function imagesForward() {
    
    var currentImage = document.getElementById('currentImage').value;
    if(parseInt(currentImage) < 15)
    {
        //alert("###Current image value is: " + currentImage);
	    for(var i = parseInt(currentImage); i > (parseInt(currentImage) - 3); i--)
	    {
		
		    idToHide = 'image' + i; 
		    document.getElementById(idToHide).style.display = 'none';	
		    //alert("idToHide is: " + idToHide);
	    }
	    for(var ix = (parseInt(currentImage) + 1); ix <= (parseInt(currentImage) + 3); ix++)
	    {
	    	idToShow = 'image' + ix;
	    	document.getElementById(idToShow).style.display = 'block';
	    	//alert("idToShow is: " + idToShow);
	    }
	    currentImage = parseInt(currentImage) + 3;
	    document.getElementById('currentImage').value = currentImage;
	    //alert("*NOW* Current image value is: " + currentImage);
    }
	
}

function imagesBackward()
{
        var currentImage = document.getElementById('currentImage').value;
        //alert("Current image value is: " + currentImage);
        if(parseInt(currentImage) > 3)
        {
            for(var ix = parseInt(currentImage); ix > (parseInt(currentImage) - 3); ix--)
            {
                    idToHide = 'image' + ix;
                    document.getElementById(idToHide).style.display = 'none';
            }

            for(var i = (parseInt(currentImage) - 5); i <= (parseInt(currentImage) - 3); i++)
            {
                   idToShow = 'image' + i;
                   document.getElementById(idToShow).style.display = 'block';
            }
            currentImage = parseInt(currentImage) - 3;
            document.getElementById('currentImage').value = currentImage;
            //alert("*NOW* Current image value is: " + currentImage);
        }
}


function showImage(imageSrc, imageName, imageContainerId, imageId, comment) {
    if(typeof imageContainer == 'undefined')
    {
    } else {
        document.getElementById(imageContainer).style.backgroundImage = '';
    }
    
    imageContainer = 'image' + imageContainerId;
    //document.getElementById(imageContainer).style.backgroundColor = '#f3f3f3';
    document.getElementById(imageContainer).style.backgroundImage = "url('../Images/imageDivBg_140x120.jpg')";
    document.getElementById('selectedImage').value = imageName;
    document.getElementById("largeImage").src = imageSrc; 
    document.getElementById("largeImage").style.display = 'block'; 
    document.getElementById("fullSizeImageDiv").style.display = 'block';
    document.getElementById("imageCommentsDiv").style.display = 'none'; 
    document.getElementById("selectedImageId").value = imageId;
    document.getElementById("imageCommentTextArea").value = unescape(comment);
    document.getElementById("updateResult").innerText = '';
}
//******************************************************************//
//ctl00_Main_PropertyImage - I hope this works all the time...      //
//******************************************************************//
//
// - at this time we pass in some things that don't get used anymore...
// - this needs to be cleaned up. 
//
function showImageSearchPage(imageSrc, imageName, imageContainerId, imageId, comment) {
    if(typeof imageContainer == 'undefined')
    {
        
    } else {
        document.getElementById(imageContainer).style.backgroundColor = '#fff';
    }
    
    if(typeof currentCommentsDivId == 'undefined')
    {  
         //alert("comment label is undef...");
         //If the comment label variable is undef then we know they haven't clicked anything in 
         //the image slider bar yet so set the first comment's display property to none. 
         document.getElementById('ctl00_Main_CommentLabel1').style.display = 'none';
    } else {
        //alert("Right now we are showing: " + currentCommentsDivId);
        document.getElementById(currentCommentsDivId).style.display = 'none';
    }
    currentCommentsDivId = 'ctl00_Main_CommentLabel' + imageContainerId;
    //alert("we are about to show: " + currentCommentsDivId);
    //alert(document.getElementById(currentCommentsDivId).innerText);
    
    imageContainer = 'image' + imageContainerId;
    document.getElementById(imageContainer).style.backgroundColor = '#b3aeab';
    document.getElementById('selectedImage').value = imageName;
    //document.getElementById("ctl00_Main_PropertyImage").src = '/' + imageSrc; 
    //document.getElementById("ctl00_Main_PropertyImage").src = imageSrc; 

    if(imageSrc.match("^http"))
    {
        document.getElementById("ctl00_Main_PropertyImage").src =  imageSrc; 
    } else {
        //alert(imageSrc);
        document.getElementById("ctl00_Main_PropertyImage").src = '/' + imageSrc; 
        document.getElementById("ctl00_Main_PropertyImage").src = imageSrc; 
        //alert(document.getElementById("ctl00_Main_PropertyImage").src);
    }
    document.getElementById(currentCommentsDivId).style.display = 'block'; 
    document.getElementById("selectedImageId").value = imageId;
}

function showImageVendorDetails(imageSrc, imageName, imageContainerId, imageId, comment) {
    if(typeof imageContainer == 'undefined')
    {
        //alert("Couldn't get the container ID");   
    } else {
        document.getElementById(imageContainer).style.backgroundColor = '#fff';
    }
    
    if(typeof currentCommentsDivId == 'undefined')
    {  
         //alert("comment label is undef...");
         //If the comment label variable is undef then we know they haven't clicked anything in 
         //the image slider bar yet so set the first comment's display property to none. 
         document.getElementById('ctl00_Main_CommentLabel1').style.display = 'none';
    } else {
        //alert("Right now we are showing: " + currentCommentsDivId);
        document.getElementById(currentCommentsDivId).style.display = 'none';
    }
    currentCommentsDivId = 'ctl00_Main_CommentLabel' + imageContainerId;
    //alert("we are about to show: " + currentCommentsDivId);
    //alert(document.getElementById(currentCommentsDivId).innerText);
    
    imageContainer = 'image' + imageContainerId;
    document.getElementById(imageContainer).style.backgroundColor = '#b3aeab';
    document.getElementById('selectedImage').value = imageName;
    document.getElementById("ctl00_Main_PropertyImage").src = imageSrc; 
    //alert("I set ctl00_Main_PropertyImage.src to: " + imageSrc);
    document.getElementById(currentCommentsDivId).style.display = 'block'; 
    document.getElementById("selectedImageId").value = imageId;
}

function showCommentsDiv(e)
{
    document.getElementById("imageCommentsDiv").style.display = 'block'; 
    
}

function showHidePropertyDetails(propertyId) {
    //alert("Div to show is: " + propertyId);
    var detailsDiv = "propDetailsDiv_" + propertyId;
    
    if (document.getElementById(detailsDiv).style.display == "none") {
        document.getElementById(detailsDiv).style.display = "block";
        
    } else {
        document.getElementById(detailsDiv).style.display = "none";
    }
}

function showCurrentCommentsDiv(comment)
{
//    alert(commentDivId);
//    if(typeof currentCommentsDivId == 'undefined')
//    {
//        
//    } else {
//        alert("Old container id: " + currentCommentsDivId);
//        document.getElementById(currentCommentsDivId).display = "none";
//    }
//    document.getElementById(commentDivId).style.display = "block";
//    currentCommentsDivId = commentDivId;
    
}

function UpdateImageCommentajaxFunction()
{
    
    document.getElementById("ajaxLoadingImage").style.display = "block";
    var comment = document.getElementById("imageCommentTextArea").value
    var imageId = document.getElementById("selectedImageId").value;
    var xmlHttp;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById("updateResult").innerText = xmlHttp.responseText;
            document.getElementById("ajaxLoadingImage").style.display = "none";
            document.getElementById("updateResult").style.color = "#c3c3c3";
        }
    }
    htmlEncodedComment = htmlEncode(comment, false, 0);
    //xmlHttp.open("GET","UpdateImageComment.aspx?imageId=" + imageId + "&comment=" + htmlEncode(comment, true, 0),true);
    xmlHttp.open("GET","UpdateImageComment.aspx?imageId=" + imageId + "&comment=" + escape(comment),true);
    xmlHttp.send(null);
}

function deleteImageAjaxFunction()
{
    var imageId = document.getElementById("selectedImageId").value;
    var xmlHttp;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById("updateResult").innerText = xmlHttp.responseText;
            document.getElementById("ajaxLoadingImage").style.display = "none";
            document.getElementById("updateResult").style.color = "#fff";
            document.getElementById("largeImage").src = "../Images/imageDeleted.png";
            document.getElementById("thumbNail_" + imageId).src = "../Images/imageDeleted.png";
            document.getElementById("thumbNailLink_" + imageId).onclick = function() {alert("Sorry - this image hase been deleted")};
        }
    }
    xmlHttp.open("GET","DeletePropertyImage.aspx?imageId=" + imageId,true);
    xmlHttp.send(null);
}
  
function GetImageCommentajaxFunction()
{
    
    document.getElementById("ajaxLoadingImage").style.display = "block";
    var imageId = document.getElementById("selectedImageId").value;
    var xmlHttp;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            //document.myForm.time.value=xmlHttp.responseText;
            document.getElementById("updateResult").innerText = xmlHttp.responseText;
            document.getElementById("ajaxLoadingImage").style.display = "none";
            //document.getElementById("updateResult").innerText = "Your comment has been added.";
            document.getElementById("updateResult").style.color = "#c3c3c3";
        }
    }
    xmlHttp.open("GET","GetImageComment.aspx?imageId=" + imageId,true);
    xmlHttp.send(null);
}
  
function showComments(msg,e)
{
	var tempX = 0;
	var tempY = 0;
	var IE = document.all?true:false

	if (IE) 
	{ 
		//alert('IE');
    		tempX = event.clientX + document.body.scrollLeft
    		tempY = event.clientY + document.body.scrollTop
  	} else { 
		//alert('Firefox - Not IE');
    		tempX = e.pageX
    		tempY = e.pageY
  	} 	

	messageDiv = document.getElementById("msgDiv");
	messageDivBody = document.getElementById("msgDivBody");
	messageDiv.style.visibility="visible";
	messageDiv.style.top = (tempY + 10) + 'px'; 
	messageDiv.style.left = (tempX - 10) + 'px';
	messageDivBody.innerHTML = "<br />&nbsp;&nbsp;&nbsp;&nbsp;This is a test<br />This is only a test.<br /><br />Look how cool, we have this long line of text and we want to see what it does!";
	
}

function hide()
{
	messageDiv = document.getElementById("msgDiv");
	messageDiv.style.visibility='hidden';
}

//function urlDecode(utftext) {
//		var string = "";
//		var i = 0;
//		var c = c1 = c2 = 0;
// 
//		while ( i < utftext.length ) {
// 
//			c = utftext.charCodeAt(i);
// 
//			if (c < 128) {
//				string += String.fromCharCode(c);
//				i++;
//			}
//			else if((c > 191) && (c < 224)) {
//				c2 = utftext.charCodeAt(i+1);
//				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
//				i += 2;
//			}
//			else {
//				c2 = utftext.charCodeAt(i+1);
//				c3 = utftext.charCodeAt(i+2);
//				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
//				i += 3;
//			}
// 
//		}
// 
//		return string;
//	}


//function URLEncode(url) //Function to encode URL.
//{
//    // The Javascript escape and unescape functions do not correspond
//    // with what browsers actually do...
//    var SAFECHARS = "0123456789" + // Numeric
//    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
//    "abcdefghijklmnopqrstuvwxyz" +
//    "-_.!~*'()"; // RFC2396 Mark characters
//    var HEX = "0123456789ABCDEF";

//    var plaintext = url;
//    var encoded = "";
//    for (var i = 0; i < plaintext.length; i++ ) {
//        var ch = plaintext.charAt(i);
//        if (ch == " ") {
//            encoded += "+"; // x-www-urlencoded, rather than %20
//        } else if (SAFECHARS.indexOf(ch) != -1) {
//            encoded += ch;
//        } else {
//            var charCode = ch.charCodeAt(0);
//            if (charCode > 255) {
//                alert( "Unicode Character '"
//                + ch
//                + "' cannot be encoded using standard URL encoding.\n" +
//                "(URL encoding only supports 8-bit characters.)\n" +
//                "A space (+) will be substituted." );
//                encoded += "+";
//            } else {
//                encoded += "%";
//                encoded += HEX.charAt((charCode >> 4) & 0xF);
//                encoded += HEX.charAt(charCode & 0xF);
//            }
//        }
//    }

//    return encoded;
//};


// 

//function URLDecode(url) //function decode URL
//{
//        // Replace + with ' '
//        // Replace %xx with equivalent character
//        // Put [ERROR] in output if %xx is invalid.
//        var HEXCHARS = "0123456789ABCDEFabcdef";
//        var encoded = url;
//        var plaintext = "";
//        var i = 0;
//        while (i < encoded.length) {
//        var ch = encoded.charAt(i);
//        if (ch == "+") {
//            plaintext += " ";
//            i++;
//        } else if (ch == "%") {
//            if (i < (encoded.length-2)
//             && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
//             && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
//                plaintext += unescape( encoded.substr(i,3) );
//                i += 3;
//        } else {
//            alert( 'Bad escape combination near ...' + encoded.substr(i) );
//            plaintext += "%[ERROR]";
//            i++;
//        }
//            } else {
//                plaintext += ch;
//                i++;
//            }
//        } // while

//    return plaintext;
//}; 

function htmlEncode(source, display, tabs)
{
	function special(source)
	{
		var result = '';
		for (var i = 0; i < source.length; i++)
		{
			var c = source.charAt(i);
			if (c < ' ' || c > '~')
			{
				c = '';
			}
			result += c;
		}
		return result;
	}
	
	function format(source)
	{
		// Use only integer part of tabs, and default to 4
		tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
		
		// split along line breaks
		var lines = source.split(/\r\n|\r|\n/);
		
		// expand tabs
		for (var i = 0; i < lines.length; i++)
		{
			var line = lines[i];
			var newLine = '';
			for (var p = 0; p < line.length; p++)
			{
				var c = line.charAt(p);
				if (c === '\t')
				{
					var spaces = tabs - (newLine.length % tabs);
					for (var s = 0; s < spaces; s++)
					{
						newLine += ' ';
					}
				}
				else
				{
					newLine += c;
				}
			}
			// If a line starts or ends with a space, it evaporates in html
			// unless it's an nbsp.
			newLine = newLine.replace(/(^ )|( $)/g, '+');
			lines[i] = newLine;
		}
		
		// re-join lines
		var result = lines.join('<br />');
		
		// break up contiguous blocks of spaces with non-breaking spaces
		result = result.replace(/  /g, '+');
		
		// tada!
		return result;
	}

	var result = source;
	
	// ampersands (&)
	result = result.replace(/\&/g,'');

	// less-thans (<)
	result = result.replace(/\</g,'');

	// greater-thans (>)
	result = result.replace(/\>/g,'');
	
	result = result.replace(/\s/g,'+');
	
	if (display)
	{
		// format for display
		result = format(result);
	}
	else
	{
		// Replace quotes if it isn't for display,
		// since it's probably going in an html attribute.
		result = result.replace(new RegExp('"','g'), '');
	}

	// special characters
	alert("before we call special: " + result);
	result = special(result);
	alert("After we call special: " + result);
	// tada!


	return escape(result);
}

