function toggleEditable(div_name) {
  if(document.getElementById(div_name).style.display == 'none') { 
  	document.getElementById(div_name).style.display = 'block'; 
  } else { 
  	document.getElementById(div_name).style.display = 'none';
  }
}

function toggleEditable(div_name1, div_name2) {
  if(document.getElementById(div_name1).style.display == 'none') { 
  	document.getElementById(div_name1).style.display = 'block'; 
  } else { 
  	document.getElementById(div_name1).style.display = 'none';
  }
  if(document.getElementById(div_name2).style.display == 'none') { 
  	document.getElementById(div_name2).style.display = 'block'; 
  } else { 
  	document.getElementById(div_name2).style.display = 'none';
  }
}

function changeStatusImg(img_id, statusString) {
	var s = statusString;
	var imgUrl = '';
	switch (s) {
	case "Active":
		imgUrl = '/marketplace/images/icn_active.png';
		break;
	case "Offline":
		imgUrl = '/marketplace/images/icn_inactive.png';
		break;
	case "Pending WebJuice", "Pending Advertiser":
		imgUrl = '/marketplace/images/icn_approval.png';
		break;
	case "Reject":
		imgUrl = '/marketplace/images/icn_rejected.png';
		break;
	case "Delete":
		imgUrl = '/marketplace/images/icn_deleted.png';
		break;
	default: imgUrl = ''; break;
	}
	if(imgUrl != '') {
		document.getElementById(img_id).src = imgUrl;
	} else {
		document.getElementById(img_id).src = '';
	}
}

function addToVerify(var_id) {
	if(document.getElementById(var_id).value == 'false') {
		document.getElementById(var_id).value = 'true';
	}
}

function changeClickThrough(url) {
	var startIndex = url.indexOf("{");
	var endIndex = url.indexOf("}");
	if (startIndex != -1 && endIndex != -1 && startIndex < endIndex) {
		var destTag = url.substring(startIndex + 1, endIndex);
		alert(destTag);
	}
}

function verifyValue(myform) { // verify bid, dailyBudget, campaignBudget values
	if (!form_submit_flag) { // avoid submit from when pressing enter
		form_submit_flag = true;
		return false;
	}
	var bid = dailyBudget = overallBudget = -1;
	var bid_temp = db_temp = overallBudget = -1;
	var name;
	var inputList = myform.getElementsByTagName('input');
	var verifyFlag = false;

	for (i=0; i < inputList.length; i++) {
		if(inputList[i].name == 'activeRow' && inputList[i].value == 'true') {
			//alert("validate at " + inputList[i].id);
			bid = overallBudget = dailyBudget = -1;
			verifyFlag = true;
		}
		if(verifyFlag == true) {
			if (inputList[i].name.indexOf("newCampaign") != -1 
				&& inputList[i].name.indexOf(".name") != -1) {
				name = inputList[i].value;
			} else if (inputList[i].name.indexOf("newCampaign") != -1 
				&& inputList[i].name.indexOf("bid") != -1) {
				bid = inputList[i].value.replace(/^\s*|\s*$/g,'');
				if (isNaN(bid) || bid == '') {
					alert(name + "\'s Bid " + bid + " must be a valid number.");
					return false;
				}
			} else if (inputList[i].name.indexOf("newCampaign") != -1 
				&& inputList[i].name.indexOf("dailyBudget") != -1) {
				dailyBudget = inputList[i].value.replace(/^\s*|\s*$/g,'');
				if (isNaN(dailyBudget) || dailyBudget == '') {
					alert(name + "\'s Daily Budget " + dailyBudget + " must be a valid number.");
					return false;
				}
			} else if (inputList[i].name.indexOf("newCampaign") != -1 
				&& inputList[i].name.indexOf("overallBudget") != -1) {
				overallBudget = inputList[i].value.replace(/^\s*|\s*$/g,'');
				if (isNaN(overallBudget) || overallBudget == '') {
					alert(name + "\'s Overall Budget " + overallBudget + " must be a valid number.");
					return false;
				}
			}
			if (bid != -1 && dailyBudget != -1 && overallBudget != -1) {
				bid_temp = parseFloat(bid);
				db_temp = parseFloat(dailyBudget);
				ob_temp = parseFloat(overallBudget);
				
				if (db_temp < bid_temp) {
					alert(name + "\'s bid $" + bid_temp + " must be less than or equal to its daily budget $" + db_temp + ".");
					return false;
				}
				if (ob_temp < db_temp) {
					alert(name + "\'s daily budget $" + db_temp + " must be less than or equal to its overall budget $" + ob_temp + ".");
					return false;
				}
				bid = overallBudget = dailyBudget = -1;
				verifyFlag = false;
			} 
		}
	}
	return true;
}



