/***********************************************************************
Description:	The Javascript of the MailExpress website
Project:	www.mailexpressinc.com
HTML:		Neal Gamradt
Graphics:	Sara Duquette
Created:	2007.03.19
Updated:	2007.07.18
***********************************************************************/

var dialup = 0;

function openWindow(strLocation,nbrWidth,nbrHeight) { 
	var strOptions = "location=yes,menubar=yes,toolbar=yes,scrollbars=yes,width=" + nbrWidth + ",height=" + nbrHeight;

	var nbrRandom=Math.floor(Math.random()*999);

	var objWindow = window.open(strLocation, String(nbrRandom), strOptions);
	objWindow.focus();

	return true;
}

function openSmallWindow(strLocation,nbrWidth,nbrHeight) { 
	var strOptions = "location=no,menubar=no,toolbar=no,scrollbars=no,width=" + nbrWidth + ",height=" + nbrHeight;

	var nbrRandom=Math.floor(Math.random()*999);

	var objWindow = window.open(strLocation, String(nbrRandom), strOptions);
	objWindow.focus();

	return true;
}

//v1.0
//Copyright 2006 Adobe Systems, Inc. All rights reserved.
function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '<object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';

  document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "wmode":
	ret.params[args[i]] = args[i+1];
	break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}
/* END ADOBE CODE */

/* BEGIN: FLASH RELATED FUNCTIONS */

//Flash Detect Variables
var flashinstalled = 0;
var flashversion = 0;

function checkFlash(nbrFlashversion) {

	if (nbrFlashversion < 1) {
		/* This Flash detection code was originally written by Peter-Paul Koch at http://www.quirksmode.org/.  He has allowed for this to be freely distributed per his copyright located at http://www.quirksmode.org/about/copyright.html. */

		if (navigator.plugins && navigator.plugins.length) {
			x = navigator.plugins["Shockwave Flash"];
			if (x) {

				flashinstalled = 2;
				if (x.description) {
					y = x.description;
					nbrFlashversion = y.charAt(y.indexOf('.')-1);
				}
			} else {
				flashinstalled = 1;
			}

			if (navigator.plugins["Shockwave Flash 2.0"]) {
				flashinstalled = 2;
				nbrFlashversion = 2;
			}

		} else if (navigator.mimeTypes && navigator.mimeTypes.length) {
			x = navigator.mimeTypes['application/x-shockwave-flash'];
			if (x && x.enabledPlugin) {
				flashinstalled = 2;
			} else {
				flashinstalled = 1;
			}
		}
	}

	if(nbrFlashversion < 8) {

		switch(document.body.id) {
			case "homepage":
				removeFlashElement("homepage_video","content_center");
				document.getElementById("content_center").className="homepage_image";
				break;
		}

		if(getCookie("oldflash") != 1){
			box = confirm("To view the video content on this site, please click \"OK\" to download the latest Flash plugin.  To view the site without video, click \"Cancel\"."); 
			
			if(box == true){
				window.location="http://www.macromedia.com/go/getflashplayer";
			}
			
			setCookie("oldflash","1",7,setCookieDomain(),"/");
			setCookie("dialup","1",7,setCookieDomain(),"/");
		}
	}
}

function noVideo(nbrVideo) {


		var arrVideo = new Array();

		//SWF Names
		arrVideo[0] = "homepage_video";
		if (getCookie(arrVideo[nbrVideo]) == 1) {
			if (document.getElementById(arrVideo[nbrVideo])) {
				getFlashMovieObject("homepage_video").SetVariable("makeItStop","1");
			}
		} else {
			setCookie(arrVideo[nbrVideo],"1",0,setCookieDomain(),"/");
		}
	}


function getFlashMovieObject(menu) {
  	if (window.document[menu]) {
      		return window.document[menu];
  	}
  	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    		if (document.embeds && document.embeds[menu]){
      			return document.embeds[menu]; 
		}
  	}
  	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1) 
	{
    		return document.getElementById(menu);
  	}
}

function removeFlashElement(childDiv,parentDiv) {
  
  	if(document.getElementById(childDiv)){
		
		var objParent = document.getElementById(parentDiv);

		var objChild = document.getElementById(childDiv);
  
		objParent.removeChild(objChild);
	}
}

