var win;

function DateValidation(valDate)
{
  var dayStr = "";
  var monthStr = "";
  var yearStr = "";
  var inDate = stripCharString(valDate.value," ");

  var dateIsBad = 0;  
  if (!allowInString(inDate,"/-0123456789"))
  {
    dateIsBad = 1; // invalid characters in date
  }
  else
  {
  	if (inDate.search("/") == -1 & inDate.search("-") == -1)
  	{
  	  switch (inDate.length)
  	  {
  		case 8:	// Assuming MMDDYYYY entry.
  			monthStr = inDate.substr(0,2);
  			dayStr = inDate.substr(2,2);
  			yearStr = inDate.substr(4);
  			break;

  		case 7:	// Assuming MDDYYYY entry.
  			monthStr = "0" + inDate.substr(0,1);
  			dayStr = inDate.substr(1,2);
  			yearStr = inDate.substr(3);
  			break;
	    
  		case 6:	// Assuming MMDDYY entry.
  			if (inDate.substr(4,2) < 70) {
  			var century = "20";
  			} else {
  			var century = "19";
  			}
	  	    
  			monthStr = inDate.substr(0,2);
  			dayStr = inDate.substr(2,2);
  			yearStr = century + inDate.substr(4,2);
  			break;
	   
  		case 5:	// Assuming MDDYY entry.
  			if (inDate.substr(3,2) < 70) {
  			var century = "20";
  			} else {
  			var century = "19";
  			}
		
  			monthStr = "0" + inDate.substr(0,1);
  			dayStr = inDate.substr(1,2);
  			yearStr = century + inDate.substr(3,2);
  			break;
	 	
  		case 4:	// Assuming MMDD entry.
  			currDate = new Date();
  			monthStr = inDate.substr(0,2);
  			dayStr = inDate.substr(2);
  			yearStr = currDate.getFullYear();
  			break;
	 	
  		case 3:	// Assuming MDD entry.
  			currDate = new Date();
  			monthStr = "0" + inDate.substr(0,1);
  			dayStr = inDate.substr(1);
  			yearStr = currDate.getFullYear();
  			break;
	 	
  		case 2:	// Assuming DD entry.
  			currDate = new Date();
  			m = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
  			monthStr = m[currDate.getMonth()];
  			dayStr = inDate;
  			yearStr = currDate.getFullYear();
  			break;
	  	    
  		case 1:	// Assuming D entry.
  			currDate = new Date();
  			m = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
  			monthStr = m[currDate.getMonth()];
  			dayStr = "0" + inDate;
  			yearStr = currDate.getFullYear();
  			break;
		
  		case 0:
  			valDate.value = inDate;
  			return;
	 	
  		default:
  			dateIsBad = 1;
  	  }
  
  	}
  	else
  	{
  	  var parseChar = "";
  	  if (inDate.search("/") == -1) {
  	    parseChar = "-";
  	  } else {
  	    parseChar = "/";
  	  }
  	  
  	  var firstSlash = inDate.indexOf(parseChar);
  	  switch (firstSlash) {
  	    case 1:
  	      monthStr = "0" + inDate.substr(0,firstSlash);
  	      break;
  	    
  	    case 2:
  	      monthStr = inDate.substr(0,firstSlash);
  	      break;
  	    
  	    default:
  	      dateIsBad = 1;
  	  }
  	  
  	  inDate = inDate.substr(firstSlash + 1);
  	  
  	  var moreProcessing = "T";
  	
  	  var secondSlash = inDate.indexOf(parseChar);
  	 
  	  switch (secondSlash) {
  	    case -1:
  	      switch (inDate.length) {
  	        case 1:
  	          dayStr = "0" + inDate;
  	          break;
  	        
  	        case 2:
  	          dayStr = inDate;
  	          break;
  	        
  	        default:
  	          dateIsBad = 1;
  	      }
  	      
  	      currDate = new Date();
	      yearStr = currDate.getFullYear();
	      
	      moreProcessing = "F";
	      break;
  	    
  	    case 1:
  	      dayStr = "0" + inDate.substr(0,secondSlash);
  	      break;
  	      
  	    case 2:
  	      dayStr = inDate.substr(0,secondSlash);
  	      break;
  	      
  	    default:
	      dateIsBad = 1;
  	  }
  	  
  	  
  	  if (moreProcessing == "T") {
  	    inDate = inDate.substr(secondSlash + 1);
  	  
  	    switch (inDate.length) {
  	      case 2:
  	        if (inDate < 70) {
    	          var century = "20";
  	        } else {
  	          var century = "19";
  	        }
  	        yearStr = century + inDate;
  	        break;
  	      
  	      case 4:
  	        yearStr = inDate;
  	        break
  	      
  	      default:
  	        dateIsBad = 1;
  	    }
  	  }
  	}
  valDate.value = monthStr + "/" + dayStr + "/" + yearStr;
  }
  
  if (dateIsBad == 1) {
    alert("Please enter a valid date.");
    valDate.focus();
    return (false);
  } else {
    var longMonthStr = getLongMonthStr(monthStr);
    var dateStr = dayStr + ' ' + longMonthStr + ' ' + yearStr;
    
    // Using form values, create a new date object
    // which looks like "Wed Jan 1 00:00:00 EST 1975".
    var dateTest = new Date( dateStr );
     
    // Convert the date to a string so we can parse it.
    var dateTestString = dateTest.toGMTString();
    
    /* Split the string at every space and put the values into an array so, using the
    previous example, the first element in the array is "Wed", the second element is "Jan",
    the third element is "1", etc. */
    var dateArray = dateTestString.split( ' ' );
    
    /* If we entered "Feb 31, 1975" in the form, the "new Date()" function converts the
    value to "Mar 3, 1975".  Therefore, we compare the month in the array with the
    month we entered into the form.  If they match, then the date is valid, otherwise,
    the date is NOT valid. */
    if ( dateArray[2] != longMonthStr ) {
      alert("Please enter a valid date");
      valDate.focus();
    }
    return (true);
  }
}

