// ***Java Script Library*** //

function _modDesc(targ,selObj,restore){
	var modVal = selObj.options[selObj.selectedIndex].id;
	target = document.getElementById('linkString');
	target.value = modVal;
}

// Confirm Delete 

function deleteItem(urlOut) {
 var where_to= confirm("Are you sure you want to delete this? - This action is permanent.");
 if (where_to== true)
 {
   window.location=urlOut;
 }
 else
 {
   null
  }
}

// Confirm Set Live 

function setLive(urlOut) {
 var where_to= confirm("Change This Issues Publication Status?");
 if (where_to== true)
 {
   window.location=urlOut;
 }
 else
 {
   null
  }
}

// Remote Pop-Up Window
function remote(theURL,winName,features){
  win2=window.open(theURL,winName,features)
  win2.creator=self
}

// Remote Pop-Up Window B
function remote2(url){
  window.opener.location=url
  close();
}


// Jump Menu
function _jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// Hide/Show Toggle
function showHide(targetName,imgName) {
  if( document.getElementById ) { // NS6+
    target = document.getElementById(targetName);
    image = document.getElementById(imgName);
  } else if( document.all ) { // IE4+
    target = document.all[targetName];
    image = document.all[imgName];
  }

  if( target ) {
    if( target.style.display == "none" ) {
      target.style.display = "inline";
      image.src = "/img/menuTree_on.gif";
    } else {
      target.style.display = "none";
      image.src = "/img/menuTree_off.gif";
    }
  }
  
}

// Swap Two Elements
function swapTargets(targetNameA,targetNameB) {
	if( document.getElementById ) { // NS6+
		targetA = document.getElementById(targetNameA);
		targetB = document.getElementById(targetNameB);
	} else if( document.all ) { // IE4+
		targetA = document.all[targetNameA];
		targetB = document.all[targetNameB];
	}

	if( targetA ) {
		if( targetA.style.display == "none" ) {
			targetA.style.display = "inline";
			targetB.style.display = "none";
			document.forms[0].articleEdit.disabled = false;
			document.forms[0].articleText.disabled = true;
		} else {
			targetA.style.display = "none";
			targetB.style.display = "inline";
			document.forms[0].articleEdit.disabled = true;
			document.forms[0].articleText.disabled = false;
		}
	}
}


// Clear Search Field  
function focusNClearInput(inputObject) {

    var hasBeenFocused = inputObject.getAttribute("hasBeenFocused") != null;

    if (!hasBeenFocused) {
        inputObject.setAttribute("hasBeenFocused", "true");
        inputObject.value = "";
        var thisForm = inputObject.form;
        for (ii = 0;  ii < thisForm.elements.length; ii++) {
            if (thisForm.elements[ii].type == "submit"){
                thisForm.elements[ii].disabled = false;
            }
        }

    }
  }
  
// Image Auto Resizer
function imageResize(page, mh, mw){
	var doober;
	//alert("I Are Run");
	//alert(page.images.length);
	for	(var i=0; i<page.images.length; i++){
		var img = page.images[i];
		//alert(img.id.substr(0,2));
		if(img.id.substr(0,2) != "U_"){
			//alert(img.width + '&' + img.height);
			if(img.width > img.height && img.width > mw){
				doober = img.width / mw;
				img.width = mw;
			} else if (img.height > img.width && img.height > mh){
				doober = img.height / mh;	
				img.height = mh;
			} else if (img.width == img.height && img.width > mw){
				img.heigh = mh;
				img.width = mw;
			} else {
				//alert('no action');
			}
		}
	}
}  

// Auto CutOff Picker
function cutOffer(item, targetName){
  if( document.getElementById ) { // NS6+
    target = document.getElementById(targetName);
  } else if( document.all ) { // IE4+
    target = document.all[targetName];
  }
  
  if(item.value == 2){
  	target.value = "500";
  }
  else if(item.value == 1){
  	target.value = "250";
  } 
  else {
  	target.value = "0"
  }
}


// ***Java Script Library*** //

function AddLeadingZeros(num, length) {
	var numstr = num.toString();
	var padlength = length - numstr.length;
	var zeros = new Array(padlength);
	for (var i = 0; i < padlength; ++i) {
		zeros[i] = '0';
	}
	return zeros.join('') + numstr;
}

function dollarFormat(num){
	if(!isNaN(num)){
		var newNum;
		newNum = Math.abs(num).toFixed(2);
		if(num < 0){
			newNum = '(' + newNum + ')';
		}
		newNum = '$' + newNum;
		return newNum;
	} else {
		return num;
	}
}

function dollarUnformat(str){
	var num = String(str);
	
	num = num.replace(/[\$\)]+/g,'');
	num = num.replace('(','-');
	
	return num;
}

function dateUnformat(str){
	var dt = '';
	
	var arDt = str.split('/');
	dt = arDt[2] + arDt[0] + arDt[1];
	
	return dt;
}

function dateFormat(str){
	var dt = '';
	
	dt += String(str).substr(4,2) + '/' + String(str).substr(6,2) + '/' + String(str).substr(0,4);
	return dt;
}

function createModal(){
	var modalWindow = {
		parent:"body",
		windowId:null,
		content:null,
		width:null,
		height:null,
		extraClass:"",
		close:function()
		{
			$(".modal-window").remove();
			$(".modal-overlay").remove();
		},
		open:function()
		{
			var modal = "";
			modal += "<div class=\"modal-overlay\"></div>";
			modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\">";
			modal += this.content;
			modal += "</div>";	
			
			$(this.parent).append(modal);
			var $window = $("#" + this.windowId);
			// ensures the modal window width is never distorted with respect to the inner table.
			$window.width($window.find("table").outerWidth());
			$window.css("margin-left", ($window.width()/2)*-1).css("margin-top", ($window.height()/2)*-1.3).addClass(this.extraClass);
			$(".close-window").click(function(){modalWindow.close();});
		}
	};
	
	return modalWindow;	
}

function genericAJAXError(XMLHttpRequest, textStatus, errorThrown) {
	if (console) {
		console.log(XMLHttpRequest);
		console.log(textStatus);
		console.log(errorThrown);
	}
}
