var openWindows = new Array(5);

function setElementHeight(element, v){
	if (element == null) return;
	if (isNaN(v)) return;
	if (v < 1) v = 1;
	element.style.height = v;
}

function setElementWidth(element, v){
	if (element == null) return;
	if (isNaN(v)) return;
	if (v < 1) v = 1;
	element.style.width = v;
}

function moveElement(elem, top, left) {
	if (elem != null) {
		elem.style.pixelTop += top;
		elem.style.pixelLeft += left;
	}
}

function resizeElement(elem, width, height) {
	if (elem != null) {
		setElementWidth(elem, elem.style.pixelWidth + width);
		setElementHeight(elem, elem.style.pixelHeight + height);
	}
}

function addWindow(w) {
	var newArray, i;

	// clean up array
	checkOpenWindows();
	
	//double array if full
	if(openWindows[openWindows.length - 1] != null) {
		newArray = new Array(openWindows.length * 2);
		for(i = 0; i < openWindows.length; i++) {
			newArray[i] = openWindows[i];	
		}
		delete openWindows;
		openWindows = newArray;
	}
	
	//check if window already in array
	for(i = 0; i < openWindows.length; i++) {
		if((openWindows[i] != null) && (openWindows[i].name == w.name)) {
			openWindows[i] = w;
			return;
		}	
	}
	
	//add new window to array
	for(i = 0; i < openWindows.length; i++) {
		if(openWindows[i] == null) {
			openWindows[i] = w;
			return;
		}
	}
}

function checkOpenWindows() {
	var bComplete = false;
	var name, i;

	//clean up array
	while(!bComplete) {
		bComplete = true;
		for(i = 0; i < openWindows.length - 1; i++) {
			try {
				name = openWindows[i].name;
			}
			catch(e) {
				openWindows[i] = null;
			}
			
			if((openWindows[i] == null) && (openWindows[i + 1] != null)) {
				openWindows[i] = openWindows[i + 1];
				openWindows[i + 1] = null;
				bComplete = false;
				break;
			}
		}
	}
}

function closeChildWindow(windowid) {
	for(var i = 0; i < openWindows.length; i++) {
		try {
			if((openWindows[i] != null) && (openWindows[i].name == windowid)) {
				openWindows[i].close();
				openWindows[i] = null;
			}
		}
		catch(e) { }
	}	
}

function closeChildWindows(parentWindow) {
	checkOpenWindows();
	
	for(var i = 0; i < openWindows.length; i++) {
		if((openWindows[i] != null) && (openWindows[i].opener == parentWindow)) {
			openWindows[i].close();
			openWindows[i] = null;
		}
	}
}

function showDiv(div, visible) {
	if (div == null) return;
	
	if (visible) {
		div.style.visibility = 'visible';
		div.style.display = 'inline';
	}
	else {
		div.style.visibility = 'hidden';
		div.style.display = 'none';
	}
}

function showElement(elem, visible) {
	if (elem == null) return;
	
	if (visible) {
		elem.style.visibility = 'visible';
		elem.style.display = 'block';
	}
	else {
		elem.style.visibility = 'hidden';
		elem.style.display = 'none';
	}
}

function resizeEntirePage()
{
	var header = document.getElementById('Header');
	var footer = document.getElementById('Footer');
	var mainBodyDiv = document.getElementById('mainBodyDiv');
	var bodyContent = document.getElementById('BodyContent');
	
	var h, w;
	
	//alert('Before:' + bodyContent.style.pixelWidth);
	
	w = getSize('width') - bodyContent.offsetLeft - 25;
	setElementWidth( bodyContent , w );
	setElementWidth(Footer, w);
	
	//alert('After:' + bodyContent.style.pixelWidth);
	
	/*
	w = .8 * getSize('width');
	//alert(w);
	
	
	
	
	
	header.style.pixelWidth = w;
	header.style.left = (getSize('width') - w) / 2;
	
	mainBodyDiv.style.pixelWidth = w;
	mainBodyDiv.style.left = ((getSize('width') - w) / 2); 
	
	bodyContent.style.pixelWidth = w - 95;
	
	footer.style.pixelWidth = w - 95;
	*/
	
	
	//alert((getSize('width') - w) / 2);
	
	//footer.style.pixelWidth = w - 95;
	//footer.style.left = parseFloat(mainBodyDiv.style.left) + 95; 
	
	//alert(footer.style.pixelWidth);
	//alert(footer.style.left);
	



}