// geo-targeting related js
var total_advertisement_row = 0;
var total_advertisement_zip_set_row = 0;

function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function isIE() {
 	appName = window.navigator.appName;
	var b;
	if(appName=="Netscape")
		return false;
	else 
		return true; //IE
 }

function rzCC(s){
   for(var exp=/-([a-z])/; 
       exp.test(s); 
       s=s.replace(exp,RegExp.$1.toUpperCase()));
   return s;
}

function _setStyle(element, declaration) {
   if (declaration.charAt(declaration.length-1)==';')
     declaration = declaration.slice(0, -1);
   var k, v;
   var splitted = declaration.split(';');
   for (var i=0, len=splitted.length; i<len; i++) {
      k = rzCC(splitted[i].split(':')[0]);
      v = splitted[i].split(':')[1];
      eval("element.style."+k+"='"+v+"'");

   }
}

function _setClass(element,className) {
	if(!isIE()) {
    	element.setAttribute('class',className);  
    } else {
    	element.setAttribute('className',className);  
    }
}
 
function addCampaignZipSet(isAdmin) {
	total_advertisement_zip_set_row++;
	
    var tbody = document.getElementById("advertisementZipSetList").getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR");
    _setClass(row,'data_row');
    //row.onmouseover="this.style.background='#FEF5CC';";
    //row.onmouseout="this.style.background='#FFFFFF'";
							
    var td1 = document.createElement("TD");
    _setClass(td1,'data');
    var input1 = document.createElement("INPUT");
    input1.setAttribute('name', 'checkedZipSets');
    input1.setAttribute('id', 'checkedZipSets');
    input1.setAttribute('type', 'checkbox');
    var input7 = document.createElement("INPUT");
    input7.setAttribute('name', 'advertisementZipSetId');
    input7.setAttribute('id', 'advertisementZipSetId');
    input7.setAttribute('type', 'hidden');
    input7.setAttribute('value', '-1'); // new advertisement id = -1
    var input9 = document.createElement("INPUT");
    input9.setAttribute('name', 'advertisementZipSetRanking');
    input9.setAttribute('id', 'advertisementZipSetRanking');
    input9.setAttribute('type', 'hidden');
    input9.setAttribute('value', 0); 
    var input10 = document.createElement("INPUT");
    input10.setAttribute('name', 'advertisementZipListText');
    input10.setAttribute('id', 'advertisementZipListText');
    input10.setAttribute('type', 'hidden');
    input10.setAttribute('value', ''); 

    td1.appendChild(input1);
    td1.appendChild(input7);
    td1.appendChild(input9);
    td1.appendChild(input10);
    var td2 = document.createElement("TD");
    _setClass(td2,'data');
    var input11 = document.createElement("INPUT");
    input11.setAttribute('name', 'advertisementZipList');
    input11.setAttribute('type', 'text');
    input11.setAttribute('value', '');
    input11.setAttribute('maxlength', '1000');
    _setStyle(input11,'width:300px;');
    td2.appendChild (input11);

	// create cpa goal field only for admin
    if (isAdmin){ 
	    var td6 = document.createElement("TD");
	    _setClass(td6,'data');
	    var input4 = document.createElement("INPUT");
	    input4.setAttribute('name', 'advertisementZipSetCpaGoal');
	    input4.setAttribute('type', 'text');
	    input4.setAttribute('value', '');
	    _setStyle(input4,'width:60px;');
	    td6.appendChild (input4);
    }
    var td3 = document.createElement("TD");
    _setClass(td3,'data');
    td3.appendChild (document.createTextNode("--"));
    var td4 = document.createElement("TD");
    _setClass(td4,'data');
    var input2;
    if (isIE()) {
    	input2 = document.createElement("<INPUT name='advertisementZipSetBid'>");
    } else { 
    	input2 = document.createElement("INPUT");
    	input2.setAttribute('name', 'advertisementZipSetBid');
    }
    input2.setAttribute('value', 0);
    _setStyle(input2,'width:60px;');
    td4.appendChild (input2);
    var td5 = document.createElement("TD");
    _setClass(td5,'data');
    var input3 = document.createElement("INPUT");
    input3.setAttribute('name', 'advertisementZipSetClickThrough');
    input3.setAttribute('type', 'text');
    input3.setAttribute('value', '');
    input3.setAttribute('id','zipset_url_'+total_advertisement_zip_set_row);
    _setStyle(input3,'width:265px;');
    td5.appendChild (input3);
    var td7 = document.createElement("TD");
    _setClass(td7,'data');
    var input5 = document.createElement("a");
    input5.setAttribute('href', 'javascript:testAdvertisementZipSetUrl('+total_advertisement_zip_set_row+')');
    var img_test = document.createElement("img");
    img_test.src = "/marketplace/images/btn_test.png";
    _setStyle(img_test,'vertical-align:bottom;border:0px;');
    input5.appendChild(img_test);
    td7.appendChild (input5);
    
    row.appendChild(td1);
    row.appendChild(td2);
	// create cpa goal field only for admin
    if (isAdmin){ 
    	row.appendChild(td6);
    }
    row.appendChild(td3);
    row.appendChild(td4);
    row.appendChild(td5);
    row.appendChild(td7);
	
    tbody.appendChild(row);

}