/* END: FLASH RELATED FUNCTIONS */

/* BEGIN: SPEED DETECT */

function CurrentTime() {
	time = new Date();
	return time.getTime();
}

function returnKBPS(kbps){
	return kbps;
}

function GetKBPS(ST,section) {
	ET = CurrentTime();
	Factor	= 1000;
	DT = ((ET - ST)/Factor);
	DataSize = 29;
	kbps1    = (DataSize/DT);
	kbpsA    = ((kbps1*8)*10*1.02);
	kbyteA   = ((DataSize*10)/DT);
	kbps     = (Math.round(kbpsA)/10);
	kbyte    = (Math.round(kbyteA)/10);

	if (getCookie("dialup") == 1 || kbps <= 80) {

		switch(section) {
			case 1:
				removeFlashElement("homepage_video","content_center");
				document.getElementById("content_center").className="homepage_image";
			break;
		} 
		setCookie("dialup","1",7,setCookieDomain(),"/");
	}
}

/* END: SPEED DETECT */

/* BEGIN: COOKIE FUNCTIONS */

function getCookie(strName) {
	var nbrStart = document.cookie.indexOf(strName + "=");
	var nbrLength = nbrStart + strName.length + 1;
	if ((!nbrStart) && (strName != document.cookie.substring(0, strName.length))) {
		return null;
	}
	if (nbrStart == -1) return null;
	var nbrEnd = document.cookie.indexOf(';', nbrLength);
	if (nbrEnd == -1) nbrEnd = document.cookie.length;
	return unescape(document.cookie.substring(nbrLength, nbrEnd));
}

function setCookie(strName,strValue,strDuration,strDomain,strPath) {

	var strCookie="";

	if (strDuration == 0) {
		strCookie=strName+"="+escape(strValue)+";DOMAIN="+strDomain+";PATH="+strPath;
	} else {
		strCookie=strName+"="+escape(strValue)+";EXPIRES="+getExpiryDate(strDuration)+";DOMAIN="+strDomain+";PATH="+strPath;
	}

	document.cookie=strCookie;

	if(!getCookie(strName)){
		return false;
	}
	else{
		return true;
	}
}

function deleteCookie(strName) {
	setCookie(strName,"",-1,setCookieDomain(),"/");
}

function getExpiryDate(intDays) {

	var strUTC; //This will hold the UTC value

	strToday = new Date(); //Get the current date.
	strToday.setTime(strToday.getTime() + 3600000*24*intDays); //Add the number of days to keep alive
	strUTC = strToday.toUTCString(); //Convert the UTC back to a string.

	return strUTC; //Return the date.
}

function setCookieDomain() {
	var strDomain = document.domain;
	
	strDomain.replace('www.','');

	return '.'+strDomain;
}

/* END: COOKIE FUNCTIONS */

/* CONTACT FORM VALIDATOR */
function salesforceCheck() {

	if(document.getElementById('first_name').value == "") {
		alert('Please enter your first name.');
		document.getElementById('first_name').focus();
		return false;
	}

	if(document.getElementById('last_name').value == "") {
		alert('Please enter your last name.');
		document.getElementById('last_name').focus();
		return false;
	}

	if(document.getElementById('email').value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){
		alert('Please enter a valid email value.');
		document.getElementById('email').focus();
		return false;
	}

	if(document.getElementById('phone').value == "") {
		alert('Please enter a phone number.');
		document.getElementById('phone').focus();
		return false;
	}

	if(document.getElementById('zip').value == "") {
		alert('Please enter your zip code.');
		document.getElementById('zip').focus();
		return false;
	}

	return true;
}

