// JavaScript Document
var cell = 0;
var office = 0;
var fax = 0;

function init(show)
{
	var divs = document.getElementsByTagName("div");
	var inputColl = document.getElementsByTagName("input");
	var selectColl = document.getElementsByTagName("select");

	document.getElementById('center').style.height = 'auto';

	// Empties all textboxes
	if (inputColl!=null){
		for (i=0; i < inputColl.length; i++)
			inputColl[i].value = "";
	}
	// Empties all selectboxes
	if (selectColl!=null){
		for (i=0; i < selectColl.length; i++)
			selectColl[i].selectedIndex = -1;
	}

	document.request.requestSubmit.value = "Submit";
	document.request.jobDistance.value = 0;
	document.request.jobHeight.value = 0;
	document.request.month.selectedIndex = 0;
	document.request.year.selectedIndex = 0;
	
	for (var i = 0; i < divs.length; i++)
	{
		//divs[i].style.display = 'none'; 	// Hides div
		var divId = divs[i].id;
		if ( divId.indexOf("raise_") == 0 )
			if( "raise" == show )
				eval("document.getElementById('" + divId + "').style.display = 'block'");
		if ( divId.indexOf("move_") == 0 )
			if( "move" == show )
				eval("document.getElementById('" + divId + "').style.display = 'block'");
		if ( divId.indexOf("all_") == 0 )
			eval("document.getElementById('" + divId + "').style.display = 'block'");
	}
	
	if( "raise" == show ){
		document.getElementById("raiseForm").innerHTML = "Clear Form";
		document.getElementById("moveForm").innerHTML = "Moving Form";
		document.getElementById('move_000').style.display = 'none';
		document.getElementById('move_000').value = '0';
	} else{
		document.getElementById("moveForm").innerHTML = "Clear Form";
		document.getElementById("raiseForm").innerHTML = "Raising Form";
		document.getElementById('raise_000').style.display = 'none';
		document.getElementById('raise_000').value = '0';
	}
	
	document.getElementById('date').style.display = 'block';
}
function showDia(label, link)
{
	document.getElementById(label).style.display = 'block';
	document.getElementById(link).href = "javascript:hideDia(\'" + label + "\', \'" + link + "\')\;";
}
function hideDia(label, link)
{
	document.getElementById(label).style.display = 'none';
	document.getElementById(link).href = "javascript:showDia(\'" + label + "\', \'" + link + "\')\;";
}
function replaceAt( i_startPos, i_length, i_newString )
{
   var i_string = this.toString();

   var result = i_string.substr( 0, i_startPos);
   result += i_newString ;
   result += i_string.substr( i_startPos+i_length );

   return result ;
}

//----- Adding 2 new methods to String object.
String.prototype.replaceAt = replaceAt ;

function placeMonth()
{
	var str = document.request.otherTime.value;
	var month = document.request.month.value + " of ";
	var length = str.length;
	
	if (length > 10) {
		document.request.otherTime.value = str.replaceAt(0,length - 5,month); }
	else {
		document.request.otherTime.value = str.replaceAt(0,length,month); }
	document.request.otherTime.focus();
}
function placeYear()
{
	var str = document.request.otherTime.value;
	var year = document.request.year.value;
	var length = str.length;

	if ( str.charAt(length - 2) == "f" ){
		document.request.otherTime.value = str.replaceAt(length, 4,year); }
	else {
		document.request.otherTime.value = str.replaceAt((length - 4), 4,year); }
	document.request.otherTime.focus();
}

function sameBuildInfo(condition)
{
	if (condition != 0) {
    	document.request.buildAddress.value = document.request.addr.value;
		document.request.buildCity.value = document.request.city.value;
		document.request.buildState.value = document.request.state.value;
		document.request.buildZip.value = document.request.zip.value;
		document.getElementById("sameAboveBuild").innerHTML = "Clear";
		document.getElementById("sameAboveBuild").href = "javascript:sameBuildInfo('0');";
	} else {
	   	document.request.buildAddress.value = "";
		document.request.buildCity.value = "";
		document.request.buildState.value = "";
		document.request.buildZip.value = "";
		document.getElementById("sameAboveBuild").innerHTML = "Same As Above";
		document.getElementById("sameAboveBuild").href = "javascript:sameBuildInfo('1');";
	}
}

function sameSiteInfo(condition)
{
	if (condition != 0) {
    	document.request.siteAddress.value = document.request.buildAddress.value;
		document.request.siteCity.value =	document.request.buildCity.value;
		document.request.siteState.value = document.request.buildState.value;
		document.request.siteZip.value = document.request.buildZip.value;
		document.request.siteFound.value = document.request.buildFound.value;
		document.request.siteGrade.value = document.request.buildGrade.value;
		document.getElementById("sameAboveSite").innerHTML = "Clear";
		document.getElementById("sameAboveSite").href = "javascript:sameSiteInfo('0');";
	} else {
		document.request.siteAddress.value = "";
		document.request.siteCity.value = "";
		document.request.siteState.value = "";
		document.request.siteZip.value = "";
		document.request.siteFound.value = "";
		document.request.siteGrade.value = "";
		document.getElementById("sameAboveSite").innerHTML = "Same As Above";
		document.getElementById("sameAboveSite").href = "javascript:sameSiteInfo('1');";
	}
}


