// Intellectual property of FreshStyle Web and Media 2008. www.FreshStyle.com.au

function IsNumeric(strString)
//  check for valid numeric strings	
{
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function ClearField(thisField, strClearText)		// Clear the field by grabing the object calling this function and its default value to be cleared
{
	if (thisField.value == strClearText)
	{
		thisField.value = ""; thisField.focus();
	}
}

function refreshTable(strTable)
{
    var table = document.getElementById(strTable);
    var tableDnD = new TableDnD();
    // Redefine the findDropTargetRow to put some debug information in
    tableDnD.findDropTargetRow = function(y) {
        var rows = this.table.tBodies[0].rows;
	    var debugStr = "y is "+y+"<br>";
        for (var i=0; i<rows.length; i++) {
            var row = rows[i];
            var rowY    = this.getPosition(row).y;
            var rowHeight = parseInt(row.offsetHeight)/2;
		    if (row.offsetHeight == 0) {
			    rowY = this.getPosition(row.firstChild).y;
			    rowHeight = parseInt(row.firstChild.offsetHeight)/2;
		    }
		    debugStr += "row["+i+"] between "+(rowY-rowHeight)+' and '+(rowY+rowHeight)+"<br>";
            // Because we always have to insert before, we need to offset the height a bit
            if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
                // that's the row we're over
			    //document.getElementById('debug').innerHTML = debugStr+"found row";
                return row;
            }
        }
	    //document.getElementById('debug').innerHTML = debugStr+"no matching row";
        return null;
    }
    // Redefine the onDrop so that we can display something
    tableDnD.onDrop = function(table, row) {
        //strQIDs = '||';     // this will rewrite the ID list from sorting order instead so when building the list, its ordered correctly
    	if (table.id == 'tblImages')
	    {
	        strImages = '||';
	    }
	    
        var rows = this.table.tBodies[0].rows;

        for (var i=0; i<rows.length; i++) {
            //strQIDs += rows[i].id.replace('trQuestionAdded_', '') + '||';
            //document.forms.ppForm.QuestionIDs.value = strQIDs;
		    //debugStr += rows[i].id+" ";
		    if (table.id == 'tblImages')
		    {
                strImages += rows[i].id.replace('trDynamicImage_', '') + '||';
		    }
	    }
	    //document.getElementById('debug').innerHTML = 'row['+row.id+'] dropped<br>'+debugStr;
    }
    tableDnD.init(table);
}

function sortSet(strTable)
{
    var objTable; var objTR;

    // first off kill the current table for sorting
    if (strCurrentTableForSorting != '')
    {
        objTable = document.getElementById(strCurrentTableForSorting);
        objTable.style.border = '1px solid transparent';
        objTable.style.cursor = 'pointer';
        
        for (var i = 0; i < objTable.rows.length; i++)
        {
            objTable.rows[i].style.cursor = 'default';
        }
    }
    
    // then get the new table for sorting
    objTable = document.getElementById(strTable);
    
    strCurrentTableForSorting = strTable;
    
    objTable.style.border = '1px dashed #CCCCCC';
    
    refreshTable(strTable);
}

function setParent (strTable, strValueHolder, strRowID, intValue)
{
    var objTable = document.getElementById (strTable);
    
    eval('document.forms[0].' + strValueHolder + '.value = intValue');
    
    for (var i = 0; i < objTable.rows.length; i++)
    {
        if (objTable.rows[i].id == strRowID + intValue)
        {
            objTable.rows[i].style.backgroundColor = '#EFEFEF';
            document.getElementById ('spanParentLevel').innerHTML = ARParentLevels[intValue];
        }
        else
        {
            objTable.rows[i].style.backgroundColor = '#FFFFFF';
        }
    }
}

function addImage(strImage)
{
    if (strImages.indexOf('||' + strImage + '||') > -1)
    {
        //alert("This Image has already been assigned!");
        return;
    }

    strImages += strImage + '||';

    try { document.getElementById('Image_' + strImage).style.backgroundColor = '#f9f7c4'; }
    catch (err) { }

    buildImages();
}

