
/******************** MAIN JAVASCRIPT LIBRARY *********************************/
/************ NOTES START *******************************************************************************************
* 
* Date: 2008/01/12
* Purpose: Main JavaScript functions
* Instructions:
* Prerequisites: 
    
************* NOTES END *********************************************************************************************/

// Load Required JS files
// loadScript('/jseone/...')
function addItemToCart(formId, e, itemId) {
	var form = document.getElementById(formId);
	if (form) {
		form.IID.value = itemId;
	}
	form.submit();
}
var lastDisplayedDiv = 0;

function setShipMethod(e) {
    document.getElementById('shipmethod_'+getIndex(e)).checked='true';
}

function getIndex(e) {
    var arrTmp = e.id.split("_");
    if (arrTmp.length < 2) {
        // alert("error: invalid object.\nobject id: " + e);
        return false;
    }
    return arrTmp[1];
}

function GetAnswerDiv(objQuestionDiv) {
	var strFaqId = getIndex(objQuestionDiv);
	var strAnswerId = "answer_" + strFaqId;
	var objAnswer = document.getElementById(strAnswerId);
	if (!objAnswer) {
		// alert("error: answer object does not exist.\nexpected id: " + strAnswerId);
		return false;
	}
	return objAnswer;
}

function ToggleAnswer(objDiv) {
	var objAnswerDiv = GetAnswerDiv(objDiv);
	if (!objAnswerDiv) {
		return false;
	}
	if (objAnswerDiv.style.display == "none") {
      //first hide previous displyed answer:
		if (lastDisplayedDiv) {
			lastDisplayedDiv.style.display = "none";
		}
      //show given answer:
		objAnswerDiv.style.display = "block";
      //store current object:
		lastDisplayedDiv = objAnswerDiv;
	} else {
		//hide given answer:
		objAnswerDiv.style.display = "none";
    }
}

function win_Pop(URL,winName,features) { 
  
  /*extract height and width from features */
  var wp = /width=\d+/g;
  var hp = /height=\d+/g;
  var np = /\d+/g;
  var heightchunk = features.match(hp);
  //alert(heightchunk);
  var widthchunk = features.match(wp);
  //alert(widthchunk);
  var height = "";
  var width = "";
  if (heightchunk != null && heightchunk[0] != null) {
          height = heightchunk[0].match(np);
          //alert('height = ' + height);
          //alert('height[0] = ' + height[0]);
          if (height != null && height[0] != null) {
        	  height = height[0];
        	  //alert ('setting height to ' + height);
          } else {
        	  height = "";
        	  //alert ('setting height to null string');
          }
  }
  if (widthchunk != null && widthchunk[0] != null) {
          width = widthchunk[0].match(np);
          if (width != null && width[0] != null) {
        	  width = width[0];
          } else {
        	 width = "";
          }
  }

  // FIXME Deal with firefox forcing statusbar and locationbar
  /*if (height.length > 0 && width.length > 0) {
	  features += ",innerWidth=" + width + ",innerHeight=" + height;
  }
  alert(features);*/ 
  //features += "status=0,location=0"; // suppress status and location bars in FireFox
  //alert(features);
  var win = window.open(URL,winName,features);

  if (height.length > 0 && width.length > 0) {
	  win.resizeTo(width,height);
  }

  win.focus(); // ensure that popup has focus
}

function popUp(url) {
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
	self.name = "mainWin";
}

function verifyEmail(form, submitForm) {
	
	var isEmailValid = true;
	var email = form.emailAddress;
	if (!email && form.email) {
		email = form.email;
	}
	if (email) {
		isEmailValid = checkEmail(email.value);

		if (!isEmailValid) {
			alert("You have entered an invalid email address. Please try again.");
			email.select();
		} else if (submitForm) {
			form.submit();
		}

	}
	return isEmailValid;
}

function checkEmail(email) {
	if ((email.indexOf('@') < 0) || ((email.charAt(email.length-5) != '.') && (email.charAt(email.length-4) != '.') && (email.charAt(email.length-3) != '.'))) {
		return false;
	}
	else {
		return true;
	}
}