function resizeBodyContent(dimension) {
	var bc
	bc = document.getElementById('BodyContent')
	if (bc == null) return;
	
	var h, w
	h = getSize('height') - 300 ;
	w = getSize('width') - bc.offsetLeft - 4 ;
	
	if (dimension == 'height')
		setElementHeight( bc , h )
	else if (dimension == 'width')
		setElementWidth( bc , w )
	else {
		setElementHeight( bc , h )
		setElementWidth( bc , w )
		}
}

function getSize( dimension ) {

var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
} else if( document.documentElement &&
	( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
}
	
if ( dimension == 'height' )
	return myHeight;
else
	return myWidth;
}


// DIALOGS
function openPrintDialog()
{
	var d, loc;
	loc = 'dialogs/printdialog.html';

	d = window.open(loc, 'printdialog', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=450');
	addWindow(d);
	
}

function initPage(abFromQuote)
{
	var FullDiv = document.getElementById('FullDiv');
	var HeaderTable1 = document.getElementById('header');
	var HeaderTable2 = document.getElementById('header2');
	
	if(HeaderTable1 != null)
		showElement(HeaderTable1, true);

	if(HeaderTable2 != null)
		showElement(HeaderTable2, true);
	
	if(FullDiv != null)
		showDiv(FullDiv, true);
	
	resizePage();	
	if (abFromQuote != undefined)
	{
		var materialtype = document.getElementById('materialtype');
		if (materialtype != null)
			showElement(materialtype, true);	
		
		var overallthickness = document.getElementById('overallthickness');
		if (overallthickness != null)
			showElement(overallthickness, true);
		
		var mintracespace = document.getElementById('mintracespace');
		if (mintracespace != null)
			showElement(mintracespace, true);
		
		var smallestsmtpitch = document.getElementById('smallestsmtpitch');
		if (smallestsmtpitch != null)
			showElement(smallestsmtpitch, true);
		
		var methodofseparation = document.getElementById('methodofseparation');
		if (methodofseparation != null)
			showElement(methodofseparation, true);
		
		var typeofsoldiermask = document.getElementById('typeofsoldiermask');
		if (typeofsoldiermask != null)
			showElement(typeofsoldiermask, true);
		 
		var silkscreen = document.getElementById('silkscreen');
		if (silkscreen != null)
			showElement(silkscreen, true);
	}
	
	
	
	initjsDOMenu();
	
	
}

function initLoginPage(tableSize, showStatusDiv)
{
	var header = document.getElementById('Header');
	var footer = document.getElementById('Footer');
	var BodyDiv = document.getElementById('Body');

	showElement(header, true);
	showDiv(BodyDiv, true);
	showElement(footer, true);
	
	if(showStatusDiv != undefined)
	{
		if(showStatusDiv == '1')
		{
			var statusText = document.getElementById('statusText');
			statusText.innerText = 'Thank you for submitting your quote. Milplex will be contacting you soon.';
		}
		else
		{
			var statusText = document.getElementById('statusText');
			statusText.innerText = '';
		}
	}

}

function getLeftMenuPos(tableSize)
{
	var MainDiv = document.getElementById('MainDiv');
	var leftPos;
	
	var windowSize = getSize('width');
	if (windowSize > (tableSize + 20))
		return ((windowSize - 20 - tableSize)/2) + 15;
	else
		return 15;
}

function SubmitContactUs()
{
	var frm = document.frmMain;
	if (validateContactInfo() == true)
		frmMain.submit();
	
}

function SubmitQuote()
{
	var frm = document.frmMain;
	if (validateQuote() == true)
	{
		refreshIDs('checkboxlist1', 'list1');
		refreshIDs('checkboxlist2', 'list2');
		frmMain.submit();	
	}
}

function refreshIDs(checkboxcollection, inputvar)
{
	var idList = document.getElementById(inputvar);
	var ids = document.getElementsByName(checkboxcollection);
	var i;
	var sTemp;

	if (ids == null) return;
	
	sTemp = '';
	for( i=0 ; i<ids.length ;i++){
		if (ids[i].checked == 1)
			sTemp += ids[i].value + ' | ';
	}
	
	idList.value = sTemp;
}

function validateQuote()
{
	if(!validateContactInfo())
		return false;

	var partnumber = document.getElementById('partnumber');
	if (partnumber.value == '')
	{
		alert('Please enter a Part Number');
		return false;
	}
	
		
	return true;
}


function validateContactInfo()
{
	var name = document.getElementById('name');
	//var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	
	
	if (name.value == '')
	{
		alert('Please enter a name');
		return false;
	}	
	
	/*if (!IsValidEmailAddress(email.value))
	{
		alert('Please enter a valid email address');
		return false;
	}*/
	
	if (phone.value == '')
	{
		alert('Please enter a phone number');
		return false;
	}	
	
	return true;

}

function IsValidEmailAddress(asEmailAddress)
{
	var lsName, lsDomain, lsTLD, liAtLoc, liDotLoc, i, temp, lipos, dotfound
   
    liAtLoc = asEmailAddress.indexOf("@");
    
    if (liAtLoc < 0)
		return false
    
    lsName = asEmailAddress.substring(0, liAtLoc);
    lsDomain = asEmailAddress.substring(liAtLoc+1);

    
    lipos = lsDomain.length-1;
    dotfound = '0';
    liDotLoc = '-1';

    while (lipos >= 0)
    {
		if (lsDomain.charAt(lipos) == '.')
		{
			dotfound = '1';
			liDotLoc = lipos;
			break;
		}
		lipos -= 1;
    
    }

    
    if (liDotLoc == '-1')
		return false;
		
	lsTLD = lsDomain.substring(liDotLoc+1);

    if (lsTLD.length > 4 || lsTLD.length < 2)
		return false;

	if (lsName.charAt(0) == '.' || lsName.charAt(lsName.length-1) == '.')
		return false;
    
    if (lsDomain.charAt(0) == '.' || lsDomain.charAt(lsDomain.length-1) == '.')
		return false;
		
	if (asEmailAddress.indexOf('..') >= 0)
		return false;
	
	lipos = 0;
	temp = 'abcdefghijklmnopqrstuvwxyz1234567890_-.';
	while (lipos < lsName.length)
	{
		if (temp.indexOf(lsName.charAt(lipos).toLowerCase()) < 0)
			return false;
	
		lipos += 1;
	}
	
	lipos = 0;
	temp = 'abcdefghijklmnopqrstuvwxyz1234567890_-.';
	while (lipos < lsDomain.length)
	{
		if (temp.indexOf(lsDomain.charAt(lipos).toLowerCase()) < 0)
			return false;
	
		lipos += 1;
	}
		
	//Valid Email Address
	return true;

}

function initCheckBoxes(checkbox, idsRef)
{
	var checkboxCollection = document.getElementsByName(checkbox);
	var idsList = document.getElementById(idsRef);
	
	var id;
	var ids = idsList.value;
	
	while(ids.indexOf("|") > 0)
	{
		id = ids.substring(0, ids.indexOf("|"));
		id = trim(id);
		selectCheckBox(checkboxCollection, id); 
		ids = ids.substring(ids.indexOf("|")+1);
	}
}

function selectCheckBox(checkboxCollection, asID)
{
	var checkboxes = checkboxCollection;
	var i;
	
	for( i=0; i<checkboxes.length; i++){
	
		if (checkboxes[i].value == asID) {
			checkboxes[i].checked = 1; 
			break;
		}
	}
}

function trim(s) 
{
	while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}
	
	return s;
}

