
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("&") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("&")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

function isValidPhoneNumber(num, requiredDigits) {
    var digits = 0;
    if (num == null) return false;
    for( i=0; i<num.length; i++ ){
        var c = num.charCodeAt(i);  
        //convert the i-th character to ascii code value
        if( (c>=48) && (c<=57) ) digits++;
    }    
    return (digits >= requiredDigits);
}

/********
 * Verify that an email addres is valid
 * Written by Paolo Wales (paolo&#64;taize.fr)
********/

function isValidEmail(emailad) {

   var exclude=/[^&#64;\-\.\w]|^[_&#64;\.\-]|[\._\-]{2}|[&#64;\.]{2}|(&#64;)[^&#64;]*\1/;
   var check=/&#64;[\w\-]+\./;
   var checkend=/\.[a-zA-Z]{2,4}$/;
   if(((emailad.search(exclude) != -1) ||
       (emailad.search(check)) == -1) ||
       (emailad.search(checkend) == -1)){
      return false;
   } else {
      return true;
   }
}

/********
 * Ensures valid emails are present and that the sender email doesn't contain
 * an 'doctor-hill.com' in the sender's domain.
 * 
 * use: for "email a friend" security
 * created by: Scott Yancey
********/

function isValidEmails() {
   
   var recipientEmail = document.forms[0].recipientEmail.value;
   var senderEmail = document.forms[0].senderEmail.value;
   var error = false;
   
   var errorMessage = "The following errors are present:\n\n";
   
   if (recipientEmail.indexOf(',') < 0) {
   		if (!isValidEmail(recipientEmail)){
   			error = true;
			errorMessage += "The recipient's Email address is not valid\n"
   	  }	
   } else {
   
   	 var addresses = new Array();
	 addresses = recipientEmail.split(',');
	 var currentAddress;
	 var i;
	 for (var i=0; i < addresses.length; i++) {      
	
		if (!isValidEmail(trim(addresses[i]))){
   			error = true;
   	    }
		
	 }
	 if (i > 10) {
			error = true;
			errorMessage += "Only 10 addresses are allowed\n"	
	 } 
	 if (error) {
	 	errorMessage += "A recipient's Email address is not valid\n"	
	 } 
	 
   }

   if (!isValidEmail(senderEmail)) {
   	error = true;
	errorMessage += "The sender's Email address is not valid"
   }
   if (senderEmail.indexOf("doctor-hill.com") >= 0) {
   	error = true;
	errorMessage += "The sender's Email address can not contain 'doctor-hill.com'."
   }
   
   if (error) {
      alert(errorMessage);
      return false;
   } else {
      return true;
   }
}

// this is for the interactive screenshot

function launchDemo(url){
	var winwidth=screen.width;
	var winheight=screen.height;
	var scroll="";

	if(winwidth>1000){
		winwidth=1020;
		winheight=700;
		scroll="no";
	}else if(winwidth<1000){
		winwidth=790;
		winheight=530;
		scroll="yes";
	}
	url=url+"&scroll="+scroll;
	newWindow=window.open(url,"Demo","toolbar=no,scrollbars=no,resizable=no,width="+winwidth+",height="+winheight+",left=0,top=0");
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function getArgs(arg_name, str) {
 var value = "", tmpstr = "";
 if (!str) str = location.search.substring(1);
 if (!str) return value;
 else {
  var tmparray = str.split("&");
  for (i=0; i<tmparray.length; i++) {
   tmpstr = tmparray[i];
   if (tmpstr.indexOf(arg_name + "=") != -1) {
    var tmp2array = tmparray[i].split("=");
    value = tmp2array[1];
   }
  }
 }
 return value;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