function addCampaign() {
	total_advertisement_row++;
	
	try {
		var state = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].value;
		var defaultBid = min_bid_list[state];
		var defaultClickThrough = document.forms[0].web.options[document.forms[0].web.selectedIndex].value+document.forms[0].clickThrough.value;//(typeof(document.forms[0].advertisementClickThrough.value) == "undefined") ? document.forms[0].advertisementClickThrough[0].value : document.forms[0].advertisementClickThrough.value;
		var defaultCpa;
		if (cpa_list[state] == 0.0) {
		//	defaultCpa = (typeof(document.forms[0].advertisementCpaGoal.value) == "undefined") ? document.forms[0].advertisementCpaGoal[0].value : document.forms[0].advertisementCpaGoal.value;
			defaultCpa = "";
		} else {
			defaultCpa = cpa_list[state];
		}
		var stateText = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].text;
		// block adding dup state
		if (typeof(document.forms[0].advertisementState) != "undefined") {
			if (typeof(document.forms[0].advertisementState.value) == "undefined") {
				for (i=0;i<document.forms[0].advertisementState.length;i++) {
					if (state == document.forms[0].advertisementState[i].value) {
						alert("Selected state exists already");
						return;
					}
				}
			} else {
				if (state == document.forms[0].advertisementState.value) {
					alert("Selected State exists already");
					return;
				}
			}
		}
	} catch (e) {
	alert(e);
		alert("Please select a state first");
		return;
	}
    var tbody = document.getElementById("advertisementList").getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR");
    _setClass(row,'data_row');
    //row.onmouseover="this.style.background='#FEF5CC';";
    //row.onmouseout="this.style.background='#FFFFFF'";
							
    var td1 = document.createElement("TD");
    _setClass(td1,'data');
    var input1 = document.createElement("INPUT");
    input1.setAttribute('name', 'checkedStates');
    input1.setAttribute('id', 'checkedStates');
    input1.setAttribute('type', 'checkbox');
    var input7 = document.createElement("INPUT");
    input7.setAttribute('name', 'advertisementId');
    input7.setAttribute('id', 'advertisementId');
    input7.setAttribute('type', 'hidden');
    input7.setAttribute('value', '-1'); // new advertisement id = -1
    var input8 = document.createElement("INPUT");
    input8.setAttribute('name', 'advertisementState');
    input8.setAttribute('id', 'advertisementState');
    input8.setAttribute('type', 'hidden');
    input8.setAttribute('value', state); 
    var input9 = document.createElement("INPUT");
    input9.setAttribute('name', 'advertisementRanking');
    input9.setAttribute('id', 'advertisementRanking');
    input9.setAttribute('type', 'hidden');
    input9.setAttribute('value', 0); 
    var input10 = document.createElement("INPUT");
    input10.setAttribute('name', 'advertisementStateText');
    input10.setAttribute('id', 'advertisementStateText');
    input10.setAttribute('type', 'hidden');
    input10.setAttribute('value', stateText); 

    td1.appendChild(input1);
    td1.appendChild(input7);
    td1.appendChild(input8);
    td1.appendChild(input9);
    td1.appendChild(input10);
    var td2 = document.createElement("TD");
    _setClass(td2,'data');
    if (state =='N/A') {
    	td2.appendChild (document.createTextNode('Default'));
    } else {
    	td2.appendChild (document.createTextNode(stateText));
    }
    var td6 = document.createElement("TD");
    _setClass(td6,'data');
    var input4 = document.createElement("INPUT");
    input4.setAttribute('name', 'advertisementCpaGoal');
    input4.setAttribute('type', 'text');
    input4.setAttribute('value', defaultCpa);
    _setStyle(input4,'width:60px;');
    td6.appendChild (input4);
    var td3 = document.createElement("TD");
    _setClass(td3,'data');
    td3.appendChild (document.createTextNode("--"));
    var td4 = document.createElement("TD");
    _setClass(td4,'data');
    var input2;
    if (isIE()) {
    	input2 = document.createElement("<INPUT name='advertisementBid'>");
    } else { 
    	input2 = document.createElement("INPUT");
    	input2.setAttribute('name', 'advertisementBid');
    }
    input2.setAttribute('value', defaultBid);
    _setStyle(input2,'width:60px;');
    td4.appendChild (input2);
    var td5 = document.createElement("TD");
    _setClass(td5,'data');
    var input3 = document.createElement("INPUT");
    input3.setAttribute('name', 'advertisementClickThrough');
    input3.setAttribute('type', 'text');
    input3.setAttribute('value', defaultClickThrough);
    input3.setAttribute('id','url_'+total_advertisement_row);
    _setStyle(input3,'width:265px;');
    td5.appendChild (input3);
    var td7 = document.createElement("TD");
    _setClass(td7,'data');
    var input5 = document.createElement("a");
    input5.setAttribute('href', 'javascript:testAdvertisementUrl('+total_advertisement_row+')');
    var img_test = document.createElement("img");
    img_test.src = "/marketplace/images/btn_test.png";
    _setStyle(img_test,'vertical-align:bottom;border:0px;');
    input5.appendChild(img_test);
    td7.appendChild (input5);
    
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td6);
    row.appendChild(td3);
    row.appendChild(td4);
    row.appendChild(td5);
    row.appendChild(td7);
	
    tbody.appendChild(row);

}

