// standard.js
// main external JavaScript file for www.bandwidth.com
// javascript by anders brownworth - abrownworth-javascript at bandwidth.com

 
/*******************************
 *	start general form functions
 *******************************/
 
function checkPhone( form ) {
  // checks npa / nxx / xxxx for rational looking values
  // abrownworth-jscript at bandwidth.com
  if ( ! isNumber( form.npa.value ) ||
       form.npa.value < 99 || 
       form.npa.value > 1000 ) {
    selectElement( form.npa );
    alert( "Please enter a valid area code." );
    return( false );

  }
  else if ( ! isNumber( form.nxx.value ) ||
            form.nxx.value < 99 || 
            form.nxx.value > 1000 ) {
    selectElement( form.nxx );
    alert( "Please enter a valid phone number." );
    return( false );

  }
  else if ( ! isNumber( form.xxxx.value ) ||
            form.xxxx.value.length != 4 ) {
    selectElement( form.xxxx );
    alert( "Please enter a valid phone number." );
    return( false );

  }
  else {
    return( true );

  }
}

function isNumber( value ) {
  if ( 1 + ( value - 1 ) == value ) {
    return( true );

  }
  else {
    return( false );

  }
}

function autoSkip( start, finish, maxLength ) {
  // if no text is selected (where possible to sense) and text is at 
  // or greater than maxLength, then go to next field
  var selectedTextLength = 0;
  if ( document.selection && document.selection.createRange ) {
    var selectedTextRange = document.selection.createRange( );
    selectedTextLength = selectedTextRange.text.length;

  }
  if ( selectedTextLength == 0 && start.value.length >= maxLength ) {
    selectElement( finish );

  }
}

function selectElement( element ) {
  // focus on element and select all text if possible
  element.focus( );
  if ( document.selection && document.selection.createRange ) {
    var range = element.createTextRange( );
    range.select( );

  }
}

/*******************************
 *	end general form functions
 *******************************/
 
 
function toggleTooltip(divId, inputHtml, height, top, left) {
	var tooltipDiv = document.getElementById( divId );
	tooltipDiv.style.height = height + "px";
	tooltipDiv.style.top = top + "px";
	tooltipDiv.style.left = left + "px";
	tooltipDiv.innerHTML = inputHtml;
}

function toggleBorder(element, on) {
	if( on ) {
		element.style.borderStyle = "solid";
		element.style.borderColor = "black";
	}
	else {
		element.style.borderStyle = "";
		element.style.borderColor = "";
	}
}


/********************************
 * start quote form functions
 ********************************/

var lookup = new Array("leadSource","campaignId","keyword","creative","externalId");
var recalledParams = "";
var params = "x"+document.location.search;
params = params.substring(2,params.length);
var paramsInURL = false;

function checkForParamsInURL() { 
	for(var a=0; a<lookup.length; a++) { // looks to see if there are parameters in the URL
		if(params.indexOf(lookup[a])>-1) {
			paramsInURL = true;
			break;
		}
	}
	return paramsInURL;
}

function checkForExistingCookie() {
	if(document.cookie.indexOf("marketingParameters")>-1) return true;
	else return false;
}


function extractParamsFromURL() {  // gets the parameters from the URL and puts them in a cookie
	terms = "";
	found = false;

	if(params) {
		paramArray = params.split("&"); //split out the parameters from the URL
	 	for(var b=0; b<lookup.length; b++) { // run through each item in the lookup array
			found = false;
			for(var a=0; a<paramArray.length; a++) { // run through each parameter from the URL
				if(paramArray[a].indexOf(lookup[b])>-1) {  
					terms+=paramArray[a].substring(paramArray[a].indexOf("=")+1,paramArray[a].length)+"|"; // assign the values from the URL into our variable
					found=true;
					break;
				}
				
			}
			if(!found) terms+="|";
		}
		terms = terms.substring(0,terms.length-1); // strip off the last pipe from terms

	var today = new Date()
 	var expires = new Date()
  	// valid for 1 year
 	expires.setTime(today.getTime() + 24*365*3600000)
		
	document.cookie = "marketingParameters=" + terms + "; expires=" + expires.toGMTString() ;
	}
}

function extractParamsFromCookie() {  // gets the parameters from the marketingParameters cookie
	crumb = document.cookie.split(";");
	marketingcrumb = -1;

	for(var a=0; a<crumb.length; a++) {
		if(crumb[a].indexOf("marketingParameters")>-1) {
			crumb[a] = crumb[a].substring(crumb[a].indexOf("=")+1,crumb[a].length)
			recalledParams = crumb[a].split("|");
			break;
		}
	}

}

function populateForm() {
	if(recalledParams[0]) document.quoteForm["lead_source"].value = recalledParams[0];
	document.quoteForm["Campaign_ID"].value = recalledParams[1]; 
	document.quoteForm["00N30000000cWrP"].value = recalledParams[2];
	document.quoteForm["00N30000000cXEQ"].value = recalledParams[3];
	document.quoteForm["00N30000000cbhr"].value = recalledParams[4];
}

function trackingController() {
	if(checkForParamsInURL()) extractParamsFromURL();
	else if(checkForExistingCookie()) {
		extractParamsFromCookie();
		populateForm()
	}
}