function promoCheck() {

	if(document.getElementById('first_name').value == "") {
		alert('Please enter your first name.');
		document.getElementById('first_name').focus();
		return false;
	}

	if(document.getElementById('last_name').value == "") {
		alert('Please enter your last name.');
		document.getElementById('last_name').focus();
		return false;
	}

	if(document.getElementById('title').value == "") {
		alert('Please enter your title.');
		document.getElementById('title').focus();
		return false;
	}

	if(document.getElementById('company').value == "") {
		alert('Please enter your company.');
		document.getElementById('company').focus();
		return false;
	}

	if(document.getElementById('address_one').value == "") {
		alert('Please enter your address.');
		document.getElementById('address_one').focus();
		return false;
	}

	if(document.getElementById('city').value == "") {
		alert('Please enter your city.');
		document.getElementById('city').focus();
		return false;
	}

	if(document.getElementById('state').value == "") {
		alert('Please enter your state.');
		document.getElementById('state').focus();
		return false;
	}

	if(document.getElementById('zip').value == "") {
		alert('Please enter your zip code.');
		document.getElementById('zip').focus();
		return false;
	}

	if(document.getElementById('url').value == "") {
		alert('Please enter your website.');
		document.getElementById('url').focus();
		return false;
	}

	if(document.getElementById('email').value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		alert('Please enter your valid email value.');
		document.getElementById('email').focus();
		return false;
	}

	if(document.getElementById('phone').value == "") {
		alert('Please enter your phone number.');
		document.getElementById('phone').focus();
		return false;
	}

	if(!document.getElementById('rules').checked) {
		alert('Please read the contest rules.');
		document.getElementById('rules').focus();
		return false;
	}

	return true;
}

function claimCheck() {

	if(document.getElementById('contact_name').value == "") {
		alert('Please enter the contact name.');
		document.getElementById('contact_name').focus();
		return false;
	}

	if(document.getElementById('email').value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		alert('Please enter your valid email value.');
		document.getElementById('email').focus();
		return false;
	}

	if(document.getElementById('phone').value == "") {
		alert('Please enter the phone number.');
		document.getElementById('phone').focus();
		return false;
	}

	if(document.getElementById('recipient_name').value == "") {
		alert('Please enter the recipient name.');
		document.getElementById('recipient_name').focus();
		return false;
	}

	if(document.getElementById('recipient_add').value == "") {
		alert('Please enter the recipient address.');
		document.getElementById('recipient_add').focus();
		return false;
	}

	if((document.getElementById('date_tendered').value == "mm/dd/yyyy") || (document.getElementById('date_tendered').value == "")) {
		alert('Please enter the date tendered to MailExpress .');
		document.getElementById('date_tendered').focus();
		return false;
	}
	var datefilter=/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	var returnval=datefilter.test(document.getElementById('date_tendered').value);
	if (returnval==false){
		alert("Please enter date as mm/dd/yyyy/ format.");
		document.getElementById('date_tendered').focus();
		return false;
	}
	return true;
}
function updateCheck() {
	myOption = -1;

	for (i=document.form_update.claim_accepted.length-1; i > -1; i--) {
		if (document.form_update.claim_accepted[i].checked) {
			myOption = i;
		}
	}

	if (myOption == -1) {
		alert("Please choose one of the two possible options before update.");
		return false;
	}
	if((document.form_update.claim_accepted[0].checked) && (document.form_update.tracking_num.value == "")) {
		alert('Please assign a tracking number for this claim before update.');
		return false;
	}

	return true;
}
/* END CONTACT FORM VALIDATOR */

/* BEGIN: OTHER FUNCTIONS */
function init() {
	window.setTimeout('noVideo(0)',250);
}

function submitForm() {
		document.form_claims.submit();
}

/* END: OTHER FUNCTIONS */