function addCampaignForNewCampaign() {
	total_advertisement_row++;
	try {
	   	var state = '';
	   	var stateText = '';
		var defaultClickThrough = document.forms[0].web.value + document.forms[0].clickThrough.value;
		if(document.forms[0].selectedState.options.selectedIndex < 0) { 
		    alert('No State Selected');
			return;
		}
		state = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].value;
		stateText = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].text;
		var defaultBid = min_bid_list[state];
		//var state = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].value;
		//var defaultBid = (typeof(document.forms[0].advertisementBid.value) == "undefined") ? document.forms[0].advertisementBid[0].value : document.forms[0].advertisementBid.value;
		//var defaultClickThrough = (typeof(document.forms[0].advertisementClickThrough.value) == "undefined") ? document.forms[0].advertisementClickThrough[0].value : document.forms[0].advertisementClickThrough.value;
		
		// block adding dup state
		if (typeof(document.forms[0].advertisementState) != "undefined") {
			if (typeof(document.forms[0].advertisementState.value) == "undefined") {
				for (i=0;i<document.forms[0].advertisementState.length;i++) {
					if (state == document.forms[0].advertisementState[i].value) {
						alert("Selected state existed already");
						return;
					}
				}
			} else {
				if (state == document.forms[0].advertisementState.value) {
					alert("Selected State existed already");
					return;
				}
			}
		}
	} catch (e) {
		alert("Please select a state first");
		return;
	}
    var tbody = document.getElementById("advertisementList").getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR");
    _setClass(row,'data_row');
    //row.onmouseover="this.style.background='#FEF5CC';";
    //row.onmouseout="this.style.background='#FFFFFF'";
							
    var td1 = document.createElement("TD");
    _setClass(td1,'data');
    var input1 = document.createElement("INPUT");
    input1.setAttribute('name', 'checkedStates');
    input1.setAttribute('id', 'checkedStates');
    input1.setAttribute('type', 'checkbox');
    var input7 = document.createElement("INPUT");
    input7.setAttribute('name', 'advertisementId');
    input7.setAttribute('id', 'advertisementId');
    input7.setAttribute('type', 'hidden');
    input7.setAttribute('value', '-1'); // new advertisement id = -1
    var input8 = document.createElement("INPUT");
    input8.setAttribute('name', 'advertisementState');
    input8.setAttribute('id', 'advertisementState');
    input8.setAttribute('type', 'hidden');
    input8.setAttribute('value', state); 
    var input81 = document.createElement("INPUT");
    input81.setAttribute('name', 'advertisementStateText');
    input81.setAttribute('id', 'advertisementStateText');
    input81.setAttribute('type', 'hidden');
    input81.setAttribute('value', stateText); 
    
    
    td1.appendChild(input1);
    td1.appendChild(input7);
    td1.appendChild(input8);
    td1.appendChild(input81);
    var td2 = document.createElement("TD");
    _setClass(td2,'data');
    //td2.setAttribute('colspan', '2');
    if (state =='N/A') {
    	td2.appendChild (document.createTextNode('Default'));
    } else {
    	td2.appendChild (document.createTextNode(stateText));
    }
    
    var td4 = document.createElement("TD");
    _setClass(td4,'data');
    var input2;
    if (isIE()) {
    	input2 = document.createElement("<INPUT name='advertisementBid'>");
    } else { 
    	input2 = document.createElement("INPUT");
    	input2.setAttribute('name', 'advertisementBid');
    }
    input2.setAttribute('type', 'text');
    input2.setAttribute('value', defaultBid);
    _setStyle(input2,'width:60px;');
    td4.appendChild (input2);
    var td5 = document.createElement("TD");
    _setClass(td5,'data');
    var input3 = document.createElement("INPUT");
    input3.setAttribute('name', 'advertisementClickThrough');
    input3.setAttribute('type', 'text');
    input3.setAttribute('value', defaultClickThrough);
    input3.setAttribute('id','url_'+total_advertisement_row);
    _setStyle(input3,'width:265px;');
    td5.appendChild (input3);
    var td7 = document.createElement("TD");
    _setClass(td7,'data');
    var input5 = document.createElement("a");
    input5.setAttribute('href', 'javascript:testAdvertisementUrl('+total_advertisement_row+')');
    var img_test = document.createElement("img");
    img_test.src = "/marketplace/images/btn_test.png";
    _setStyle(img_test,'vertical-align:bottom;border:0px;');
    input5.appendChild(img_test);
    td7.appendChild (input5);
    
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td4);
    row.appendChild(td5);
    row.appendChild(td7);
	
    tbody.appendChild(row);
    
}

