// JavaScript Document


function showDiv(targetDiv) {
	Div = document.getElementById(targetDiv);
	Div.className = 'popUpDivVisible';
}

function hideDiv(targetDiv) {
	Div = document.getElementById(targetDiv);
	Div.className = 'popUpDivHidden';
}

function ajaxFormtest(formName,contentsPath,targetDiv) {
	
	theForm = document.getElementById(formName);
	
	theForm.submit();
	
	}

function ajaxForm(formName,contentsPath,targetDiv) {

	ColdFusion.Ajax.submitForm(formName, contentsPath, ajaxcallback, ajaxerrorHandler);

	function ajaxcallback(text)
    {

		replaceDiv = document.getElementById(targetDiv);
		replaceDiv.innerHTML=text;
	}
	
}


function ajaxerrorHandler(code, msg)
    {
        alert("Error!!! " + code + ": " + msg);
    }

function CFWindow(name,title,url,wwidth,wheight)
	{
	

	var windowOptions = new Object();
      windowOptions.width = wwidth;
      windowOptions.height = wheight;
      windowOptions.center = true;
      windowOptions.modal = true;
      windowOptions.resizable = true;
      windowOptions.initshow = true;
      windowOptions.draggable = true;
      windowOptions.closeable = true;
	  windowOptions.refreshOnShow = true;
	  windowOptions.minheight= 100;
	  windowOptions.minwidth = 100;
	  windowOptions._cf_refreshOnShow = true;
	  windowOptions. bodystyle = 'margin: 0px; padding: 0px; background-color: #FFFFFF;';
      windowOptions.headerstyle = 'background-color: #000033; font: bold 10px Verdana;';

		ColdFusion.Window.create(name,title,url,windowOptions);
		ColdFusion.navigate(url,name)
		
	}
	
	
function scaleImage(imageId, size)
{
	
	var pic = document.getElementById(imageId);
	var w=pic.width;
	var h=pic.height;
	var file=pic.src;
	
	if (h == 0 | w == 0)
	{
		var newimage = new Image();
		newimage.src = file ;
		h = newimage.height;
		w = newimage.width;
	}

	if (h > w)
	{
		if (h > size) 
		{
		var f = h / size;
		pic.width=(w / f)|0;
		pic.height=(h / f)|0;
		}
		else
		{
		pic.width=w;
		pic.height=h;
		}
	}
	else if (w >= h)
	{
				
		if (w > size) 
		{
		var f = w / size;
		pic.width=(w / f)|0;
		pic.height=(h / f)|0;
		}
		else
		{
		pic.width=w;
		pic.height=h;
		}
	}
	
}

<!--Catalog SubItem Option Scripts-->
function updateSubItemsOptions(subItemID,optionID,PathToWebRoot) {
	sourceform = 'SubItem'+subItemID+'Option'+optionID+'Form';
	sourcefield = 'Option'+optionID;
	destfield = 'SubItem'+subItemID+'Option'+optionID;	
	document.optionsAndPriceForm[destfield].value = document[sourceform].elements[sourcefield].value;
	
	//testing	
	//document.optionsAndPriceForm.action=PathToWebRoot+'webscripts/catalog/Catalog_Item_SubItem_Price.cfm?SubItemID='+subItemID;
	//document.optionsAndPriceForm.target='_blank';
	//document.optionsAndPriceForm.submit();
	
	//Live
	sendform(subItemID,PathToWebRoot);
}	

function updateSubItemInventoryItem(subItemID,PathToWebRoot) {
	sourceform = 'SubItem'+subItemID+'InvForm';
	destfield = 'SubItem'+subItemID+'InvItemID';	
	document.optionsAndPriceForm[destfield].value = document[sourceform].InvItemID.value;
	sendform(subItemID,PathToWebRoot);
}

function sendform(subItemID,PathToWebRoot) {	
	ajaxForm('optionsAndPriceForm', PathToWebRoot+'webscripts/catalog/Catalog_Item_SubItem_Price.cfm?SubItemID='+subItemID, 'priceDiv'+subItemID);
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function popUpWindow(URLStr, windowname, left, top, width, height)
{

 if (window[windowname])
 	{
	if(!window[windowname].closed) window[windowname].close();
 	}
window[windowname] = window.open(URLStr, windowname, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
								 
								

}

function closepopUpWindow(windowname) {
	if (window[windowname])
 	{
		if(!window[windowname].closed) window[windowname].close();
 	}		
}

function overlayPleaseWait() {
	
	createOverlay();
	
	loadGraphic = document.getElementById("pleaseWaitGraphic");
	objh = parseFloat(loadGraphic.style.height)/2;
	objw = parseFloat(loadGraphic.style.width)/2;
	loadGraphic.style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
	loadGraphic.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px'; 
	
}
	
function createOverlay() {
		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','pleaseWaitOverlay');

		objBody.appendChild(objOverlay);
		
		var objPWGraphic = document.createElement("div");
		objPWGraphic.setAttribute('id','pleaseWaitGraphic');
		objPWGraphic.style.height = '64px';
		objPWGraphic.style.width = '32px';		
		objPWGraphic.innerHTML = 'Please Wait<br>';
		
		objBody.appendChild(objPWGraphic);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.setAttribute('src', '/tools/web_viewer/webscripts/Images/loading.gif');
		objPWGraphic.appendChild(objLoadingImage);
}

