function checklistscript() {
	user_input = document.forms[0].value.value;
	if (user_input == -1) {
		// something is wrong
		alert('Please select a Supplier');
		return false;
	}
	return true;
}

function checktextscript2() {
	user_input = document.forms[0].text1.value;
	user_input2 = document.forms[0].text2.value;

	if (user_input == "") {
		// something is wrong
		alert('Please fill in all the text fields');
		return false;
	}
	else if (user_input2 == "") {
		// something is wrong
		alert('Please fill in all the text fields');
		return false;
	}
	return true;
}

function checktextscript() {
	user_input = document.forms[0].text1.value;
	if (user_input == "") {
		// something is wrong
		alert('Please fill in all the text fields');
		return false;
	}
	return true;
}


function linkRefer() {
	var theLinks = document.links;
	
	//keep a record of referential pages via cookies
	for (i=0; i < theLinks.length; i++) {
		var thisLink = theLinks[i];
		var thisurl = document.location.href;
		if (thisLink.onclick==null) {
			thisLink.onclick = setReferCookie;
		}
	}
	
	//make any links with name 'refer' point to the referring page
	var referLinks = document.getElementsByName('refer');
	for (i=0; i < referLinks.length; i++) {
		var thisLink = referLinks[i];
		cookieValue = getCookie('refer');
		thisLink.href = cookieValue;
	}
}

function setReferCookie() {
  SetCookie('refer', document.location.href, 1);
}


function openPopUp(linkURL) {
window.open(linkURL,'popup','width=450,height=450,scrollbars=1')
}

function openPopUp_big(linkURL) {
child = window.open(linkURL,'popup_big','width=720,height=800,scrollbars=1')
}

function reload_window(){
    window.location.reload(true);
}

function disableFxn(identifier)
{
	var box = document.forms[identifier].incab;
	if (box.options[box.selectedIndex].value == 0) {
		document.forms[identifier].support485.disabled = 1;
		document.forms[identifier].support485.selectedIndex = 1;
	} else {
		document.forms[identifier].support485.disabled = 0;
	}
}


function special_message(theoid) {
  window.open("survey_special_message.php?oid=" + theoid,"mywindow", 'width=650,height=650,scrollbars=1, resizable=yes');
}

function doPopup(url, w, h) {
  window.open(url,"mywindow"+Math.floor(Math.random()*5), 'width='+w+',height='+h+',scrollbars=1, resizable=yes');
}

function doPopupFull(url, w, h) {
  window.open(url,"mywindow"+Math.floor(Math.random()*5), 'width='+w+',height='+h+',scrollbars=1, resizable=yes, menubar=1');
}

function roundedCorners() {
 var divs = document.getElementsByTagName('div');
 var rounded_divs = [];
 /* First locate all divs with 'rounded' in their class attribute
 for (var i = 0; i < divs.length; i++) {
   if (/\brounded\b/.exec(divs[i].className)) {
     rounded_divs[rounded_divs.length] = divs[i];
   }
 }
 /* Now add additional divs to each of the divs we have found */
 for (var i = 0; i < rounded_divs.length; i++) {
   var original = rounded_divs[i];
   /* Make it the inner div of the four */
   original.className = original.className.replace('rounded', '');
   /* Now create the outer-most div */
   var tr = document.createElement('div');
   tr.className = 'rounded2';
   /* Swap out the original (we'll put it back later) */
   original.parentNode.replaceChild(tr, original);
   /* Create the two other inner nodes */
   var tl = document.createElement('div');
   var br = document.createElement('div');
   /* Now glue the nodes back in to the document */
   tr.appendChild(tl);
   tl.appendChild(br);
   br.appendChild(original);
 }
} 