function addCampaignAdvertiser() {
	total_advertisement_row++;
	try {
	   	var state = '';
	   	var stateText = '';
		
		var defaultClickThrough = document.forms[0].web.value + document.forms[0].clickThrough.value;
		if(document.forms[0].selectedState.options.selectedIndex < 0) { 
		    alert('No State Selected');
			return;
		}
		state = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].value;
		stateText = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].text;
		var defaultBid = min_bid_list[state];
		//var state = document.forms[0].selectedState.options[document.forms[0].selectedState.options.selectedIndex].value;
		//var defaultBid = (typeof(document.forms[0].advertisementBid.value) == "undefined") ? document.forms[0].advertisementBid[0].value : document.forms[0].advertisementBid.value;
		//var defaultClickThrough = (typeof(document.forms[0].advertisementClickThrough.value) == "undefined") ? document.forms[0].advertisementClickThrough[0].value : document.forms[0].advertisementClickThrough.value;
		
		// block adding dup state
		if (typeof(document.forms[0].advertisementState) != "undefined") { 
			if (typeof(document.forms[0].advertisementState.value) == "undefined") {
				for (i=0;i<document.forms[0].advertisementState.length;i++) {
					if (state == document.forms[0].advertisementState[i].value) {
						alert("Selected state existed already");
						return;
					}
				}
			} else {
				if (state == document.forms[0].advertisementState.value) {
					alert("Selected State existed already");
					return;
				}
			}
		}
	} catch (e) {
		alert("Please select a state first");
		return;
	}
    var tbody = document.getElementById("advertisementList").getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR");
    _setClass(row,'data_row');
    //row.onmouseover="this.style.background='#FEF5CC';";
    //row.onmouseout="this.style.background='#FFFFFF'";
							
    var td1 = document.createElement("TD");
    _setClass(td1,'data');
    var input1 = document.createElement("INPUT");
    input1.setAttribute('name', 'checkedStates');
    input1.setAttribute('id', 'checkedStates');
    input1.setAttribute('type', 'checkbox');
    var input7 = document.createElement("INPUT");
    input7.setAttribute('name', 'advertisementId');
    input7.setAttribute('id', 'advertisementId');
    input7.setAttribute('type', 'hidden');
    input7.setAttribute('value', '-1'); // new advertisement id = -1
    var input8 = document.createElement("INPUT");
    input8.setAttribute('name', 'advertisementState');
    input8.setAttribute('id', 'advertisementState');
    input8.setAttribute('type', 'hidden');
    input8.setAttribute('value', state); 
    var input9 = document.createElement("INPUT");
    input9.setAttribute('name', 'advertisementRanking');
    input9.setAttribute('id', 'advertisementRanking');
    input9.setAttribute('type', 'hidden');
    input9.setAttribute('value', 0); 
    var input10 = document.createElement("INPUT");
    input10.setAttribute('name', 'advertisementStateText');
    input10.setAttribute('id', 'advertisementStateText');
    input10.setAttribute('type', 'hidden');
    input10.setAttribute('value', stateText); 
    td1.appendChild(input1);
    td1.appendChild(input7);
    td1.appendChild(input8);
    td1.appendChild(input9);
    td1.appendChild(input10);
    var td2 = document.createElement("TD");
    _setClass(td2,'data');
    if (state =='N/A') {
    	td2.appendChild (document.createTextNode('Default'));
    } else {
    	td2.appendChild (document.createTextNode(stateText));
    }
    var td3 = document.createElement("TD");
    _setClass(td3,'data');
    td3.appendChild (document.createTextNode("--"));
    var td4 = document.createElement("TD");
    _setClass(td4,'data');
    var input2;
    if (isIE()) {
    	input2 = document.createElement("<INPUT name='advertisementBid'>");
    } else { 
    	input2 = document.createElement("INPUT");
    	input2.setAttribute('name', 'advertisementBid');
    }
    input2.setAttribute('type', 'text');
    input2.setAttribute('value', defaultBid);
    _setStyle(input2,'width:60px;');
    td4.appendChild (input2);
    var td5 = document.createElement("TD");
    _setClass(td5,'data');
    _setStyle(td5,'white-space:nowrap;')
    var input3 = document.createElement("INPUT");
    input3.setAttribute('name', 'advertisementClickThrough');
    input3.setAttribute('type', 'text');
    input3.setAttribute('value', defaultClickThrough);
    input3.setAttribute('id','url_'+total_advertisement_row);
    _setStyle(input3,'width:265px;');
    td5.appendChild (input3);
    var td7 = document.createElement("TD");
    _setClass(td7,'data');
    var input5 = document.createElement("a");
    input5.setAttribute('href', 'javascript:testAdvertisementUrl('+total_advertisement_row+')');
    var img_test = document.createElement("img");
    img_test.src = "/marketplace/images/btn_test.png";
    _setStyle(img_test,'border:0px none;vertical-align: bottom;margin-left:6');
    input5.appendChild(img_test);
    td7.appendChild (input5);
    
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
    row.appendChild(td4);
    row.appendChild(td5);
    row.appendChild(td7);
    tbody.appendChild(row);
    
}