function checkForm( ) {
  form = document.quoteForm;
  flag = true;

  if ( ! checkPhone( form ) ) {
    flag = false;

  } 
  else if ( form.street.value == "" ) {
    form.street.focus( );
    alert( 'You must provide your street address.' );
    flag = false;

  }
  else if ( form.zip.value == "" ) {
    form.zip.focus( );
    alert( 'You must provide your zipcode.' );
    flag = false;

  }
  else if ( form.last_name.value == "" ) {
    form.last_name.focus( );
    alert( 'Please provide your name.' );
    flag = false;

  }
  else if ( form.email.value == "" ) {
    form.email.focus( );
    alert( 'Please provide your email address.' );
    flag = false;

  }
  else if ( form.company.value == "" ) {
    form.company.focus( );
    alert( 'Please provide your company name.' );
    flag = false;

  }

  if( flag ) {
    if ( form.product.selectedIndex == 0 ) {
      form.retURL.value = "content/voip0d1b.html?page=voip_quote_thanks";
    }
    form.phone.value = form.npa.value + "-" + form.nxx.value + "-" + form.xxxx.value;
    form.elements[0].value = form.product.value;
  }
  return( flag );
}


function checkForm2( ) {
  form = document.quoteForm;
  flag = false;

  if ( ! checkPhone( form ) ) {
    flag = false;

  } 
  else if ( form.street.value == "" ) {
    form.street.focus( );
    alert( 'You must provide your street address.' );
    flag = false;

  }
  else if ( form.zip.value == "" ) {
    form.zip.focus( );
    alert( 'You must provide your zipcode.' );
    flag = false;

  }
  else if ( form.full_name.value == "" ) {
    form.full_name.focus( );
    alert( 'Please provide your name.' );
    flag = false;

  }
  else if ( form.email.value == "" ) {
    form.email.focus( );
    alert( 'Please provide your email address.' );
    flag = false;

  }
  else if ( form.company.value == "" ) {
    form.company.focus( );
    alert( 'Please provide your company name.' );
    flag = false;

  }
  else {
  	flag = true;
  }

  if( flag ) {
    if ( form.product.selectedIndex == 0 ) {
      //form.retURL.value = "http://www.bandwidth.com/content/voip?page=voip_quote_thanks";
    }
    form.phone.value = form.npa.value + "-" + form.nxx.value + "-" + form.xxxx.value;
    form.elements[0].value = form.product.value;
	var splitName = form.full_name.value.split(" ");
    document.getElementById('first_name').value = splitName[0];
	document.getElementById('last_name').value = ((splitName.length == 2) ? splitName[1] : " ");
		
  	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
  
  	// declare a cookie expiration of 1 minute from now
	var expires = new Date( today.getTime() + (1000*60) );
	
	// build up the "variableTransportCookie"
  	document.cookie = 'variableTransportCookie='
						+ escape(form.product.value) + '|'
						+ escape(form.full_name.value) + '|'
						+ escape(form.email.value) + '|' 
						+ escape(form.company.value) + '|'
						+ escape(form.npa.value) + '|'
						+ escape(form.nxx.value) + '|'
						+ escape(form.xxxx.value) + '|'
						+ escape(form.street.value) + '|' 
						+ escape(form.zip.value) + '|' 
						+ escape(form.description.value) + ';expires=' + expires.toGMTString() + ';path=/';
  }
  return( flag );
}

/********************************
 * end quote form functions
 ********************************/
 
 
 
 
 
 
/****************************
 *	start misc. functions
 ****************************/

function popWindow( theURL, winName, features ) {
  window.open( theURL, winName, features );
}

/****************************
 *	end misc. functions
 ****************************/







/******************************************
 *	start partner application functions
 ******************************************/

function changeForm( ) {

	if( document.getElementById('affiliate').checked )
	{
		document.location = "content/partners12ef.html?page=application&amp;program=affiliate";
	}
	else if( document.getElementById('var').checked ) 
	{
		document.location = "content/partnersb78c.html?page=application&amp;program=var";
	}
}

function prependDollarSign( amount ) {
	return "$" + amount;
}

/******************************************
 *	end partner application functions
 ******************************************/
 
 
 
 
 

/***************************************
 *	start customer portal functions
 ***************************************/
 
function replaceUsername( ) {
  var field = document.passportForm.un;

  if ( field.value == "username" ) {
    field.value = "";

  }

}

function replacePassword( ) {
  var field = document.passportForm.pw;

  if ( field.value == "password" ) {
    field.value = "";

  }

}

function checkPassportForm( ) {
  form = document.passportForm;
  flag = true;

  if ( form.un.value == "" || form.un.value == "username" ) {
    form.un.focus( );
    alert( 'You must provide your username.' );
    flag = false;

  }
  else if ( form.pw.value == "" || form.pw.value == "password" ) {
    form.pw.focus( );
    alert( 'You must provide your password.' );
    flag = false;

  }
  return( flag );
}

/***************************************
 *	end customer portal functions
 ***************************************/

// Opens a new window to the specified URL.
function navigateTo( url )
{
	if( url == null || url.length == 0 )
        	alert("A URL must be specified to navigate to.");
                
        var windowFeatures = 'width=800,height=600,dependent=no,scrollbars=yes,top=0,left=0';
        
	window.open(url, 'newWindow', windowFeatures); 
} 