function delImage(strImage)
{
    //var ARImages = strImages.split('||');

    strImages = strImages.replace('||' + strImage + '||', '||');

    try { document.getElementById('Image_' + strImage).style.backgroundColor = '#FFFFFF'; }
    catch (err) { }

    buildImages();
}

function buildImages()
{
    var strList = '';
    var strListPanel = '';
    var ARImages = strImages.split('||');

    for (var i = 1; i < (ARImages.length - 1); i++)
    {
        strList = strList + '<tr id="trDynamicImage_' + ARImages[i] + '">';
        strList = strList + '   <td style="width: 1px; cursor: pointer; padding: 2px 10px 2px 0px;" onClick="delImage(\'' + ARImages[i] + '\');"><img src="' + strImagesPath + 'admin/icons/invalid.gif" border="0" alt="remove from selection"></td>';
        strList = strList + '   <td style="padding: 2px 10px 2px 0px;">' + ARImages[i] + '</td>';
        strList = strList + '   <td style="width: 1px; cursor: help; padding: 2px 0px 2px 0px;" onMouseOver="previewImage(\'' + ARImages[i] + '\', \'' + strSection + '\');"><img src="' + strImagesPath + 'admin/icons/preview.gif" border="0"></td>';
        strList = strList + '</tr>';
        try { document.getElementById('Image_' + ARImages[i]).style.backgroundColor = '#f9f7c4'; }
        catch (err) { }
    }

    // this is the panel which holds the Module rows... hence append list to this string (last line)
    strListPanel = strListPanel + '<table id="tblImages" width="233" cellpadding="0" cellspacing="0" border="0">' + strList + '</table>';

    document.getElementById('divImages').innerHTML = strListPanel;

    document.forms[0].Images.value = strImages;

    refreshTable('tblImages');
}

function previewImage(strImage, strType)
{
    var objDiv = document.getElementById ('divImagePreview');
    var objImage = document.getElementById ('imgPreview');
    var objSpan = document.getElementById('spanPreview');
    
    if (strImage == null || strImage == undefined)
    {
        objDiv.style.display = 'none';
    }
    else
    {
        objSpan.innerHTML = strImage;
        objImage.src = strSitePath + 'Resources/?W=250&H=150&File=' + strType + '/' + strImage;
        objDiv.style.display = 'block';
    }
}

function confirmDelete(strURL)
{
    var objConfirmation = confirm ('Are you sure you wish to delete this record?');
    if (objConfirmation == false)
    {
        return;
    }
    
    document.location = strURL;
}

function loadurl(dest, strAction) {
 try {
   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
  		new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch (e) { alert('Your browser does not support XMLHTTP.'); }
 
 xmlhttp.open("GET", dest);
 xmlhttp.send(null);
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function roundNumber(num, dec)
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function fadeInOut(strObject, dir, speed, c, strType)
{
    var obj = document.getElementById (strObject);
    //var c = 0;

    if(obj.filters)
    {
        obj.style.filter='alpha(opacity='+c+')';
    }
    else
    {
        obj.style.opacity=c/100;
    }

    if(dir=='in')
    { 
        dir1='in';
        if (c == 0) { obj.style.display = 'block'; }
        c++;
    } 
    else
    { 
        dir1='out';
        c--;
    }

    if(c>100)
    {
        c=100;
        // the line below fades out the notification
        if (strType == 'Notification') { setTimeout('fadeInOut(\'' + strObject + '\', \'out\', ' + speed + ', ' + c + ')', 2); }
        return;
    }

    if(c<0)
    {
        c=0;
        obj.style.display = 'none';
        return;
    }

    setTimeout('fadeInOut(\'' + strObject + '\', \'' + dir + '\', ' + speed + ', ' + c + ', \'' + strType + '\')', speed)
}