function deleteCampaignZipSet() {
	var c_value = "";
	var mytable=document.getElementById('advertisementZipSetList');
	var flag = false;
	for (var i=1; i < document.forms[0].checkedZipSets.length; i++)
    {	
    	//alert(document.forms[0].checkedZipSets.length);
    	//alert(getInnerText(mytable.rows[i].cells[1]));
   		if (document.forms[0].checkedZipSets[i].checked)
      	{
/*
      		if (document.forms[0].checkedZipSets.length <=2) {
    			alert("At least one Zip Set is required");
    			break;
    		}
     		if (document.forms[0].profile.value == "0" && getInnerText(mytable.rows[i].cells[1]) == "Default") {//document.forms[0].advertisementState[i-1].value == "N/A") {
     			alert("Default advertisement cannot be removed from this campaign.");
    			return;
     		}
*/     		
      		mytable.deleteRow(i);
      		i--;
      	}
   	}
}

function deleteCampaign() {
	var c_value = "";
	var mytable=document.getElementById('advertisementList');
	var flag = false;
	for (var i=1; i < document.forms[0].checkedStates.length; i++)
    {	
    	//alert(document.forms[0].checkedStates.length);
    	//alert(getInnerText(mytable.rows[i].cells[1]));
   		if (document.forms[0].checkedStates[i].checked)
      	{
      		if (document.forms[0].checkedStates.length <=2) {
    			alert("At least one campaign is required");
    			break;
    		}

     		if (document.forms[0].profile.value == "0" && getInnerText(mytable.rows[i].cells[1]) == "Default") {//document.forms[0].advertisementState[i-1].value == "N/A") {
     			alert("Default advertisement cannot be removed from this campaign.");
    			return;
     		}
      		mytable.deleteRow(i);
      		i--;
      	}
   	}
}
function getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

