$(document).ready(function() {
     $(".theIcon").draggable({
//	    start: function (event,ui) {
//			$(this).css('z-index',1000);
//		}
           helper: 'clone', zIndex: 1000,
		   start:  function (event, ui) {
//					  alert("startdragging");
				    }
 
	 });

	 $(".theIcon").hover(
		 function() {
		   $(this).addClass("theIconHlt");
		   },
		 function() {
		   $(this).removeClass("theIconHlt");
		   }
		);

	 $(".govIconTargetCont").droppable ({
			drop:  function( event, ui ) {
//				oldHtml = $( this ).html();
//				$( this).html("<br><br>" + oldHtml);
              theDroppedID = ui.draggable.attr("id");
                theDroppedID = theDroppedID.substring(4);
                theDroppedHtml = ui.draggable.html();
				// This is the img, accept only setlDirRnd 
				if (theDroppedHtml.search(/govDirRnd/i) >0)
				{
					theDroppedHtml = theDroppedHtml.replace(/govDirRnd/i,"govDir")
					theDroppedHtml = theDroppedHtml.replace(/\.png/i,"B.jpg")
					imgDesc = extractImgDtl(theDroppedHtml)
					$( this).html(theDroppedHtml);

					$("#tmp_setl").val(theDroppedID);
				
					if ($("#tmp_norm").val() > 0)
					{
						// call ajax to make the calculation, and put the return string
    		           $.post ("calcMemVal.asp",{numSetl: $("#numSetl").val() , setl: $("#tmp_setl").val(), norm:$("#tmp_norm").val() }, function (data) {parseResponseCalc(data);},"html");
					} else {
						// not setllment yet, just put the title below
                        $( "#selDescID").html (imgDesc);
					}
				}
			}
	 });

	 $(".normIconTargetCont").droppable ({
			drop:  function( event, ui ) {
//				oldHtml = $( this ).html();
//				$( this).html("<br><br>" + oldHtml);
              theDroppedID = ui.draggable.attr("id");
                theDroppedID = theDroppedID.substring(4);
                theDroppedHtml = ui.draggable.html();
				// This is the img, accept only setlDirRnd 
				if (theDroppedHtml.search(/normDirRnd/i) >0)
				{
					theDroppedHtml = theDroppedHtml.replace(/normDirRnd/i,"normDir")
					theDroppedHtml = theDroppedHtml.replace(/\.png/i,"B.jpg")
//						alert(theDroppedHtml);
					imgDesc = extractImgDtl(theDroppedHtml)
					$( this).html(theDroppedHtml);
                    
//						alert($("#tmp_setl").val() );
//						alert($("#tmp_norm").val() );
					$("#tmp_norm").val(theDroppedID);

					if ($("#tmp_setl").val() > 0)
					{
						// call ajax to make the calculation, and put the return string
    		           $.post ("calcMemVal.asp",{numSetl: $("#numSetl").val() , setl: $("#tmp_setl").val(), norm:$("#tmp_norm").val() }, function (data) {parseResponseCalc(data);},"html");

					} else {
						// not setllment yet, just put the title below
                        $( "#normDescID").html (imgDesc);
					}
					
				}
			}
	 });

});


function parseResponseCalc(data) {
//	alert(data);
		var bigList = data.split("^");
        setlDispPrice = bigList[0];
		costUsDispTop = bigList[2];
		$("#topNormNum").html(costUsDispTop);
		$("#topSetlNum").html($("#numSetl").val());
		setlImgHTML = $("#dropSetl").html();
		theSetlDesc = extractImgDtl(setlImgHTML);
		normImgHTML = $("#dropNorm").html();
		theNormDesc = extractImgDtl(normImgHTML);
		pureCostUs = bigList[2];
		pureCostUs = pureCostUs.replace(/\,/g,"");
		$("#tmp_costUs").val(pureCostUs)
		 $("#curURL").val("");
	

		settlementStr =  "<b>"+$("#numSetl").val()+"</b> " + theSetlDesc + "<br>(" + setlDispPrice + " ש&quot;ח ליחידת חישוב)";
		normStr =  "<b>"+bigList[1]+"</b> " + theNormDesc + "<br>(" + bigList[3] + " ש&quot;ח ליחידת חישוב)";
        $( "#selDescID").html (settlementStr);
			$( "#normDescID").html (normStr);
			
		if (bigList[4]) {
		// setlment comment
		   $("#setlCommentTitle").html("ממשלה");
		   $("#setlCommentText").html(bigList[4] + "<br><br>");
		}
		if (bigList[5]) {
		// setlment comment
		   $("#normCommentTitle").html("מוצרים");
		   $("#normCommentText").html(bigList[5] + "<br><br>");
		}
}


function extractImgDtl (imgStr)
{
			var myRegEx = /alt=\"[^\"]+\"/;
			var myRegExQuote = /alt=\'[^\']+\'/;
			var imgDesc = myRegEx.exec(imgStr);
			if (!imgDesc)
			{
				// explorer conversion of double quote to single quote
			   imgDesc = myRegExQuote.exec(theDroppedHtml);
			}
			imgDesc = String(imgDesc);
			imgDesc = imgDesc.substring(5,imgDesc.length-1);
			return imgDesc;
}

function numChanged() {
    if (($("#tmp_norm").val() > 0) && ($("#tmp_setl").val() > 0)) {
    		           $.post ("calcMemVal.asp",{numSetl: $("#numSetl").val() , setl: $("#tmp_setl").val(), norm:$("#tmp_norm").val() }, function (data) {parseResponseCalc(data);},"html");
	}
}

function activateAnim() {
 MM_openBrWindow("animCalcs.asp?normNum=" + $("#tmp_norm").val() + "&setlNum=" + $("#tmp_setl").val() + "&setlDir=gov&theRatio=" + $("#tmp_costUs").val(),'animCalcs','width=420,height=280')
}

 function genURL() {
	 var curURL = "http://www.zvuluny.co.il/memshalton.asp?numSetl=" + $("#numSetl").val() + "&setl=" + $("#tmp_setl").val() + 
		 "&norm=" + $("#tmp_norm").val();
	 $("#curURL").val(curURL);
 }
