//Copyright by Avadh Patel
//Created for SoftUse


var base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split("");

function base64_encode (s)
 {
   var r = ""; var p = ""; var c = s.length % 3;
 
   if (c > 0) { for (; c < 3; c++) { p += '='; s += "\0"; } }
 
   for (c = 0; c < s.length; c += 3) {
 
     if (c > 0 && (c / 3 * 4) % 76 == 0) { r += "\r\n"; }
 
     var n = (s.charCodeAt(c) << 16) + (s.charCodeAt(c+1) << 8) + s.charCodeAt(c+2);
 
      n = [(n >>> 18) & 63, (n >>> 12) & 63, (n >>> 6) & 63, n & 63];
 
      r += base64chars[n[0]] + base64chars[n[1]] + base64chars[n[2]] + base64chars[n[3]];
   }
 
   return r.substring(0, r.length - p.length) + p;
 }

var globalFG = "#FFFFFF";
var globalBG = "#000000";

var num_templates = 12;
function create_templates() {
	templates = new Array(num_templates);
	
	templates[0] = new Array(2);
	templates[0][0] = "#000000";
	templates[0][1] = "#FFFFFF";
	
	templates[1] = new Array(2);
	templates[1][0] = "#FFFFFF";
	templates[1][1] = "#000000";
	
	templates[2] = new Array(2);
	templates[2][0] = "#FFFFFF";
	templates[2][1] = "#940808";
	
	templates[3] = new Array(2);
	templates[3][0] = "#940808";
	templates[3][1] = "#FFFFFF";

	templates[4] = new Array(2);
	templates[4][0] = "#FFFFFF";
	templates[4][1] = "#037D00";
	
	templates[5] = new Array(2);
	templates[5][0] = "#037D00";
	templates[5][1] = "#FFFFFF";

	templates[6] = new Array(2);
	templates[6][0] = "#FFFFFF";
	templates[6][1] = "#08008F";
	
	templates[7] = new Array(2);
	templates[7][0] = "#08008F";
	templates[7][1] = "#FFFFFF";

	templates[8] = new Array(2);
	templates[8][0] = "#000000";
	templates[8][1] = "#B7EDFF";
	
	templates[9] = new Array(2);
	templates[9][0] = "#B7EDFF";
	templates[9][1] = "#000000";

	templates[10] = new Array(2);
	templates[10][0] = "#000000";
	templates[10][1] = "#FFC0CB";
	
	templates[11] = new Array(2);
	templates[11][0] = "#FFC0CB";
	templates[11][1] = "#000000";

}

function create_template_boxes() {
	var adv = document.getElementById("advanceArea");
	var tempID=0;
	var x=0;
	var y=0;
	var numRows = num_templates / 4 + num_templates % 4;
	for(y=0; y<numRows; y++) {
		var row = adv.insertRow(y);
	for(x=0; x<4; x++) {
		var cell = row.insertCell(x);
		var templBox = document.createElement("a");
		//templBox.setAttribute("color",templates[x][0]);
		//templBox.setAttribute("background",templates[x][1]);
		templBox.style.color=templates[tempID][0];
		templBox.style.background=templates[tempID][1];
		templBox.style.border="2px solid black";
		templBox.style.borderColor = "black";
		templBox.style.paddingLeft = "10px";
		templBox.style.paddingRight = "10px";
		templBox.style.paddingTop = "1px";
		templBox.style.paddingBottom = "1px";
		templBox.style.marginLeft = "15px";
		templBox.style.marginTop = "10px";
		templBox.style.marginBottom = "10px";
		templBox.setAttribute("id",tempID);
		templBox.setAttribute("onClick","template_click(this)");
		templBox.setAttribute("onTap","template_click(this)");
		templBox.href="#";
		templBox.innerHTML = "a";
		//templBox.appendChild(document.createTextNode("a"));
		//adv.appendChild(templBox);
		cell.appendChild(templBox);
		tempID++;
	}
	}
}

var last_template_id = -1;

