// JavaScript Document

function openwin(url,name){
   window.open (url, name, config='height=500,width=550, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
}


function ascii_value (c)
{
	// restrict input to a single character
	c = c . charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function NumOnly(item){
	temp1=item.length;
	temp2='';
		for(x=0;x<temp1;x++){
		//remove non numbers
		tempitem=item.substr(x,1);
		//alert(tempitem + " " + ascii_value(tempitem) );
				if (ascii_value(tempitem)>47 && ascii_value(tempitem)<58){
					temp2=temp2+item.substr(x,1);
				}
		}
	return temp2;
}

function check(ourform){
		
	var good=true;
	
	//fix phone
	phone=ourform.p.value;
	//alert(phone);
	//remove non numbers
	phone=NumOnly(phone);
	//check that 10 or 11 numbers left
	if (phone.length>11 || phone.length<10 ){
		//kill
		good=false;
		msg='Please enter a valid phone number.';
		formpart='p';
	}
	//get rid of 1 
	if (phone.length==11){
	if (Left(phone,1)==1){
		phone=Right(phone,10);
	}else{
	//not a 1	
		good=false;
		msg='Please enter a valid phone number.';
		formpart='p';
		}
	}
	
	ourform.p.value=phone;
	
	if(good){
		// Google Analytics
		pageTracker._trackPageview('/submitsearch');
		ourform.submit();
		//alert('good');
	}else{
	
	alert(msg);
	ourform[formpart].focus();
	return false;
	}
	
	
}

function setzeCurser() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
         }
      }
   }
}