function contactCheck() {

	if(document.request_form.first_name.value == "") {
		alert('Please enter your first name.');
		document.request_form.first_name.focus();
		return false;
	}
	if(document.request_form.last_name.value == "") {
		alert('Please enter your last name.');
		document.request_form.last_name.focus();
		return false;
	}
	if(document.request_form.job_title.value == "") {
		alert('Please enter your job title.');
		document.request_form.job_title.focus();
		return false;
	}
	if(document.request_form.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		alert('Please enter a valid email value.');
		document.request_form.email.focus();
		return false;
	}
	if(document.request_form.phone.value == "") {
		alert('Please enter your phone number.');
		document.request_form.phone.focus();
		return false;
	}
	if(document.request_form.zip.value == "") {
		alert('Please enter your zip code.');
		document.request_form.phone.focus();
		return false;
	}
	if(!document.request_form.Preferred_email.checked && !document.request_form.Preferred_phone.checked && !document.request_form.Preferred_mail.checked ) {
		alert('Please selcet your preferred contact method.');
		document.request_form.Preferred_email.focus();
		return false;
	}
	if(document.request_form.company.value == "") {
		alert('Please enter your company name.');
		document.request_form.company.focus();
		return false;
	}
	if(document.request_form.employees.value == "") {
		alert('Please select company employees.');
		document.request_form.employees.focus();
		return false;
	}
	if(document.request_form.industry.value == "") {
		alert('Please selcet company industry.');
		document.request_form.industry.focus();
		return false;
	}
	if(document.request_form.state.value == "") {
		alert('Please selcet state.');
		document.request_form.state.focus();
		return false;
	}
	if(!document.request_form.mail_flats.checked && !document.request_form.mail_printed.checked && !document.request_form.mail_parcels.checked && !document.request_form.mail_media.checked) {
		alert('Please selcet the materials type of your mail.');
		document.request_form.mail_flats.focus();
		return false;
	}
	return true;
}

function landingCheck() {

	if(document.landing_form.first_name.value == "") {
		alert('Please enter your first name.');
		document.landing_form.first_name.focus();
		return false;
	}
	if(document.landing_form.last_name.value == "") {
		alert('Please enter your last name.');
		document.landing_form.last_name.focus();
		return false;
	}

	if(document.landing_form.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		alert('Please enter a valid email value.');
		document.landing_form.email.focus();
		return false;
	}
	if(document.landing_form.Phone__c.value == "") {
		alert('Please enter your phone number.');
		document.landing_form.Phone__c.focus();
		return false;
	}

	if(document.landing_form.company.value == "") {
		alert('Please enter your company name.');
		document.landing_form.company.focus();
		return false;
	}
	return true;
}



function downloadCheck() {

	if(document.download_form.firstname.value == "") {
		alert('Please enter your first name.');
		document.download_form.firstname.focus();
		return false;
	}

	if(document.download_form.lastname.value == "") {
		alert('Please enter your last name.');
		document.download_form.lastname.focus();
		return false;
	}

	if(document.download_form.company.value == "") {
		alert('Please enter your company name.');
		document.download_form.company.focus();
		return false;
	}

	if(document.download_form.phone.value == "") {
		alert('Please enter your phone number.');
		document.download_form.phone.focus();
		return false;
	}

	if(document.download_form.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		alert('Please enter a valid email value.');
		document.download_form.email.focus();
		return false;
	}

	if(!document.download_form.privacy_policy.checked == 1) {
		alert('Please confirm that you have read the privacy statement.');
		document.download_form.privacy_policy.focus();
		return false;
	}

	return true;
}
function submitPriorityForm(cform){
  if(downloadCheck()){
		cform.submit();	
	}
}
	
function setPopUpLinks() {

	var hrefs = document.getElementsByTagName('A');

	for (i in hrefs) { 
		if (hrefs[i].className == 'pop_up') {
			hrefs[i].onclick = function() { window.open(this.href); return false; }
			hrefs[i].onkeypress = function() { window.open(this.href); return false; }
		}
	}
}

//AJAX Functions

var xmlhttp=false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
  try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}  
}

function changeContent(parentDiv,childDiv,page) {
	var element = document.getElementById(parentDiv);
	
	xmlhttp.open("GET", page);      

	xmlhttp.onreadystatechange = function() {
      		if (xmlhttp.readyState == 4){
			if(xmlhttp.status == 200 || xmlhttp.status == 0) {
				deleteChild(parentDiv,childDiv);
				element.innerHTML = xmlhttp.responseText;
				urchinTracker(page); 
											
					}
        		}
		}
     
	xmlhttp.send(null);
}
function deleteChild(parentDiv,childDiv) {

  	if(document.getElementById(childDiv)) {
		
		//Get a reference to the parent.
		var objParent = document.getElementById(parentDiv);

		//Get a reference to the child.
		var objChild = document.getElementById(childDiv);
  
		//Remove the child.
		objParent.removeChild(objChild);
	}
}

function changeTextContent(parentDiv,childDiv,page){
	
	changeContent(parentDiv,childDiv,page);
}

