/* 
   
	/scripts/common.js
	common javascript functions
		 
*/		 

function formatSSN(input) {
    var regexp = /^([\d]{3})-?([\d]{2})-?([\d]{4})$/;
    if (!input.value.match(regexp)) return;
    var parts = regexp.exec(input.value);
    input.value = parts.slice(1).join("-");
}

function formatPhone(input) {
    var regexp = /^([\d]{3})[-\.]?([\d]{3})[-\.]?([\d]{4})$/;
    if (!input.value.match(regexp)) return;
    var parts = regexp.exec(input.value);
    input.value = parts.slice(1).join("-");
}

function formatDate(input) {
    if (input.value.length == 5 || input.value.length == 7) {
        input.value = "0" + input.value;
    }
    var regexp = /^([\d]{2})[-\/]?([\d]{2})[-\/]?([\d]{2})([\d]{2})?$/;
    if (!input.value.match(regexp)) return;
    var parts = regexp.exec(input.value).slice(1);
    if ((parts[parts.length - 1] == '') || (typeof parts[parts.length - 1] == 'undefined')) parts.pop();
    if (parts.length == 3) {
        var rightNow = new Date();
        var thisYear = rightNow.getFullYear() % 100;
        var thisCentury = (rightNow.getFullYear() - thisYear) / 100;
        if (parts[2] > ((thisYear) + 10)) {
            parts[2] = (thisCentury - 1) + parts[2];
        } else {
            parts[2] = thisCentury + parts[2];
        }
    }
    if (parts.length == 4) {
        parts[2] = parts[2] + parts.pop();
    }
    input.value = parts.join("/");
}


function getAsyncXML1(requestURL, callback) 
{
	var xmlHttp1;

	// Mozilla
	if (window.XMLHttpRequest) {
		xmlHttp1 = new XMLHttpRequest();
		xmlHttp1.onreadystatechange = callback;
		xmlHttp1.open("POST", requestURL, true);
		xmlHttp1.send(null);

	// IE
	} else {
		xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlHttp1) {
			xmlHttp1.onreadystatechange = callback;
			xmlHttp1.open("POST", requestURL, true);
			//xmlHttp1.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xmlHttp1.send();
		}
	}

	return xmlHttp1;
}


function checkValueFormat ( field, precision ) {

	var formObj = getFormObj('quoteForm');
	var thisValue = formObj[field].value;
	
	if ( precision === undefined ) precision = 2; 

	if ( thisValue == '' ) {
		formObj[field].value = '0.00';
	} else {
		var newVal = new Number(decommafy(thisValue));
		var testVal = newVal.toFixed(precision);
		if ( testVal == 'NaN' ) {
			formObj[field].value = '0.00';
			alert('Invalid number entered. Setting field to 0.');
		} else {
			formObj[field].value = newVal.toFixed(precision);
		}
	}
}


function decommafy(argvalue) {
	var re = /,/g;
	return argvalue.replace(re, "");
}


function getElement(psID) {
    if (document.all) {
    	if ( isElement(document.all[psID]) ) {
	        return document.all[psID];
		}
    } else {
    	if ( isElement(document.getElementById(psID)) ) {
        	return document.getElementById(psID);
        }
    }
    return false;
}

function getParentElement(psID) 
{
    if(document.all) {
    	if ( isElement(parent.document.all[psID]) ) {
        	return parent.document.all[psID];
        }
    } 
    else {
    	if ( isElement(parent.document.getElementById(psID)) ) {
        	return parent.document.getElementById(psID);
        }
    }
	return false;
}


function isElement(o) {
	return o && o.nodeType == 1 && typeof o.tagName === "string"
}

function isInt( str )
{
	var i = parseInt(str);
	if (isNaN (i)) return false;

	i = i.toString();
	if (i != str) return false;

	return true;
}



