////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
    var xmlHttp; 
    var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
    var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
    var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
    //netscape, safari, mozilla behave the same??? 
    var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;  

////////////AutoTab////////////////////////////////////////////////
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
		input.form[(getIndex(input)+1) % input.form.length].select();
}
function containsElement(arr, ele) {
var found = false, index = 0;

while(!found && index < arr.length)
if(arr[index] == ele)
	found = true;
else
	index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
return index;
}
return true;
}
///////////////////////////////////////////////////////////////////

////////////Toggle Layer - hide/unhide loader etc////////////////////////////////////////////////
function toggleLayer(whichLayer)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display == "none" ? "":"none";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display == "none" ? "":"none";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display == "none" ? "":"none";
	}
}
///////////////////////////////////////////////////////////////////

////////////Window openers////////////////////////////////////////////////
function openWin(thrURL, vW, vH) {
	var w
	if (w) {
		w.location = thrURL
	} else {
		w= eval("wi" + "ndow.o" + "pen(thrURL, 'iMedia', 'width='+vW+', height='+vH+', scrollbars=0,status=0,toolbar0,resizable=0')");
	
		if (window.focus) {
			w.focus()
		}
		return false;
	}
}

function openWinFull(thrURL) {
	var w
	var maxW = (screen.availWidth-8), maxH = (screen.availHeight-30);
	if (w) {
		w.location = thrURL
	} else {
		w= eval("wi" + "ndow.o" + "pen(thrURL, 'iMedia', 'width='+maxW+', height='+maxH+', scrollbars=yes,status=no,toolbar=no,resizable=yes')");
	
		if (window.focus) {
			w.focus()
		}
		return false;
	}
}
///////////////////////////////////////////////////////////////////


////////////Show divs////////////////////////////////////////////////
function showCal(whichDiv) {
	if ( whichDiv.style.display == "none")
		whichDiv.style.display = "";
	else
		whichDiv.style.display = "none";
}
///////////////////////////////////////////////////////////////////


/////Save checked items in array for link collation - to be pasted in client email body
var myEmails = new Array();

function AddForEmail(RecID) {
    
    if (myEmails.length > 0) {
        
        var sAdding = new Boolean(false);
        
        for (i=0;i<myEmails.length;i++)
        {
            if (myEmails[i] == RecID) {
                myEmails.splice(i,1); // need to test this
                sAdding = false;
                //alert("Removing at:" + (i) + "--->" + RecID);
                break;
            } else {
                sAdding = true;
            } 
        }
        if (sAdding == true) {
            myEmails.push(RecID);
            //alert("Addin at:" + (myEmails.length) + "--->" + RecID);
        }
    }
    else {
        if (myEmails[0] == RecID) {
            myEmails.splice(0,1);
            //alert("Removing at:" + (0) + "--->" + RecID);
        
        } else {
            myEmails[0] = RecID;
            //alert("Addin at:0--->" + RecID);
        }
    }
}

function DisplayForEmail() {
    var strIDs = new String();
    
    if (myEmails.length<1) {
        alert("Please select some items before clicking the Email button. You can do this by ticking the checkboxes next to the envelope icons.");
    } else {
        for (i=0;i<myEmails.length;i++)
        {
            if (strIDs !== null) {
                strIDs = strIDs.concat(myEmails[i] + "%0D%0A")
            }
        }    
        document.location.href="mailto:?body=" + strIDs
    }
    
}
function fnClickOK(sender, e) { 
    __doPostBack(sender,e); 
}
///////////////////////////////////////////////////////////

////////////Count text field chars////////////////////////////////////////////////
function textCounter(field,cntfield,maxlimit) {
		if (field.value.length > maxlimit) // if too long...trim it!
			field.value = field.value.substring(0, maxlimit);
			// otherwise, update 'characters left' counter
		else
			cntfield.value = maxlimit - field.value.length;
}
///////////////////////////////////////////////////////////////////


////////////Confirm clip purchase////////////////////////////////////////////////
function confirmBuy(ClipID, UserID, lkl) {
    if (document.getElementById(lkl).Id != "_fin_")
    {
      confirmation = confirm("Please confirm that you wish to view this chargeable item.");
      if (confirmation) 
      {
        document.getElementById(lkl).src = "images/Icons/KonicaQEZ.gif";
        document.getElementById(lkl).Id = "_fin_";
        $.post("buyClip.aspx", { UID: UserID, ID: ClipID } );
        return
      } else {
       return false;
      }
		}
}

function confirmBuyBrdCst(ClipID, UserID, lkl, tvLink) {
    if (document.getElementById(lkl).Id != "_fin_")
    {
      confirmation = confirm("Please confirm that you wish to view this chargeable item.");
      if (confirmation) 
      {
        document.getElementById(lkl).src = "images/Icons/Camera4.gif";
        document.getElementById(lkl).Id = "_fin_";
        $.post("buyClip.aspx", { UID: UserID, ID: ClipID } );
        openWin(tvLink, 885,820);
        return
      } else {
       return false;
      }
    } else {
        openWin(tvLink, 885,820);
		}
}

function confirmBuyRadio(ClipID, UserID, lkl, radioLink) {
    if (document.getElementById(lkl).Id != "_fin_")
    {
      confirmation = confirm("Please confirm that you wish to view this chargeable item.");
      if (confirmation) 
      {
        document.getElementById(lkl).src = "images/Icons/sound.gif";
        document.getElementById(lkl).Id = "_fin_";
        $.post("buyClip.aspx", { UID: UserID, ID: ClipID } );
        openWin(radioLink, 650,250);
        return
      } else {
       return false;
      }
		} else {
        openWin(radioLink, 650,250);
    }
}

function confirmBuyExt(ClipID, UserID) {
    confirmation = confirm("Please confirm that you wish to view this chargeable item.");
    if (confirmation) 
    {
      $.post("http://www.intelligentmedia.com/iMedia/buyClip.aspx", { UID: UserID, ID: ClipID } );
      location.reload(true);
      return
    } else {
     return false;
    }
}

function flagItem(RecID, cbxState, UserID, divImage) {
                       $('#jQLoader').html('<p><img src="http://www.intelligentmedia.com/images/genloader.gif" /></p>');
                       $.post("http://www.intelligentmedia.com/iMedia/flagItem.aspx", { ID: RecID, UID: UserID, State: cbxState }, function(data) { $('#jQLoader').html('<p></p>'); });
    if (cbxState) {
      $('#'+divImage).html('<b><img src="images/flag.gif" /></b>');
    } else {
      $('#'+divImage).html('<b></b>');
    }
}

function CancelPostBack() {
    var objMan = Sys.WebForms.PageRequestManager.getInstance();
    if (objMan.get_isInAsyncPostBack())
    {
        objMan.abortPostBack();
    }
}


function loadDetails(sender, arrSelectedRecords, primaryKeyColumn) {
  var record = arrSelectedRecords[0];

  var rowIndex = 0;
  var primaryKeyValue = arrSelectedRecords[0][primaryKeyColumn];
  for (var i = 0; i < sender.Rows.length; i++) {
    if (sender.Rows[i] && primaryKeyValue == sender.Rows[i].Cells[primaryKeyColumn].Value) {
      rowIndex = i;
      break;
    }
  }

  var row = sender.GridBodyContainer.firstChild.firstChild.childNodes[1].childNodes[rowIndex];
  var images = row.getElementsByTagName('IMG');
  for (var i = 0; i < images.length; i++) {
    if (images[i].parentNode.className == 'ob_gDGEB') {
      images[i].onclick();
        break;
    }
  }
}

///////////////////////////////////////////////////////////////////