// profile conversion message box
var messagePanel;
function showMsgForProfileConv() {
	if (profile_conversion_flag==1) {
		msg = "Please..<br>(1) Add at least 1 State and associated Max Bid to the bid table under the Target Your Bids By State section, and <br>(2) Hit the Save Changes button to confirm the status of this campaign. <a href=\"javascript:popup_window('faq');\">FAQs</a> are available. ";
	} else if (profile_conversion_flag==2) {
		msg = "Please..<br>(1) Enter a Max Bid under the Modify Your Bid section, and <br>(2) Hit the Save Changes button to comfirm the status of this campaign.";
	} else if (profile_conversion_flag==3) {
		msg = "Please..<br>(1) Set a Max Bid for the Default State/Region under the Target Your Bids By State section, and <br>(2) Hit the Save Changes button to confirm the status of this campaign.";
	} else if (profile_conversion_flag==4) {
		msg = "Please..<br>(1) Review the State(s) in the bid table under the Target Your Bids By State section, and <br>(2) Hit the Save Changes button to comfirm the status of this campaign.";
	} else {
		return;
	}
	messagePanel = new wjMessagePanel("Action Required",msg);
	messagePanel.show();
}



function containChar(value) {
	var r = value - 0;
	if (r==value)
		return false;
	else 
		return true;
}

