var last_scramble = '' 
var last_query = '' 

descr_label = getObjectPA("search_label");
descr_label = $("#description_label").val();
//alert(descr_label);
var transformations = new Array();
transformations[0] = "Change word (token) order"
transformations[1] = "Insert, replace, delete characters"
transformations[2] = "Split and rearrange the text"
transformations[3] = "Try a canned query"

function getObjectPA(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

function randomString(demo) {
    var text1 = new Array();
	var text2 = new Array();
	var text3 = new Array();
	var text4 = new Array();
	text1[0] = "jasoz fitzgerlad";
    text1[1] = "Bilings Fitzgerlad Jasoz";
    text1[2] = "dexterwon, 54124";
    text1[3] = "crazy horse r. fisher";
    text1[4] = "willis horse shoe";
    text1[5] = "tailor dork";
    text1[6] = "Mesquite Pest";
    text1[7] = "k. baker trailer park";
    text1[8] = "G. Smyth 567";
    text1[9] = "Millership";
	text2[0] = "Chateaunuf de Pape";
    text2[1] = "Chateaunuf de p";
    text2[2] = "Chateau Picoron Bordeaux";
    text2[3] = "Xegienec dvYon Mua Brodaeux Supreieur";
    text2[4] = "0070849095987";
    text2[5] = "5987007084909";
    text2[6] = "0-07084/909598#7";
    text2[7] = "Dove soap bar 6ct 610194";
    text2[8] = "FREEZE POPS FRIENDS & GUMBY";
    text2[9] = "smoothie f. kids oranges pnieap.- 180 mls";
    text2[10] = "katkit36ct";
    text2[11] = "0000308";
	text3[0] = "Run-N-Rise 2100 Grenada Blvd";
    text3[1] = "Hit-n-run delivery";
    text3[2] = "kickapig stud";
    text3[3] = "Cookies and crackers weedpatch";
    text3[4] = "Gaiathrim Ramaswimas";
    text3[5] = "wangshuchi, math dept";
    text3[6] = "Ramires Gonzales Allentown PA";
    text3[7] = "Islander's Custom Tackle";
    text3[8] = "Bloody Mary manuf";
    text3[9] = "dovetail wood Raleigh NC";
	text4[0] = "porte pass 66810589";
    text4[1] = "Passport 66810589";
    text4[2] = "Libbeeya";
    text4[3] = "1960 july 20";
    text4[4] = "Vessel IMO 9323833";
    text4[5] = "Tax ID No. 52169x9409";
    
	switch (demo) {
		case 1: {
			var r = Math.floor(Math.random() * text1.length);
			return text1[r];
			break; // unnecessary, but a habit
		}
		case 2: {
			var r = Math.floor(Math.random() * text2.length);
			return text2[r];
			break; // unnecessary, but a habit
		}
		case 3: {
			var r = Math.floor(Math.random() * text3.length);
			return text3[r];
			break; // unnecessary, but a habit
		}
		case 4: {
			var r = Math.floor(Math.random() * text4.length);
			return text4[r];
			break; // unnecessary, but a habit
		}
	}
}

var permute = function(a) {
function randOrd(){
  return (Math.round(Math.random())-0.5);
}
a.sort(randOrd);  
return a;
}
var scramble_counter = 0;

function shuffle(array) {
    var tmp, current, top = array.length;
    if(top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }
   return array;
}

function changeTokenOrder(x) {
    var tokens = new Array();
	tokens = x.split(' ');
	l = tokens.length;
	if (l<=1) {return x;}
	var perm = new Array();
	perm = shuffle(tokens);
	out = perm.join(' ');
	if (out == x) {
		//alert('again'+out);
		out = changeTokenOrder(x); // try again if string was not modified
	}
	return out;
}

function removeInsertRandomCharacter(x) {
    if (x.length <1) { return x;}
	var out = '';
	for (var i=0; i < x.length; i++) {
		var r = Math.random();
		if (r < 0.04) { // insert random char
			out = out + random_char();
			out = out + x[i];
			}
		else if ((r >= 0.04) && (r < 0.08)) {// replace with random char
			out = out + random_char();
			}
		else if ((r >= 0.08) && (r < 0.12)) {// delete char
			i++;
			}
		else {
			out = out + x[i];
		}
	}		
	
	if (out == x) {
		//alert('again'+out);
		out = removeInsertRandomCharacter(x); // try again if string was not modified
	} 
    return out;
}

function randomSplit(x) {
	var out = new Array();
	j = 0;
	out[0]='';
	var thresh = 0.1;
	if (x.length >1) {
		thresh = 0.03
	}
	for (var i=0; i < x.length; i++) {
		var r = Math.random();
		if ( (r < thresh) && (i < x.length-1) ) { 
			j++;
			out[j]='';
			if (r>0.3) {i++;} //almost always keep at least two chars together
			if (r>0.5) {i++;} //often keep at least two chars together
			}
		out[j] = out[j] + x[i];
	}		
    //alert ('before:'+out);
	out = changeTokenOrder(out.join(' '));
	//alert ('after:'+out);
	if (x == out) out = randomSplit(x); // try again if it did not have any effect
	return out;
}

function crs(l) {
	var chars = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXTZ abcdefghiklmnopqrstuvwxyz ,.<>/?;:[]{}!@#$%^&*()_+=";
	var randomstring = '';
	for (var i=0; i<l; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function random_char() {
	var chars = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXTZ abcdefghiklmnopqrstuvwxyz ,.<>/?;:[]{}!@#$%^&*()_+=";
	var rnum = Math.floor(Math.random() * chars.length);
	return chars.substring(rnum,rnum+1);
}

function scramble(x)
{
	var in_text = x.value
	var num_words = 0;
	
	scramble_counter++;
	num_tokens = in_text.split(' ').length;
	num_chars = in_text.length;
	
	if ( (in_text == "Search") || (num_chars < 4) || (scramble_counter % 5)==0 ) {
		x.value = randomString(current_demo);
		$("#search_label").html ("A random query was chosen.");
		// make this the new base query
		last_query = x.value
		}
	else {
		// if you hit "Surprise Me" multiple times, if re-scrambles the original form, not re-scrables the scrambled text
		// unless it was "Search" - the original default query
		if ((  in_text == last_scramble ) && (last_query != "Search") ) {
			in_text = last_query;
		}
	
		if (num_tokens == 1) {
			var choice = Math.floor(Math.random() * 2);
			switch (choice) {
			case 0: {
				x.value = removeInsertRandomCharacter(in_text); //crs(20);
				$("#search_label").html ("Some query characters were randomly changed.");
				break;
				}
			case 1: {
				x.value = randomSplit(in_text);
				$("#search_label").html ("Query was split into multiple tokens.");
				break;
				}
			}	 
		} else { // 2+ tokens
			var choice = Math.floor(Math.random() * 3)
			switch (choice) {
			case 0: {
				x.value = changeTokenOrder(in_text);
				$("#search_label").html ("Query token order was randomly changed.");
				break;
				}
			case 1: {
				x.value = removeInsertRandomCharacter(in_text); //crs(20);
				$("#search_label").html ("Some query characters were randomly changed.");
				break;
			}
			case 2: {
				x.value = randomSplit(in_text);
				$("#search_label").html ("Query was split into multiple tokens.");
				break;
			}
		  }
		}
		last_query = in_text;
	}

	last_scramble = x.value;
	
	gridReload();
	return;
}