function addDiv (elment, fieldId) {
  if (document.getElementById) {
    var div = document.createElement('DIV');
      if (document.all) { // doesn't work in M14
        div.id = fieldId;
      }
      else if (document.getElementById) { // NN6 workaround
        input.setAttribute('id', fieldId);
      }
    elment.appendChild(div);
  }
  return div;
}

function addition(){
	var length = document.request.buildAdd.value;
	var inputWidName; 
	var inputLenName; 
	var strCaption; 
	var text;
	var div;
	
	var n = 1;
	while (document.getElementById("adds" + n)){
		document.getElementById("all_014").removeChild(document.getElementById("adds" + n));
		n = n + 1;
	}
		
	for (var i = 1; i <= length; i++){
		strCaption = "Addition #" + i + ": ";
		text = document.createTextNode(strCaption);
		inputWidName = "buildAddWid0" + i;
		inputLenName = "buildAddLen0" + i;
		div = document.createElement('DIV');
		div.style.marginLeft = "10px";
		div.id = "adds" + i;
		
		document.getElementById("all_014").appendChild(div);
			document.getElementById("adds" + i).appendChild(text);
				addField(document.getElementById("adds" + i), "text", inputWidName, "", 3);
				text = document.createTextNode("Width");
				document.getElementById("adds" + i).appendChild(text);
				
				addField(document.getElementById("adds" + i), "text", inputLenName, "", 3);
				text = document.createTextNode("Length");
				document.getElementById("adds" + i).appendChild(text);
	}
		//document.getElementById('addDia').style.display = 'block';
}

function addField (elment, fieldType, fieldClass, fieldName, fieldValue, fieldSize) {
     var input = document.createElement('INPUT');
	 
	 input.type = fieldType;
	 input.name = fieldName;
	 input.className = fieldClass;
	 input.id = fieldName;
	 input.value = fieldValue;
	 if (fieldSize != null)
	 	input.size = fieldSize;
	 
	 elment.appendChild(input);
}

function addPhone(label){
	var parentDiv = document.getElementById("request");
	var div = document.createElement('DIV');
	var br = document.createElement('BR');
	var strCaption = label + " #: ";
	var strId = label + "Div";
	var text = document.createTextNode(strCaption);
	
	if (label == "Cell")
		cell = 1;
	else if (label == "Office")
		office = 1;
	else
		fax = 1;
		
	div.id = strId;
	parentDiv.insertBefore(div, document.getElementById("all_020"));
	document.getElementById(strId).appendChild(text);
	addField(document.getElementById(strId), "text", "textbox", label, "", 15);
	
	document.getElementById(strId).appendChild(br);
	
	text = document.createTextNode("Best Time to Call: ");
	document.getElementById(strId).appendChild(text);
	addField(document.getElementById(strId), "checkbox", "checkbox", label + "TMorn", "Morning", null);
	
	text = document.createTextNode("Morning ");
	document.getElementById(strId).appendChild(text);
	addField(document.getElementById(strId), "checkbox", "checkbox", label + "TAfter", "Afternoon", null);
	
	text = document.createTextNode("Afternoon ");
	document.getElementById(strId).appendChild(text);
	addField(document.getElementById(strId), "checkbox", "checkbox", label + "TEven", "Evening", null);
	
	text = document.createTextNode("Evening ");
	document.getElementById(strId).appendChild(text);
	
	strCaption = label + "Phone";
	document.getElementById(strCaption).innerHTML = "Remove " + label;
	document.getElementById(strCaption).href = "javascript:removePhone(\"" + strId + "\", \"" + strCaption + "\", \"" + label + "\")\;";
}

function removePhone(divId, linkId, label) {
	if (label == "Cell")
		cell = 0;
	else if (label == "Office")
		office = 0;
	else
		fax = 0;
		
	document.getElementById("request").removeChild(document.getElementById(divId));
	document.getElementById(linkId).innerHTML = label + " Number";
	document.getElementById(linkId).href = "javascript:addPhone(\"" + label + "\")\;";
}

function validatePhone(form) {
	if (form.phone.value != "" )
		return validateNumber(form.phone, 'Please enter your 10 digit phone number: 1234567890', 10, 10);
	else if (document.getElementById("Cell"))
		return validateNumber(form.Cell, 'Please enter your 10 digit cell number: 1234567890', 10, 10);
	else if (document.getElementById("Office"))
		return validateNumber(form.Office, 'Please enter your 10 digit office number: 1234567890', 10, 10);
	else if (document.getElementById("Fax"))
		return validateNumber(form.Fax, 'Please enter your 10 digit fax number: 1234567890', 10, 10);
	else
		return validateNumber(form.phone, 'Please enter your 10 digit phone number: 1234567890', 10, 10);
}

function validateNumber(field, msg, min, max) {
	if (!min) { min = 0 }
	if (!max) { max = 255 }

	if ( (parseInt(field.value) != field.value) ||
             field.value.length < min ||
             field.value.length > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}

	return true;
}

function validateEmail(email, msg, optional) {
	if (!email.value && optional) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	if (!re_mail.test(email.value)) {
		alert(msg);
		email.focus();
		email.select();
		return false;
	}

	return true;
}

function validateString(field, msg, min, max) {
	if (!min) { min = 1 }
	if (!max) { max = 65535 }
	
	if (!field.value || field.value.length < min || field.value.max > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}

	return true;
}

function validateState(field, msg) {
	if (field.selectedIndex < 0) {
		alert(msg);
		field.focus();
		return false;
	}

	return true;
}