function bakeCookie(name,value) {
   argv=arguments;
   argc=arguments.length;
   expires=(argc>2) ? argv[2] : null;
   path=(argc>3) ? argv[3] : null;
   domain=(argc>4) ? argv[4] : null;
   secure=(argc>5) ? argv[5] : false;
   document.cookie=name+"="+escape(value) +
     ((expires === null) ? "" : ("; expires="+expires.toUTCString())) +
     ((path === null) ? "" : ("; path="+path)) +
     ((domain === null) ? "" : ("; domain="+domain)) +
     ((secure === true) ? "; secure" : "");
}

function eatCookie(name) {
   arg=name+"=";
   alen=arg.length;
   clen=document.cookie.length;
   i=0;
   while (i<clen) {
      j=i+alen;
      if (document.cookie.substring(i,j) == arg) {
          return eatCookieVal(j);
          }
      i=document.cookie.indexOf(" ",i) + 1;
      if (i === 0) {break;}
   }
}
function eatCookieVal(offset) {
   endstr=document.cookie.indexOf(";",offset);
   if (endstr == -1) {endstr=document.cookie.length;}
   return unescape(document.cookie.substring(offset,endstr));
}

function initCookie(areaID) { // points at color element
    if ((userphone=eatCookie("userphone"))) {


        document.getElementById("phone").value = userphone
        
    }

    document.getElementById("phone").focus();
}

function putPhoneInCookie(){
    var date = new Date();
	date.setTime(date.getTime()+(30*24*60*60*1000));

	var phone = document.getElementById("phone").value;
    bakeCookie("userphone", phone, date);
}


var request = false;
try {
  request = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      request = false;
    }
  }
}

function cleanString(str) {
                return str.replace(/[\(\)\.\-\s,]/g, "");
            }
function request_call(){
  if (!request) {
        Alert ("sorry, click to call will not work with your browser");
    } else {
        
		var phone = document.getElementById("phone").value;
        var phone = cleanString(phone);
		
        if (validate_phone(phone)) {
            

			// The web services request minus the domain name

            if(location.href.match(/https/))
	        var url = "https://fixmypcnow.com/proxy.php?phone_to_call=" + escape(phone)+"&click_id=196&referrer=Unknown";
            else
	        var url = "http://fixmypcnow.com/proxy.php?phone_to_call=" + escape(phone)+"&click_id=196&referrer=Unknown";
            request.onreadystatechange = update_message; 
            request.open("GET", url, true);
            request.send(null);
            putPhoneInCookie();

         } else {
            alert ('there is a problem with the phone number, please check it and try again');
         }
     }
}


function update_message(){
    if (request.readyState < 4) {
        document.getElementById('call_result').innerHTML = ' Connecting. . .<br /> <img src="loading.gif" /> ';
    } else if (request.readyState==4 && request.status == 200) {
        document.getElementById('call_result').innerHTML =  request.responseText ;
   }
   
    
}

 function cleanString (str) {
                return str.replace(/[\(\)\.\-\s,]/g, "");
 }

function validate_phone(phone) {


    if (phone.length != 10) {
        alert("Sorry, the phone number you entered does not have 10 digits!  ");
        return 0;
     }
     var strValidChars = "0123456789";

     for (i = 0; i < 10; i++)
      {
       if (strValidChars.indexOf(phone.charAt(i)) == -1)
         {
          alert("Enter just numbers, do not include any punctuation. ");
            return 0;
         }
      }

return 1;
}