// Show hide a block by an ID value
function showHideById (blockId, imgBlockId) {

	var obj = document.getElementById(blockId);

	// Show/hide the element
	if( obj.style.display == 'inline' ) {
		obj.style.display = 'none';
	} else {
		obj.style.display = 'inline';
	}


	// Change the show/hide graphic (if applicable)
	if ( imgBlockId ) {

		var imgBlock = document.getElementById(imgBlockId);

		if( obj.style.display == 'inline' ) {
			imgBlock.src = '/images/actions/action_expand.jpg';
		} else {
			imgBlock.src = '/images/actions/action_collapse.jpg';
		}
	}
}


// Show hide a block 
function showHide (blockId, blockType, imgBlockId) {

	tags = document.getElementsByTagName(blockType);

	for ( i = 0 ; i < tags.length ; i++ ) {
		if ( tags[i].id == blockId ) {

			if( tags[i].style.display == 'none' ) {
				tags[i].style.display = '';
			} else {
				tags[i].style.display = 'none';
			}
		}
	}

	// Change the show/hide graphic (if applicable)
	if ( imgBlockId ) {

		var imgBlock = document.getElementById(imgBlockId);

		if( imgBlock.src.indexOf('collapse') == -1 ) {
			imgBlock.src = '/images/actions/action_collapse.jpg';
		} else {
			imgBlock.src = '/images/actions/action_expand.jpg';
		}
	}
}



// Show/hide a set of blocks, swapping one for the other
function blockSwap (block1Id, block2Id, blockType, imgBlockId) {

	tags = document.getElementsByTagName(blockType);

	for ( i = 0 ; i < tags.length ; i++ ) {
		if ( tags[i].id == block1Id ) {

			if( tags[i].style.display == 'none' ) {
				tags[i].style.display = '';
			} else {
				tags[i].style.display = 'none';
			}
		}

		if ( tags[i].id == block2Id ) {

			if( tags[i].style.display == 'none' ) {
				tags[i].style.display = '';
			} else {
				tags[i].style.display = 'none';
			}
		}
	}

	// Change the show/hide graphic (if applicable)
	if ( imgBlockId ) {

		var imgBlock = document.getElementById(imgBlockId);

		if( imgBlock.src.indexOf('collapse') == -1 ) {
			imgBlock.src = '/images/actions/action_collapse.jpg';
		} else {
			imgBlock.src = '/images/actions/action_expand.jpg';
		}
	}
}


// Reset a form to have no entered/selected data
function form_reset ( formName ) {


	form = getFormObj(formName);

	for ( thisIX = 0 ; thisIX < form.elements.length; thisIX++ ) {
		switch ( form.elements[thisIX].type ) {
			case 'hidden':
			case 'submit':
				// do nothing
				break
			case 'select-one':
				form.elements[thisIX].selectedIndex = 0
				break
			case 'checkbox':
				form.elements[thisIX].checked = false
				break
			default:
				form.elements[thisIX].value = ''
				break
		}
	}
}


function getFormObj ( formName ) {
	return document.forms[formName];
}


function setRadioValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function getRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function formatPhone (thisField) {
	thisPhone = thisField.value;
	thisPhone = thisPhone.replace(/\D/g,'');
	if ( isNaN(thisPhone)) {
		alert('Invalid phone number');
		thisField.value = "";
		thisField.focus();
	} else if ( thisPhone.length >= 3 && thisPhone.length <= 5 ) {
		thisPhone = thisPhone.replace(/^(\d{3})(\d{0,3})/,'$1-$2');
	} else if ( thisPhone.length >= 6 && thisPhone.length <= 10 ) { 
		thisPhone = thisPhone.replace(/^(\d{3})(\d{3})/,'$1-$2-');
	} else if ( thisPhone.length >= 11 ) { 
		thisPhone = thisPhone.replace(/^(\d{3})(\d{3})(\d{4})/,'$1-$2-$3 ');
	}
	thisField.value = thisPhone;
}