function trylogin()
{
	var frm = document.frmMain;
	if (validateLogin() == true)
	{
		frmMain.submit();	
	}
}

function validateLogin()
{
	var email = document.getElementById('email');
	var password = document.getElementById('password');	
	
	if (email.value == '')
	{
		alert('Please enter an email address');
		return false;
	}	
	
	if (!IsValidEmailAddress(email.value))
	{
		alert('Please enter a valid email address');
		return false;
	}
	
	if (password.value == '')
	{
		alert('Please enter a password');
		return false;
	}
	
	return true;
	
}

function tryregister()
{
	var frm = document.frmMain;
	if (validateRegister() == true)
	{
		frmMain.submit();	
	}
}

function validateRegister()
{
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var password = document.getElementById('password');	
	var phone = document.getElementById('phone');
	
	if (name.value == '')
	{
		alert('Please enter a name');
		return false;
	}	
	
	if (email.value == '')
	{
		alert('Please enter an email address');
		return false;
	}	
	
	if (!IsValidEmailAddress(email.value))
	{
		alert('Please enter a valid email address');
		return false;
	}
	
	if (password.value == '')
	{
		alert('Please enter a password');
		return false;
	}
	
	if (phone.value == '')
	{
		alert('Please enter a phone number');
		return false;
	}	
	return true;
	
}

