var newwin ='';

function newWd(page,width, height) {
    newwin = window.open(page, '', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height); 
	newwin.focus();
} 
function sure()
{
	return confirm('Are you sure you would like to remove this?');
}

var isIE = (document.all) ? true : false;
var displayType = (isIE) ? 'block' : 'table-row-group';

function toggleRows(cid,on_off) {
	if (document.getElementById) {
		document.getElementById(cid).style.display = (on_off) ? displayType : 'none';
	}
}

function showAttachmentDiv(type)
{
	if (type != 'attachments')
		if (parent.document.getElementById('attachments_div'))
			parent.document.getElementById('attachments_div').style.display = 'none';
		else
			document.getElementById('attachments_div').style.display = 'none';
		
	
	if (type != 'cover_letter'){
		if (parent.document.getElementById('cover_letter_div'))
			parent.document.getElementById('cover_letter_div').style.display = 'none';
		else
			document.getElementById('cover_letter_div').style.display = 'none';
			
	}
	
	if (type != 'replace_cover_letter'){
		if (parent.document.getElementById('replace_cover_letter_div'))
			parent.document.getElementById('replace_cover_letter_div').style.display = 'none';
		else
			document.getElementById('replace_cover_letter_div').style.display = 'none';
			
	}
	
	if (type != 'archived_div'){
		if (parent.document.getElementById('archived_div'))
			parent.document.getElementById('archived_div').style.display = 'none';
		else
			document.getElementById('archive_div').style.display = 'none';
			
	}
		
	if (type != '')
		document.getElementById(type+'_div').style.display = 'block';	

	if (type == ""){
		//document.getElementById('attachments_div').style.display = 'none';
		parent.document.getElementById('cover_letter_div').style.display = 'none';
		parent.document.getElementById('replace_cover_letter_div').style.display = 'none';
		parent.document.getElementById('attachments_div').style.display = 'none';
	}	
		
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		home_over = newImage("/pics/home-over.gif");
		services_over = newImage("/pics/services-over.gif");
		licenses_permits_over = newImage("/pics/licenses_permits-over.gif");
		faqs_over = newImage("/pics/faqs-over.gif");
		about_us_over = newImage("/pics/about_us-over.gif");
		myforms_over = newImage("/pics/myforms-over.gif");
		preloadFlag = true;
	}
}

function _getElement(id)
{
	if (document.getElementById && document.getElementById(id)) {
		return document.getElementById(id);
	}
	else if (document.all && document.all[id]) {
		return document.all[id];
	}
	else {
		return false;
	}
}

function setBlockDisplay(id,display_type)
{
	_getElement(id).style.display = display_type;
}

function toggleBlock(id)
{
	if (_getElement(id).style.display == 'none') {
		_getElement(id).style.display = 'block';
		_getElement(id+'pic').src = '/pics/dtable.opened.png';
	}
	else {
		_getElement(id).style.display = 'none';
		_getElement(id+'pic').src = '/pics/dtable.closed.png';
	}
}

function setVisibility(id,is_visible) 
{
	_getElement(id).style.visibility = (is_visible) ? 'visible' : 'hidden';
}

function getSelectedValue(select)
{
	if (!select ||
		select.options == null || 
		!select.options || 
		select.options.length == 0) {
			return '';
	}
	
	return select.options[select.selectedIndex].value;	
}

function calculateTotalPrice(fulfillment_price,self_file_price,all_docs_price,shipping_price,warn)
{
	var total;
	if (warn){
		if (!shipWarning())
			return false;
	}
	if (document.form1.is_fulfillment[0] && document.form1.is_fulfillment[0].checked){
		
		document.form1.ship_method_id.disabled = false;
		total = parseFloat(fulfillment_price);
	}
	else{
		if (document.form1.ship_method_id)
			document.form1.ship_method_id.disabled = true;
		total = parseFloat(self_file_price); 
	}

	if(document.form1.all_docs && document.form1.all_docs.checked)
		total = parseFloat(total) + parseFloat(all_docs_price);
	total += shipping_price;
	total = total.toFixed(2);	
	if (document.getElementById('total_div'))
		document.getElementById('total_div').innerHTML = '$'+total;
	
	
}