function stripCharString (inString, charString)  {
  var outString = "";
  for (var count = 0; count < inString.length; count++)  {
    var tempChar = inString.substring (count, count + 1);
    var strip = false;
    for (var countX = 0; countX < charString.length; countX++) {
      var stripThis = charString.substring(countX, countX+1);
      if (tempChar == stripThis) {
        strip = true;
        break;
      }
    }
    if (!strip)
      outString = outString + tempChar;
  }
  return (outString);
}

function allowInString (inString, refString)  {
  for (var count = 0; count < inString.length; count++)  {
    var tempChar = inString.substring (count, count + 1);
    if (refString.indexOf (tempChar, 0) == -1)  
      return (false);
  }
  return (true);
}

function getLongMonthStr (inMonthStr)  {
  var month = ""
  switch (inMonthStr) {
    case "01":
      month = "Jan";
      break;
    case "02":
      month = "Feb";
      break;
    case "03":
      month = "Mar";
      break;
    case "04":
      month = "Apr";
      break;
    case "05":
      month = "May";
      break;
    case "06":
      month = "Jun";
      break;
    case "07":
      month = "Jul";
      break;
    case "08":
      month = "Aug";
      break;
    case "09":
      month = "Sep";
      break;
    case "10":
      month = "Oct";
      break;
    case "11":
      month = "Nov";
      break;
    case "12":
      month = "Dec";
      break;
  }
  return (month);
}