function saveAllBids() {
	var bid = document.forms[0].appliedBid.value;

	if (isEmpty(bid) || containChar(bid)) {
		alert("Please enter valid bid price!");
		return;
	}
	var o = document.getElementsByName("advertisementBid");

	if (typeof(document.forms[0].advertisementBid.value) == "undefined") { // multiple entries
		for(i=0;i<document.forms[0].advertisementBid.length;i++) {
			if (document.forms[0].checkedStates[i+1].checked)
				document.forms[0].advertisementBid[i].value = bid;
		}
	} else { // one entry
		if (document.forms[0].checkedStates[1].checked)
			document.forms[0].advertisementBid.value = bid;
	}
}
function saveAllZipSetBids() {
	var bid = document.forms[0].appliedZipSetBid.value;

	if (isEmpty(bid) || containChar(bid)) {
		alert("Please enter valid bid price!");
		return;
	}
	var o = document.getElementsByName("advertisementZipSetBid");

	if (document.forms[0].advertisementZipSetBid){
	if (typeof(document.forms[0].advertisementZipSetBid.value) == "undefined") { // multiple entries
		for(i=0;i<document.forms[0].advertisementZipSetBid.length;i++) {
			if (document.forms[0].checkedZipSets[i+1].checked)
				document.forms[0].advertisementZipSetBid[i].value = bid;
		}
	} else { // one entry
		if (document.forms[0].checkedZipSets[1].checked)
			document.forms[0].advertisementZipSetBid.value = bid;
	}
	}
}
function encodeRE(s) { return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1') }
function parseDestTag(input,default_value) {
	var index1 = input.indexOf("{");
	var index2 = input.lastIndexOf("}");
	var patten = '/\$state/$/g';
	if ((index1 != -1 && index2 == -1) || (index1 == -1 && index2 != -1) || (index1 > index2)) {
		return "-1";
	}
	if (index1 != -1 && index2 != -1 && index1<index2) {
		var destTagContent = input.substring(index1+1, index2).replace(" ", "");
		var tagParts = destTagContent.split(",");
		if (tagParts.length==2) {
				if (tagParts[0].toLowerCase()=="$statecode$" || tagParts[0].toLowerCase()=="$state$" || tagParts[0].toLowerCase()=="$sc$" || tagParts[0].toLowerCase()=="$zip$") {
					if (tagParts[1].length>2 && tagParts[1].charAt(0)=='<' && tagParts[1].charAt(tagParts[1].length-1) == '>') {
						if (default_value != true)
							var defualtTag = tagParts[1].substring(1, tagParts[1].length-1);
						else {
							if (tagParts[0].toLowerCase()=="$statecode$" || tagParts[0].toLowerCase()=="$sc$") {
								var defualtTag = "CA";
							} else if (tagParts[0].toLowerCase()=="$state$") {
								var defualtTag = "California";
							} else if (tagParts[0].toLowerCase()=="$zip$") {
								var defualtTag = "90210";
							}
						}
						var inputWoTag = input.substring(0, index1);
						if (index2==(input.length-1))
							result = input.substring(0, index1) + defualtTag;
						else {
							result = input.substring(0, index1) + defualtTag + input.substring(index2+1,input.length);
							inputWoTag = inputWoTag + input.substring(index2+1,input.length);
						}
						if (inputWoTag.match(/\$[\w]+\$/g)!=null) {
							return '-1';
						} else {
							return result; // dest tag format correct, return by replace it by default value
						}
					} 
				}
		}
		// dest tag verifcation fails return -1
		return "-1";
	} else { // no dest tag
		if (input.match(/\$[\w]+\$/g)!=null) {
			return '-1';
		} else {
			return input;
		}
	}
}

function testAdvertisementUrl(index)
{
  var link;
  link=document.getElementById('url_'+index).value; 
  l = parseDestTag(link);
  if (l=="-1") {
  	alert("Geo Token Syntax Error");
  	return;
  }
  	
  popup = window.open(l, "", "width=800,height=650,status=1,resize=1,scrollbars=1");
  popup.focus();
}

function testAdvertisementZipSetUrl(index)
{
  var link;
  link=document.getElementById('zipset_url_'+index).value; 
  l = parseDestTag(link);
  if (l=="-1") {
  	alert("Geo Token Syntax Error");
  	return;
  }
  	
  popup = window.open(l, "", "width=800,height=650,status=1,resize=1,scrollbars=1");
  popup.focus();
}

function checkAll() {
	if (document.forms[0].checkedStates[0].checked) {
		for (i=0; i < document.forms[0].checkedStates.length; i++) {
			document.forms[0].checkedStates[i].checked = true;
		}
	} else {
		for (i=0; i < document.forms[0].checkedStates.length; i++) {
			document.forms[0].checkedStates[i].checked = false;
		}
	}
}

function checkAllZipSets() {
	if (document.forms[0].checkedZipSets[0]){
	if (document.forms[0].checkedZipSets[0].checked) {
		for (i=0; i < document.forms[0].checkedZipSets.length; i++) {
			document.forms[0].checkedZipSets[i].checked = true;
		}
	} else {
		for (i=0; i < document.forms[0].checkedZipSets.length; i++) {
			document.forms[0].checkedZipSets[i].checked = false;
		}
	}
	}
}

function copyToField(fromDiv, toDiv) {
	document.getElementsByName(toDiv)[0].value = document.getElementsByName(fromDiv)[0].value;
}

function validateUpload(id) {
	var fileName = document.getElementById(id).value;
	if (fileName == "") {
		alert("Please select a file to upload.");
		return false;
	}
	onUpload();
	return true;
}

/////