function resizePage()
{
	var fullDiv = document.getElementById('FullDiv');
	var mainDiv = document.getElementById('MainDiv');
	var content = document.getElementById('Content');
	var tableBodyContent = document.getElementById('TableBodyContent');
	var overflowDiv = document.getElementById('OverflowDiv');
	var leftToolbarTable = document.getElementById('leftToolbarTable');

	
	var h, w
	h = getSize('height') - 61 ;
	w = getSize('width') - mainDiv.offsetLeft;
	
	if (fullDiv != null)
		setElementHeight(fullDiv, h);
		
	if (tableBodyContent != null)
		setElementHeight(tableBodyContent, h - 15 - 4);
	
	if (overflowDiv != null)
		setElementHeight(overflowDiv, h - 15 - 4 - 30 - 20);
		
	if (mainDiv != null)
		setElementWidth(mainDiv, w);
		
	if (content != null)
		setElementWidth(content, w - 150);

	if(leftToolbarTable != null)
	{
		if(h<=400)
		{
		//	setElementHeight(leftToolbarTable, h);
		}	
	}

}

function changeImagesArray(array){
    if (document.images)
      for(var i=0; i<array.length; i+=2){
          if(document[array[i]] != null){
            document[array[i]].src = array[i+1];
          }
        }
  }
        
  function changeImages(){
          changeImagesArray(changeImages.arguments);
        }
        
  function toggleImages(){
          for(var i=0; i<toggleImages.arguments.length; i+=2){
              changeImagesArray(toggleImages.arguments[i+1]);
            }
        }
  
  function preloadImages(){
            
      if(document.images){
        MenuItemOver = newImage("Res/images/menuItemOver_back.gif");
      }
  }
            
  function newImage(arg){
          if(document.images){
            rslt = new Image();
            rslt.src = arg;
            return rslt;
          }  
            
        }
 
  function tItemHover(row, isover)
  {
      if(isover)
        row.className = 'MenuItemHover';
      else
        row.className = 'MenuItem';
  }
         
  function openDialog(url, windowname, bInNewWindow){
  
    if(bInNewWindow)
        window.open(url, windowname);
    else
        window.location.href = url;
  }      
