//--> VALIDATE FORM
function validate_form(){

	var name = document.getElementById('name').value;
	var address = document.getElementById('address').value;
	var city = document.getElementById('city').value;
	var state = document.getElementById('state').value;
	var zip = document.getElementById('zip').value;

	if(name.length == 0 || address.length == 0 || city.length == 0 || state.length == 0 || zip.length == 0){
		alert('You must enter you name, address, city, state, and zip code');
		return false;
	}

}

//--> VALIDATE FORM Bottom of page
function validate_form_bot(){

	var name = document.getElementById('name-bot').value;
	var address = document.getElementById('address-bot').value;
	var city = document.getElementById('city-bot').value;
	var state = document.getElementById('state-bot').value;
	var zip = document.getElementById('zip-bot').value;

	if(name.length == 0 || address.length == 0 || city.length == 0 || state.length == 0 || zip.length == 0){
		alert('You must enter you name, address, city, state, and zip code');
		return false;
	}

}

//--> VALIDATE FORM Modal Box
function validate_form_modal(){

	var name = document.getElementById('name-modal').value;
	var address = document.getElementById('address-modal').value;
	var city = document.getElementById('city-modal').value;
	var state = document.getElementById('state-modal').value;
	var zip = document.getElementById('zip-modal').value;
	var phone = document.getElementById('phone-modal').value;
	var email = document.getElementById('email-modal').value;

	if((address.length == 0 && city.length == 0 && state.length == 0 && zip.length == 0) && phone.length == 0 && email.length == 0){
		alert('Please fill out either your address, phone number, or email address');
		return false;    
	}

}


/**
 *
 * Slideshow
 *
 */
var timing = [0,5,5,5,5,5,11];
function slide(num,name,fadedur){

	new Effect.Fade(name+num);
	setTimeout('new slide_next('+num+',\''+name+'\','+fadedur+')', 100);

}

function slide_next(num,name,fadedur){

	var num = parseInt(num+1);
	if(num >= timing.length) num = 1;
	new Effect.Appear(name+num,{duration: fadedur});
	setTimeout('new slide('+num+',\''+name+'\','+fadedur+')', timing[num] * 1000);

}

function findAndReplace(searchText, replacement, searchNode) {
    if (!searchText || typeof replacement === 'undefined') {
        // Throw error here if you want...
        return;
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'g') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html,head,style,title,link,meta,script,object,iframe';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function(){
                var html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML = html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}


function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
	var pair = vars[i].split("=");
	if (pair[0] == variable) {
		return pair[1];
	}
  }
}

onload = function(){


	var append_to_links = 'no';
	var append_syntax = '';

	if(getQueryVariable('adw') == 'y' || location.href.search(/lgp/i) > 0){
		findAndReplace("\\(?\\d{3}\\)?[^\\d]+\\d{3}[^\\d]+\\d{4}", "(714) 912-9806");
		append_to_links = 'yes';
		append_syntax = '?adw=y';
	}

	if(append_to_links == 'yes'){
		var o = document.links;
		var query = document.location.search;
		for(var i=0;i<o.length;i++){
			o[i].href = o[i].href+append_syntax;
		}
	}


}


function count(action)
{

	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }

	xmlhttp.open("GET","http://www.ptkn.com/admin/counter/"+action+"/",true);
	xmlhttp.send();

}