function swap(listIdPrefix) {
    collapsedList = document.getElementById(listIdPrefix + "_collapsed");
    expandedList = document.getElementById(listIdPrefix + "_expanded");
    if (collapsedList.style.display == "block") {
        collapsedList.style.display = "none";
        expandedList.style.display = "block";
        document.cookie = 'questionShow=yes';
    } else {
        collapsedList.style.display = "block";
        expandedList.style.display = "none";
        document.cookie = 'questionShow=no';
    }
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function scrollQuestions(distance) {
		divqid = document.getElementById('questions');
		divqid.scrollTop=distance;		
}
	
function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function checkSelects(din, dinIndex) {
	key_dins = new Array(2,7,10,5,3,4,6,11,12);
	
	slct = document.getElementById('din' + din);
	if (din==5) {
		high_flow = document.getElementById('din6');
		if (slct.value=='l' || slct.value=='b' || slct.value=='-5') {
			high_flow.disabled = false;
			document.getElementById('din6_explain').innerHTML='';
		} else {
			high_flow.disabled = true;
			document.getElementById('din6_explain').innerHTML='Applicable only to 40 or 60 GPM';
		}
	}
	
	if (din==11) {
		positions = document.getElementById('din12');
		if (slct.value=='s') {
			positions.value = '2';
			positions.disabled = true;
			document.getElementById('din12_explain').innerHTML='Side Mount is always two sides';
		} else {
			positions.value = '-5';
			positions.disabled = false;
			document.getElementById('din12_explain').innerHTML='';
		}
	}
	
	if (din==3) {
		products = document.getElementById('din3');
		hoses = document.getElementById('din4');
		if (products.value > hoses.value && hoses.value != -5) {
			hoses.value = products.value;
			document.getElementById('din4_explain').innerHTML = 'Hoses Outlets increased to ' + products.value;
		} else {
			document.getElementById('din4_explain').innerHTML = '';
		}
		
		
		fleet = document.getElementById('fleet');
		fleet_explain = document.getElementById('fleet_exp');
		if (products.value ==4) {
			fleet.disabled=true;
			fleet_explain.innerHTML = 'Fleet Card not compatible with 4 products';
		} else {
			fleet.disabled=false;
			fleet_explain.innerHTML = '';
		}
		
		
	}
	
	if (din==4) {
		products = document.getElementById('din3');
		hoses = document.getElementById('din4');
		document.getElementById('din4_explain').innerHTML = '';
		if (hoses.value < products.value && hoses.value!= -5) {
			hoses.value = products.value;
			document.getElementById('din4_explain').innerHTML = 'You cannot have less hose outlets than products';
		}
		
		fleet = document.getElementById('fleet');
		fleet_explain = document.getElementById('fleet_exp');
		if (hoses.value ==4) {
			if (fleet.value == 'f') {
				fleet.value = 'o';
			}
			fleet_explain.innerHTML = 'Internal Fleet Card not compatible with 4 hose outlets';
			
		} else {
			fleet.disabled=false;
			fleet_explain.innerHTML = '';
		}
		
		combo = document.getElementById('din6');
		if (hoses.value==2 && combo.value != 'c' && combo.value != 'd') {
			orientation = document.getElementById('din11');
			positions = document.getElementById('din12');
			fleet_explain.innerHTML = 'Note: The 2 outlet Internal Fleet Card option requires<br /> a front oriented (mount), 2 Positions dispenser';
		}
	}
	
	ajaxRequestProducts();
}

function checkFleet() {
	startAllSelects();
}

function startAllSelects() {
	key_dins = new Array(2,7,10,5,3,4,6,11,12);
	for(i=0; i<key_dins.length; i++) {
		checkSelects(key_dins[i], i);
	}
}

function getSelectParams(sort, order) {
	key_dins = new Array(2,7,10,5,3,4,6,11,12);
	din_values = new Array();
	
	for(i=0; i<key_dins.length; i++) {
		din_values[i] = 'din' + key_dins[i] + '=' + document.getElementById('din' + key_dins[i]).value;
	}
	
	fleet = document.getElementById('fleet');
	din_values.push('fleet=' + fleet.value)
	
	if (typeof(sort) != 'undefined') {
		din_values.push('sort=' + sort);
	}
	
	if (typeof(order) != 'undefined') {
		din_values.push('order=' + order);
	}
	
	
	return din_values.join('&');
}

function ajaxRequestProducts(sort, order) {
	
  var pars = getSelectParams(sort, order) + '&rnd=' + (Math.random()*5);
  var url = 'productLoad.php';
  var qAjax = new Ajax.Updater(
  			'productsDiv',
  			url,
  			{
  					method: 'get',
  					parameters: pars,
  					evalScripts: true
  			});
}

function submitSend() {
	check = document.getElementById('permission');
	contact_name = document.getElementById('contact_name');
	contact_email = document.getElementById('contact_email');
	if (check.checked) {
		if (contact_name.value != '') {
			if (contact_email.value != '') {
				document.sendSpec.submit();
			} else {
				alert('Please provide us with your email so we can contact you.')
			}
		} else {
			alert('Please provide us with your name so we can contact you.');
		}
	} else {
		alert('Please check the box giving us permission to review your spec.');
	}
}

function extendDiv() {
	qDiv = document.getElementById('questions');
	extend = document.getElementById('extend');
	if (qDiv.style.height == 200 +'px') {
		qDiv.style.height = 400;
		extend.innerHTML = 'Collapse Questions Up';
		
	} else {
		qDiv.style.height = 200;
		extend.innerHTML = 'Extend Questions Down';
	}
}

function hideDiv() {
	testDiv = document.getElementById('testDiv');
	if (testDiv.style.display == 'none') {
		testDiv.style.display = 'block';
		testDiv.style.width = "500px";
	} else {
		testDiv.style.display = 'none';
		testDiv.style.width = "50px";
	}
}

function noteSubmit(id) {	
	textBox = document.getElementById('text' + id);
	
	document.getElementById('notesDiv').innerHTML = 'Updating...';
	
	var pars = 'id=' + id + '&notes=' + textBox.value + '&rnd='+(Math.random()*5);
	ajaxEngine.sendRequestAndUpdate( 'notesRequest', 
		'notesDiv', {parameters:pars, evalScripts:true});

	document.getElementById('span' + id).innerHTML = textBox.value;
	if (textBox.value != '') {
		document.getElementById('i' + id).innerHTML = '';
	} else {
		document.getElementById('i' + id).innerHTML = 'No Notes Entered';
	}
	
	document.getElementById('div' + id).innerHTML = '<a href=\"javascript:addForm(' + id + ')\" class=\"link_traditional\">Edit</a>'
}

function doCorners() {
	settings = {
      tl: { radius: 20 },
      tr: { radius: 20 },
      bl: { radius: 20 },
      br: { radius: 20 },
      antiAlias: true,
      autoPad: false
    }

    var cornersObj = new curvyCorners(settings, 'roundMe');
    cornersObj.applyCornersToAll();
}
