/* validation functions */
function alertAndFocus(element, alertText) {
	alert(alertText);
	element.focus();	
}
function isValueEmpty(value) { 
	
	if (value == "" ){ return true; }
	return false;  
}
function isEmpty(element, defaultValue, errorAlert) { 
	//alert(element.value);
	if ( (element.value == "" ) || (element.value == defaultValue ) ){ 
		alertAndFocus(element,errorAlert); 
		return true;  
	} else {
		return false;  
	}
}
function clearInput(defaultText, elementName) {	
	element = document.getElementById(elementName);			
	//alert(element.value)
	if (element.value == defaultText) {
		element.value= "";
	}	
}
function isNumerical(stringInput, allowedChars) {  
	var decPoints = 0;
	var decCheck = false;    
	
	if (allowedChars.indexOf(".") != -1) {     
		decCheck = true;
	} 	               
	for (i = 0;  i < stringInput.length;  i++)  {
		ch = stringInput.charAt(i);            
		if (allowedChars.indexOf(ch) == -1) {    
			return false;
		}             
		if ( (decCheck == true) && (ch == ".") ) {
			decPoints++;
			if (decPoints > 1) {
				return false;
			}
		}         
	}	 
	return true;
}
function isSelectedIndex(element, errorAlert, indexValue) {     
	if (element.selectedIndex == indexValue) {  
		alert(errorAlert);
		element.focus();
		return true;        
	}  
	return false;
}
function validateEmail(email, defaultText) {	
	if ((email == "") || (email == defaultText)) {
		return false;
	} else {
		eLength = email.length;
		eAt = email.indexOf("@"); 
		eDot = email.lastIndexOf(".");	
		
		// if no @ sign, @ is at start or end of email
		if ( (eAt == -1) || (eAt == 0) || (eAt == eLength-1) ) {
			return false;
		} else {
			// if no dot, dot is at start end or next to or less than @
			if ( (eDot == -1) || (eDot == 0) || (eDot == eLength-1) || (eDot <= eAt+1)  )  {
				return false;
			}		
		}
	}
	return true;
}
/* end of validation functions */



/* sytle switcher */
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}
function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}
function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}
window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
/* end of sytle switcher */



/* popup */
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function popupHelp(strUrl) {
	MM_openBrWindow(strUrl,'Help','scrollbars=yes,width=400,height=300');	
	return false;
}

/* val retrieve quote form */
function valRetrieveQuote() {
	//frmUsername
	//frmPassword
	if ( isEmpty(document.getElementById("frmEmail"), "email address", "Please enter your email address.")) {
		return false;
	}
	if ( isEmpty(document.getElementById("frmReference"), "reference", "Please enter your quote reference number.")) {
		return false;
	}

	return true;
}

function setElementValue(elementID, valueToSet) {
	// check the element exists
	if (document.getElementById(elementID)) {
		document.getElementById(elementID).value = valueToSet;
	}
}
function setSelectedIndex(elementID, indexToSet) {
	// check the element exists
	if (document.getElementById(elementID)) {
		document.getElementById(elementID).selectedIndex = indexToSet;
	}
}
function setElementChecked(elementID, valueToSet) {
	// check the element exists
	if (document.getElementById(elementID)) {
		document.getElementById(elementID).checked = valueToSet;
	}
}