function template_click(element) {
	//First clear the last template's green border
	if(last_template_id != -1) {
	  var y = document.getElementById(last_template_id);
	  y.style.borderColor = "black";
	}
	var x = element.getAttribute("id");
	if(x < 0) {
		return;
	}
	element.style.borderColor = "#00FF21";
	globalFG = templates[x][0];
	globalBG = templates[x][1];
	last_template_id = x;
}

function advance() {
	//clear any child nodes
	var adv = document.getElementById("advanceArea");
	if(adv){
		while(adv.firstChild) {
			adv.removeChild(adv.firstChild);
		}
	}
	//create templates
	create_templates();
	//now show templates
	create_template_boxes();
}

function create_css(fg, bg) {
	var css_str = "<style type=\"text/css\">";
	css_str = css_str + "body { color:" + fg + ";";
	css_str = css_str + "background:" + bg + "; text-align:center; font-family:arial; } ";
	css_str = css_str + "body > name { font-size:60px; } ";
	css_str = css_str + "body > a { text-decoration:none; color:inherit; } ";
	css_str = css_str + "</style>"
	return css_str;
}

var iconUrl = null;

function get_icon_url() {
	var url = String(window.location);
	var index = url.indexOf("?");
	if(index == -1) {
		iconUrl = null;
		return null;
	}
	var data = url.substr(index+1);

	var splitted = data.split("=");
	if(splitted[0] == "icon") {
		iconUrl = splitted[1];
		return iconUrl;
	}
	return null;
}

function create_string(n,s,iu)
{
  var inputStr = "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=10.0, user-scalable=1\"\/>";
    
  if(iu != null){
  	inputStr = inputStr + "<link rel=\"apple-touch-icon\" href=\"" + iu + "\" />";
  }
  inputStr = inputStr + create_css(globalFG, globalBG);
  inputStr = inputStr + "</head><body><name>";
  inputStr = inputStr + n;
  inputStr = inputStr +"</name><br><a href=\"http://soft-use.com\">SoftUse</a><script type=\"text/javascript\">location.href=\"tel://" + s + "\";<\/script><\/body><\/html>";
  
  var encodedStr = base64_encode(inputStr);
  //return inputStr;
  return encodedStr;
}

function zap(field) {
    if (! field.zapped ) {
        field.zapped = true;
        field.value  = "";
    }
    if (field.value) {
    	field.value = "";
    }
}
var output = null;

function clear_output() {
  if(output) {
    while(output.firstChild) {
      output.removeChild(output.firstChild);
    }
  }
}

function write_output(s) {
  if (output) {
      output.appendChild(document.createElement("br"));
   } else {
      output = document.getElementById("outputArea");
   }
  clear_output();
  //var output = document.getElementById("outputArea");
  output.appendChild(document.createTextNode(s));
}

function write_output_link(s) {
  if (output) {
      output.appendChild(document.createElement("br"));
   } else {
      output = document.getElementById("outputArea");
   }
  clear_output();
  var link = "data:text/html;base64," + s;
  var a_element = document.createElement("a");
  a_element.href = link;
  a_element.innerHTML = "Tap for Shortcut";
  output.appendChild(a_element);
  
  }  


function generate() {
  //var contact_name = eval(inForm.co_name.value);
  var contact_name = "";
  contact_name = document.getElementById("co_name").value;
  var phone_no = -1;
  //write_output(document.getElementById("co_num").value);
  //Well some phone numbers have leading '+' and '0' so we need to keep that..  
  phone_no = document.getElementById("co_num").value;
  var icon_url = document.getElementById("icon_url").value;
  if(icon_url == "Enter icon url") {
  	icon_url = null;
  }
  if(phone_no == -1) {
    write_output("Please Enter the Phone No");
    return;
  }else {
    var i= phone_no.indexOf("+");
	 if(i != -1) {
	 	phone_no = "%2b"+ phone_no.substring(i+1,phone_no.length);
	 }
    var encodedStr = create_string(contact_name, phone_no, icon_url);
    write_output_link(encodedStr);
   }
}

function loaded() {
	window.scrollTo(0,1);
}

window.addEventListener("load", function() { setTimeout(loaded, 100) }, false);