function SwapImage(top, bottom, landscape)
{
	if (landscape == "true")
	{
		document.forms[0].LargerPreviewTop.style.width = 168;
		document.forms[0].LargerPreviewTop.style.height = 120;

		document.forms[0].LargerPreviewBottom.style.width = 168;
		document.forms[0].LargerPreviewBottom.style.height = 120;

		document.forms[0].LandscapeSpacerTop.style.height = 63;
		document.forms[0].LandscapeSpacerBottom.style.height = 45;
	}
	else
	{
		document.forms[0].LargerPreviewTop.style.width = 120;
		document.forms[0].LargerPreviewTop.style.height = 168;

		document.forms[0].LargerPreviewBottom.style.width = 120;
		document.forms[0].LargerPreviewBottom.style.height = 168;

		document.forms[0].LandscapeSpacerTop.style.height = 0;
		document.forms[0].LandscapeSpacerBottom.style.height = 0;
	}

	document.forms[0].LargerPreviewTop.src = top;
	document.forms[0].LargerPreviewBottom.src = bottom;
}

function HideImagePanel()
{
	if( document.getElementById("MouseOverPanel") )
	{
		document.getElementById("MouseOverPanel").style.display = "none";
	}
}

function getPos(elm)
{
	var ref = elm;
	var elmOffsetTop = elmOffsetLeft = 0;

	while(ref != document.body)
	{
		elmOffsetTop += ref.offsetTop;
		elmOffsetLeft += ref.offsetLeft;
		ref = ref.offsetParent;
	}
}

function ChangeImage(originalImage, imageName, isLandscape, linkUrl, imageHost)
{
	
	document.getElementById("MouseOverLink").href = linkUrl;
	
	mouseOverImage = document.getElementById("MouseOverImage");
	mouseOverImage.src = imageHost + "Images/General/spacer.gif";
	mouseOverInsideImage = document.getElementById("MouseOverInsideImage");
	mouseOverInsideImage.style.visibility = "hidden";

	var smallImageHeight = 0;
	var largeImageHeight = 0;
	
	if (isLandscape == "True")
	{
		mouseOverImage.width = 210;
		mouseOverImage.height = 150;
		
		smallImageHeight = 82;
		largeImageHeight = 150;
	}
	else
	{
		mouseOverImage.width = 150;
		mouseOverImage.height = 210;

		smallImageHeight = 115;
		largeImageHeight = 210;
	}

	var ref = originalImage;
	var elmOffsetTop = elmOffsetLeft = 0;

	while(ref != document.body)
	{
		elmOffsetTop += ref.offsetTop;
		elmOffsetLeft += ref.offsetLeft;
		ref = ref.offsetParent;
	}
	
	mouseOverPanel = document.getElementById("MouseOverPanel");

	mouseOverPanel.style.left = elmOffsetLeft;
	mouseOverPanel.style.top = elmOffsetTop - (largeImageHeight - smallImageHeight);
	mouseOverPanel.style.position = "absolute";
	mouseOverImage.src = imageName;
	mouseOverPanel.style.display = "";
}

function ChangeImage2(originalImage, imageName, imageInsideName, isLandscape, linkUrl, imageHost)
{
	
	document.getElementById("MouseOverLink").href = linkUrl;
	
	mouseOverImage = document.getElementById("MouseOverImage");
	mouseOverImage.src = imageHost + "Images/General/spacer.gif";
	mouseOverInsideImage = document.getElementById("MouseOverInsideImage");
	mouseOverInsideImage.src = imageHost + "Images/General/spacer.gif";

	var smallImageHeight = 0;
	var largeImageHeight = 0;
	
	if (isLandscape == "True")
	{
		mouseOverImage.width = 210;
		mouseOverImage.height = 150;
		
		mouseOverInsideImage.width = 210;
		mouseOverInsideImage.height = 150;
		
		smallImageHeight = 82;
		largeImageHeight = 150;
	}
	else
	{
		mouseOverImage.width = 150;
		mouseOverImage.height = 210;

		mouseOverInsideImage.width = 150;
		mouseOverInsideImage.height = 210;

		smallImageHeight = 115;
		largeImageHeight = 210;
	}

	var ref = originalImage;
	var elmOffsetTop = elmOffsetLeft = 0;

	while(ref != document.body)
	{
		elmOffsetTop += ref.offsetTop;
		elmOffsetLeft += ref.offsetLeft;
		ref = ref.offsetParent;
	}
	
	mouseOverPanel = document.getElementById("MouseOverPanel");

	mouseOverPanel.style.left = elmOffsetLeft;
	mouseOverPanel.style.top = elmOffsetTop - (largeImageHeight - smallImageHeight);
	mouseOverPanel.style.position = "absolute";

	mouseOverImage.src = imageName;
	mouseOverInsideImage.src = imageInsideName;

	mouseOverPanel.style.display = "";
}