function shipWarning()
{
	conf = confirm('This will update all forms going to the same address!');
	if (conf){
		old_ship_method_id = document.form1.ship_method_id.value;
		return true;
	}
	else{
		document.form1.ship_method_id.value = old_ship_method_id;
		return false;
	}
}

function changeVisibility(id)
{
	//if visiblility is not set it will show not hide it
	if (_getElement(id).style.visibility == 'visible')
		setVisibility(id,false);	
	else{
		setVisibility(id,true);	
	}
	return;	
	//alert(_getElement(id).style.visibility.hidden);
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

var last_balloon_content_id;
var last_balloon_relative_to;

function showBalloon(content_id,relative_to)
{
	if (content_id == null) content_id   = last_balloon_content_id;
	if (relative_to == null) relative_to = last_balloon_relative_to;
	
	last_balloon_content_id = content_id;
	last_balloon_relative_to = relative_to;
	
	_getElement('balloon_content').innerHTML = _getElement(content_id).innerHTML;
	_getElement('balloon').style.visibility = 'visible';
	
	var x = findPosX(_getElement(content_id));
	var y = findPosY(_getElement(content_id));
	
	var window_width = 0;
	
	if (document.all) {
		window_width=document.body.offsetWidth;
	}
	else if (window.innerWidth) {
		window_width=window.innerWidth - 13;
	}
	
	if (x > (window_width - 260)) {
		x = window_width - 300;
		setBlockDisplay('balloon_arrow_left','none');
		setBlockDisplay('balloon_arrow_right','none');
	}
	else {
		setBlockDisplay('balloon_arrow_left','block');
		setBlockDisplay('balloon_arrow_right','none');
	}
	
	_getElement('balloon').style.left = x + 'px';
	_getElement('balloon').style.top  = y + 'px';
}

function hideBalloon()
{
	_getElement('balloon').style.visibility = 'hidden';
}

function checkAgree()
{
	if (document.completeFrm.reviewed.checked)
		return true;
	else{
		alert('You must review before moving on');
		return false;	
	}
		
}

function viewTab(tab_id)
{
	setBlockDisplay('zipcode_search','none');
	setBlockDisplay('location_search','none');
	setBlockDisplay('browse_search','none');
	setBlockDisplay('recent_search','none');
	
	setBlockDisplay(tab_id,'block');
}

function printNewWd(page)
{
	var w = window.open(page, '', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600'); 
	w.focus();
	//newwin.print();
	//newwin.close();
}

function checkAmount(frmFld)
{	

	var places = 2;
	
	if(checkAmount.arguments.length > 1)
		places = checkAmount.arguments[1];
	if (!validateDollars(frmFld.value)) {
		if (old_amount)
			frmFld.value = old_amount;
		else	
			frmFld.value = '0.00';
		alert("Invalid Amount");
		return false;
	}
	else {
		if (old_amount)
			old_amount = dollars(frmFld.value,places);
		frmFld.value = dollars(frmFld.value,places);
		return true;
	}
}

// round to 2 decimal places
function dollars(x) 
{	
	var places = 2;
	var zeroes='';
	var amount;
	var roundto;
	if(dollars.arguments.length > 1)
		places = dollars.arguments[1];
	for(var i=0; i < places ;i++){
		zeroes +='0' 
	}
	roundto = parseInt(1 + zeroes);
	// round number
	amount = Math.round(x*roundto)/roundto;
	// convert to string
	amount = amount.toString();
	// get position of decimal
	dPlace = amount.indexOf('.',0);
	

	if (dPlace == -1)  // no decimal found
		return amount + '.' + zeroes;
		//return amount + '.00';
	else {
		decimals = amount.substring(dPlace+1,amount.length);	
		if (decimals.length == places)
			return amount;
		else
			return amount + '0';
	}
}

function validateDollars(string) 
{
    if (!string) return false;
    var chars = "0123456789.";
    
    for (var i = 0; i < string.length; i++) {
       if (chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}

function updatePrintStatus(form_id)
{
	_getElement('print1_'+form_id).innerHTML = '<img src="/pics/checkmark2.gif" width=10 height=10>';
	_getElement('print2_'+form_id).innerHTML = 'Print';
	setTimeout('window.location.reload( true );',4000);
	
	
}

function changePic(id,width,height,image)
{
	_getElement(id).width = width;
	_getElement(id).height = height;
	_getElement(id).src = '/pics/'+image;	
}

function trim(str) {
	if(str.length < 1) {
		return"";
	}

	str = rTrim(str);

	str = lTrim(str);

	if(str==""){
		return "";
	}
	else{
		return str;
	}
} //End Function

function rTrim(str){
	var w_space = String.fromCharCode(32);
	var v_length = str.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(str.charAt(iTemp) == w_space){
		}
		else{
			strTemp = str.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	return strTemp;

} //End Function

function lTrim(str){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = str.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
	if(str.charAt(iTemp) == w_space){
	}
	else{
		strTemp = str.substring(iTemp,v_length);
		break;
	}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

function getAcrobatVersion()
{
	var acrobat=new Object();

	acrobat.installed=false;
	acrobat.version='0.0';

	if (navigator.plugins && navigator.plugins.length) {
		for (x=0; x < navigator.plugins.length; x++) {
			if (navigator.plugins[x].description.indexOf('Adobe Acrobat') != -1) {
				acrobat.version=parseFloat(navigator.plugins[x].description.split('Version ')[1]);
				
				if (acrobat.version.toString().length == 1) acrobat.version+='.0';
				acrobat.installed=true;
				break;
			}
		}
	}
	else if (window.ActiveXObject) {
		for (x=2; x<10; x++) {
			try {
				oAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
				if (oAcro) {
					acrobat.installed=true;
					acrobat.version=x+'.0';
				}
			}
			catch(e) {}
		}
	
		try {
			oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
	
			if (oAcro4) {
				acrobat.installed=true;
				acrobat.version='4.0';
			}
		}
		catch(e) {}
	
		try {
			oAcro7=new ActiveXObject('AcroPDF.PDF.1');
	
			if (oAcro7) {
				acrobat.installed=true;
				acrobat.version='7.0';
				
				// get real version
				vers = oAcro7.GetVersions().split(",");
				vers = vers[1].split("=");
				acrobat.version = vers[1];
			}
		}
		catch(e) {}
	}
	return acrobat;
}

function setPointer(theRow,classn)
{
	if (typeof(theRow.style) == 'undefined') {
        return false;
    }
    
    /*
    if (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {
        return false;
    }

    var row_cells_cnt           = theRow.cells.length;
    for (var c = 0; c < row_cells_cnt; c++) {
        	theRow.cells[c].bgColor = thePointerColor;
    }*/
    
    theRow.className = classn;
}

var opt = new Array(
	new Array(new Array('0','None')),	
	new Array(new Array('0','Every Day')),	
	new Array(new Array('0','Sunday'),
			  new Array('1','Monday'),
			  new Array('2','Tuesday'),
			  new Array('3','Wednesday'),
			  new Array('4','Thursday'),
			  new Array('5','Friday'),
			  new Array('6','Saturday')),	
	new Array(31)
);

for (var x=0;x<31;x++)
	opt[3][x] = new Array(x+1,x+1);

opt[3][30][1] += " (will run the last of the month)";

function setOptions(fld,index)
{
	var options = opt[index];
	while (fld.options.length) fld.options[0] = null;
	for (var x=0;x<options.length;x++) {
		fld.options[x] = new Option(options[x][1],options[x][0]);
	}
}

function selectOption(fld,val)
{
	for (var x=0;x<fld.options.length;x++) {
		if (fld.options[x].value == val) {
			fld.options[x].selected = true;
			break;	
		}
	}
}