function DisplayFullSizeImage(originalImage, imageName, isLandscape, retailerImageDir)
{
	win = new PopupWindow( 'PreviewDiv' );
	var x;
	var y;
	
	if (isLandscape == "True")
	{
		x = (document.body.clientWidth - 504) / 2;
		y = (document.body.clientHeight - 360) /2;
	}
	else
	{
		x = (document.body.clientWidth - 360) / 2;
		y = (document.body.clientHeight - 504) /2;
	}
	
	if( x < 0 )
	{
		x = 0;
	}
	
	if( y < 0 )
	{
		y = 0;
	}

	win.offsetX = x;
	win.offsetY = y;
	
	win.autoHide();

	var innerHTML = "<table class='LargePreviewTable' cellspacing='0' cellpadding='0'>";
	innerHTML += "<tr>";
	innerHTML += "<td class='LargePreviewHeader'>&nbsp;Larger View</td>";
	innerHTML += "<td class='LargePreviewHeader' OnClick='win.hidePopup();' style='cursor: pointer' align='right'><img src='" + GetRetailerImage( retailerImageDir, "close.gif" ) + "' alt='Close'></td>";
	innerHTML += "</tr>";
	innerHTML += "<tr><td colspan='2'><img src='" + imageName + "' border='0'></td></tr>";
	innerHTML += "</table>";
	
	win.populate( innerHTML );
}

function GetRetailerImage( retailerDir, image )
{
	var img = new Image();
	img.src = retailerDir + image;
	if( img.complete )
	{
		return retailerDir + image;
	}
	else
	{
		return retailerDir + "../../images/general/" + image;
	}
}

function DisplayCITBWindow(linkUrl, isPunchout)
{
	if (isPunchout)
	{
		var width = 810;
		var height = 620;
		var winw = (screen.width - width) / 2;
		var winh = (screen.height - height) / 2;
		
		if (width > screen.width) 
		{
			width = screen.width - 10;
			winw = 0;
		}
		
		if (height > screen.height)
		{
			height = screen.height - 40;
			winh = 0;
		}
		
		var winpos = "width=" + width + ",height=" + height + ",scrollbars=yes,top=" +
			winh + ",left=" + winw + ",resizable=yes";
			
		var xwindow = window.open(linkUrl, "CardOrder", winpos);
		
		xwindow.opener = self;
		xwindow.window.focus();
	}
	else
	{
		location.replace(linkUrl);
	}
}

function DisplayCITBLabelWindow(linkUrl, isPunchout)
{
	if (isPunchout)
	{
		var width = 810;
		var height = 700;
		var winw = (screen.width - width) / 2;
		var winh = (screen.height - height) / 2;
		
		if (width > screen.width) 
		{
			width = screen.width - 10;
			winw = 0;
		}
		
		if (height > screen.height)
		{
			height = screen.height - 40;
			winh = 0;
		}
		
		var winpos = "width=" + width + ",height=" + height + ",scrollbars=yes,top=" +
			winh + ",left=" + winw + ",resizable=yes";
			
		var xwindow = window.open(linkUrl, "CardOrder", winpos);
		
		xwindow.opener = self;
		xwindow.window.focus();
	}
	else
	{
		location.replace(linkUrl);
	}
}

function DeleteCITBCard(linkUrl)
{
		location.replace(linkUrl);
}

