var AdRateList = "";
var AdCategoryIDList = "";

function BuildAdRate(AdRate)
{
	if(AdRate != "")
	{
		var RowCount = parseInt(document.getElementById("hidRowCount").value);
		var HidCatagory = document.getElementById("hidCatagory").value
		
		var tdRowText;
		
		var valueArray = AdRate.split("<#>");
		
		var canInsert = "1";
		
		/*for(var i = 0; i < RowCount; i++)
		{
			tdRowText = document.getElementById("hidCategory" + i).value;
			if(tdRowText == valueArray[6])
			{
				canInsert = "0";
				break;
			}
		}*/
		
		if(document.getElementById("hidCategory-" + valueArray[6]))
		{
			canInsert = "0";
		}
		
		if(canInsert == "1")
		{
			/*
			var tableTemp = document.getElementById("CatagoryAdRates");
			
			var newRow = tableTemp.insertRow(-1);
			
			var newCell0 = newRow.insertCell(-1);
			newCell0.colSpan = "6";
			newCell0.innerHTML = "<table border=\"0\" width=\"100%\" cellspacing=\"1\" id=\"PageValue" + valueArray[6] + "\"></table>";
			*/
			
			if(!document.getElementById("PageValue" + valueArray[6]))
			{
				var tableTemp = document.getElementById("CatagoryAdRates");
			
				var newRow = tableTemp.insertRow(-1);
				
				var newCell1 = newRow.insertCell(-1);
				newCell1.colSpan = "6";
				newCell1.innerHTML = "<table border=\"0\" width=\"100%\" cellspacing=\"1\" id=\"PageValue" + valueArray[6] + "\"></table>";
			}
			
			var tableTemp2 = document.getElementById("PageValue" + valueArray[6]);
			var tblRow = tableTemp2.rows.length;
			var newRow3 = tableTemp2.insertRow(-1);
			newRow3.name="tblRow-" + valueArray[6];
			newRow3.bgColor = "#ffffff";
			
			var newCell0 = newRow3.insertCell(-1);
			newCell0.id = "row" + valueArray[6];
			newCell0.innerHTML = "<input type=\"hidden\" id=\"hidCategory-" + valueArray[6] + "\" value=\"" + valueArray[6] + "\">Daily Rates";
			newCell0.align ="center";
			
			var newCellC;
			var newCellR;
			
			for (var j = 1; j < 6; j++)
			{				
				newCellR = newRow3.insertCell(-1);
				newCellR.innerText = "$ " + valueArray[j];
				newCellR.align ="right";
				newCellR.width = "75";
			}
			
			var newRow2 = tableTemp2.insertRow(-1);
			newRow2.name="tblRow-" + valueArray[6] + "-Copy";
			newRow2.bgColor = "#ffffff";
			
			newCell0 = newRow2.insertCell(-1);
			newCell0.innerHTML = valueArray[0] + " Rotation";
			newCell0.align ="center";
			
			for (var j = 1; j < 6; j++)
			{
				newCellC = newRow2.insertCell(-1);
				newCellC.innerHTML = "<input type=\"text\" value=\"0\" size=\"3\" id=\"txtrow" + valueArray[6] + "cell" + j + "\" class=\"txtInfo2\" onclick=\"javascript:this.select();\" onmouseover=\"javascript:this.select();\" onkeypress=\"javascript:numkeypress();\" />";
				newCellC.align ="right";
				newCellC.width = "75";
			}
			
			RowCount = RowCount + 1;
			
			document.getElementById("hidRowCount").value = RowCount;
			document.getElementById("hidCatagory").value = HidCatagory + valueArray[6] + ",";
			//alert(document.getElementById("hidCatagory").value);
		}
	}
}

function BuildAdRateByCategory(AdRate,PageValue)
{
	if(AdRate != "")
	{
		var RowCount = parseInt(document.getElementById("hidRowCount").value);		
		
		var HidCatagory = document.getElementById("hidCatagory").value
		
		var tdRowText;
		
		var valueArray = AdRate.split("<#>");
		
		var pagevalueArray = PageValue.split("<#>");
		
		var canInsert = "1";
		
		/*for(var i = 0; i < RowCount; i++)
		{
			tdRowText = document.getElementById("hidCategory" + i).value;
			if(tdRowText == valueArray[6])
			{
				canInsert = "0";
				break;
			}
		}*/
		
		if(document.getElementById("hidCategory-" + valueArray[6]))
		{
			canInsert = "0";
		}
		
		if(canInsert == "1")
		{					
			var tableTemp = document.getElementById("PageValue" + pagevalueArray[6]);
			
			var newRow = tableTemp.insertRow(-1);
			//var tblRow = tableTemp.rows.length;
			newRow.name="tblRow-" + valueArray[6];
			newRow.bgColor = "#ffffff";
			
			var newCell0 = newRow.insertCell(-1);
			newCell0.id = "row" + valueArray[6];
			newCell0.innerHTML = "<input type=\"hidden\" id=\"hidCategory-" + valueArray[6] + "\" value=\"" + valueArray[6] + "\">Daily Rates";
			newCell0.align ="center";
			
			var newCellC;
			var newCellR;
			
			for (var j = 1; j < 6; j++)
			{				
				newCellR = newRow.insertCell(-1);
				newCellR.innerText = "$ " + valueArray[j];
				newCellR.align ="right";
				newCellR.width = "75";
			}
			
			var newRow2 = tableTemp.insertRow(-1);
			newRow2.name="tblRow-" + valueArray[6] + "-Copy";
			newRow2.bgColor = "#ffffff";
			
			newCell0 = newRow2.insertCell(-1);
			newCell0.innerHTML = valueArray[0] + " Rotation";
			newCell0.align ="center";
			
			for (var j = 1; j < 6; j++)
			{
				newCellC = newRow2.insertCell(-1);
				newCellC.innerHTML = "<input type=\"text\" value=\"0\" size=\"3\" id=\"txtrow" + valueArray[6] + "cell" + j + "\" class=\"txtInfo2\" onclick=\"javascript:this.select();\" onmouseover=\"javascript:this.select();\" onkeypress=\"javascript:numkeypress();\" />";
				newCellC.align ="right";
				newCellC.width = "75";
			}
			
			
			RowCount = RowCount + 1;
			
			document.getElementById("hidRowCount").value = RowCount;
			document.getElementById("hidCatagory").value = HidCatagory + valueArray[6] + ",";
		}
	}
}

function BuildAdRateList(AdRate, PageValue)
{
	ShowTable();
	
	var AdRateListArray = AdRate.split("|");
	
	if(AdRateList == "")
	{
		//AdRateList = AdRate;
	}
	
	var AdRateArray;
	
	var AdCategory = "";
	
	var AdCategoryList = "";
	
	var RowCount = parseInt(document.getElementById("hidRowCount").value);
	
	var PageValueArray = PageValue.split("<#>");
	
	for(var i = 0; i < AdRateListArray.length; i++)
	{
		AdRateArray = AdRateListArray[i].split("<#>");
		
		if(AdCategoryIDList == "")
		{
			AdCategoryIDList = AdCategoryIDList + "," + AdRateArray[6];
			AdRateList = AdRateListArray[i];
		}
		else
		{
			if((AdCategoryIDList + ",").indexOf("," + AdRateArray[6] + ",") == -1)
			{
				AdCategoryIDList = AdCategoryIDList + "," + AdRateArray[6];
				AdRateList = AdRateList + "|" + AdRateListArray[i];
				//alert(AdRateList);
			}
		}
		
		if(i < AdRateListArray.length)
		{
			var canInsert = "1";
				
			/*for(var j = 0; j < RowCount; j++)
			{
				tdRowText = document.getElementById("hidCategory" + j).value;
				if(AdRateArray[6] == tdRowText)
				{
					canInsert = "0";
					break;
				}
			}*/
			
			if(document.getElementById("hidCategory-" + AdRateArray[6]))
			{
				canInsert = "0";
			}
		
			if(document.getElementById("Category-" + AdRateArray[6] + "-" + i ))
			{
				if(document.getElementById("Category-" + AdRateArray[6] + "-" + i ).checked)
				{
					AdCategory = AdCategory + "<input type=\"checkbox\" id=\"Category-" + AdRateArray[6] + "-" + i + "\" checked disabled=\"false\"> " + AdRateArray[0] + " > ";
				}
				else
				{
					AdCategory = AdCategory + "<input type=\"checkbox\" id=\"Category-" + AdRateArray[6] + "-" + i + "\" disabled=\"false\"> " + AdRateArray[0] + " > ";
				}
			}
			else if(canInsert == "0")
			{
				AdCategory = AdCategory + "<input type=\"checkbox\" id=\"Category-" + AdRateArray[6] + "-" + i + "\" checked disabled=\"false\"> " + AdRateArray[0] + " > ";
			}
			else
			{
				if(PageValueArray[6] == AdRateArray[6])
				{
					AdCategory = AdCategory + "<input type=\"checkbox\" id=\"Category-" + AdRateArray[6] + "-" + i + "\" checked onclick=\"javascript:AddCategory('Category-" + AdRateArray[6] + "-" + i + "','" + AdRateArray[6] + "', '" + PageValue + "');\"> " + AdRateArray[0] + " > ";
				}
				else
				{
					AdCategory = AdCategory + "<input type=\"checkbox\" id=\"Category-" + AdRateArray[6] + "-" + i + "\" onclick=\"javascript:AddCategory('Category-" + AdRateArray[6] + "-" + i + "','" + AdRateArray[6] + "', '" + PageValue + "');\"> " + AdRateArray[0] + " > ";
				}
				AdCategoryList = AdCategoryList + "Category-" + AdRateArray[6] + "-" + i + ",";
			}
		}
	}
	
	if(AdCategory != "")
	{
		if(canInsert == "1")
		{
			AdCategory = AdCategory + "<input type=\"checkbox\" id=\"SelectAll" + PageValue + "\" onclick=\"javascript:SelectAllCategory('" + AdCategoryList + "','" + PageValue + "');\"> Select All ";
		}
		else
		{
			alert("The category you select have been exist.");
			return;
		}
		
		var tableTemp = document.getElementById("CatagoryAdRates");
			
		var newRow = tableTemp.insertRow(-1);
		newRow.bgColor = "#cccccc";
			
		var newCellC = newRow.insertCell(-1);
		newCellC.colSpan = "6";
		newCellC.innerHTML = AdCategory;
		
		newRow = tableTemp.insertRow(-1);
		newCellC = newRow.insertCell(-1);
		newCellC.colSpan = "6";
		newCellC.innerHTML = "<table border=\"0\" width=\"100%\" cellspacing=\"1\" id=\"PageValue" + PageValueArray[6] + "\"></table>";
				
		tableTemp = document.getElementById("PageValue" + PageValueArray[6]);
		
		newRow = tableTemp.insertRow(-1);
		newRow.bgColor = "#efefef";
		
		newCellC = newRow.insertCell(-1);
		newCellC.innerHTML = "<b>Ad Types</b>";
		newCellC.align = "center";
		
		newCellC = newRow.insertCell(-1);
		newCellC.innerHTML = "<b>BILLBOARD</b>";
		newCellC.align = "center";
		newCellC.width = "75";
		
		newCellC = newRow.insertCell(-1);
		newCellC.innerHTML = "<b>BLIMP</b>";
		newCellC.align = "center";
		newCellC.width = "75";
		
		newCellC = newRow.insertCell(-1);
		newCellC.innerHTML = "<b>RAINY DAY</b>";
		newCellC.align = "center";
		newCellC.width = "75";
		
		newCellC = newRow.insertCell(-1);
		newCellC.innerHTML = "<b>HOT AIR</b>";
		newCellC.align = "center";
		newCellC.width = "75";
		
		newCellC = newRow.insertCell(-1);
		newCellC.innerHTML = "<b>ROAD SIGN</b>";
		newCellC.align = "center";
		newCellC.width = "75";
	}
}

function AddCategory(CheckBoxID, CateID, PageValue)
{
	var AdRateListArray = AdRateList.split("|");
	var AdRateArray;
	for(var i = 0; i < AdRateListArray.length; i++)
	{
		AdRateArray = AdRateListArray[i].split("<#>");
		
		//alert(AdRateList);
		//alert(CateID + "," + AdRateArray[6]);
		
		if(CateID == AdRateArray[6])
		{
			if(!document.getElementById(CheckBoxID).checked)
			{
				DeleteCategory(AdRateListArray[i], PageValue);
				document.getElementById(CheckBoxID).checked = false;
			}
			else
			{
				BuildAdRateByCategory(AdRateListArray[i], PageValue);
				document.getElementById(CheckBoxID).checked = true;
			}
			break;
		}
	}
}

function DeleteCategory(AdRate,PageValue)
{
	if(AdRate != "")
	{				
		var RowCount = parseInt(document.getElementById("hidRowCount").value);
		
		var HidCatagory = document.getElementById("hidCatagory").value;
		
		var valueArray = AdRate.split("<#>");
		
		var pagevalueArray = PageValue.split("<#>");
		
		var tableTemp = document.getElementById("PageValue" + pagevalueArray[6]);		

		var deleteTemp = "";
		
		for (i=0; i<tableTemp.rows.length; i++)
		{
			tdname=tableTemp.rows[i].name;
			if (tdname == "tblRow-" + valueArray[6] || tdname == "tblRow-" + valueArray[6] + "-Copy")
			{
				deleteTemp = deleteTemp + i + ",";
			}
		}
		
		var deleteTempArray = deleteTemp.split(",");
		
		for (i = deleteTempArray.length - 2; i >= 0; i --)
		{
			tableTemp.deleteRow(deleteTempArray[i]);
		}
		
		RowCount = RowCount - 1;
		
		document.getElementById("hidRowCount").value = RowCount;
		document.getElementById("hidCatagory").value = HidCatagory.replace("," + valueArray[6] + ",", ",");
	}
}

function GetOrderInfo()
{
	var RowCount = parseInt(document.getElementById("hidRowCount").value);
	var CheckInfo;
	var CheckAll = "0";
	
	var el = document.getElementsByTagName('input');
	var len = el.length;
	
	for(var i=0; i<len; i++)
	{
		if((el[i].type=="text") && CheckRotation(el[i].value) > 0)
		{
			CheckAll = "1";
			break;
		}
	}
	
	var HomeFirstRotation = CheckRotation(document.getElementById("txtFirstRotation").value);
	var HomeSecondRotation = CheckRotation(document.getElementById("txtSecondRotation").value);
	var HomeThirdRotation = CheckRotation(document.getElementById("txtThirdRotation").value);
	var HomeFourthRotation = CheckRotation(document.getElementById("txtFourthRotation").value);
	var HomeFifthRotation = CheckRotation(document.getElementById("txtFifthRotation").value);
	
	CheckInfo = "HomePage,";
	
	HomeFirstRotation >= 0 ? CheckInfo = CheckInfo + HomeFirstRotation + "<#>" : CheckInfo = CheckInfo + "0<#>";
	HomeSecondRotation >= 0 ? CheckInfo = CheckInfo + HomeSecondRotation + "<#>" : CheckInfo = CheckInfo + "0<#>";
	HomeThirdRotation >= 0 ? CheckInfo = CheckInfo + HomeThirdRotation + "<#>" : CheckInfo = CheckInfo + "0<#>";
	HomeFourthRotation >= 0 ? CheckInfo = CheckInfo + HomeFourthRotation + "<#>" : CheckInfo = CheckInfo + "0<#>";
	HomeFifthRotation >= 0 ? CheckInfo = CheckInfo + HomeFifthRotation + "<#>" : CheckInfo = CheckInfo + "0<#>";
	
	CheckInfo = CheckInfo + "|";
	
	var hiddenValue;
	var checkbValue;
	var rotationValue;
	
	//alert(document.getElementById("hidCatagory").value);
	
	var hidCatagory = document.getElementById("hidCatagory").value.split("<#>");
	
	for(var i = 1; i < hidCatagory.length - 1; i++)
	{
		//hiddenValue = document.getElementById('hidCategory'+ i);
		
		hiddenValue = hidCatagory[i];
		
		CheckInfo = CheckInfo + "Category-" + hiddenValue + "<#>";
		
		for(var j = 1; j < 6; j++)
		{
			//alert("txtrow" + hiddenValue + "cell" + j + "-" + document.getElementById("txtrow" + hiddenValue + "cell" + j).value);
			rotationValue = CheckRotation(document.getElementById("txtrow" + hiddenValue + "cell" + j).value);
			
			if(rotationValue >= 0)
			{
				CheckInfo = CheckInfo + rotationValue + "<#>";
			}
			else
			{
				CheckInfo = CheckInfo + "0<#>";
			}
		}
		
		CheckInfo = CheckInfo + "|";
	}
	
	document.getElementById("hidCart").value = CheckInfo;
	
	if(CheckAll == "0")
	{
		alert("Please select the ads and pages you want to advertise on!");
		return false;
	}
	else
	{
		//alert(CheckInfo);
		return true;
	}
}

function GetOrderInfo2()
{
	var RowCount = parseInt(document.getElementById("hidRowCount").value);
	var CheckInfo;
	var CheckAll = "0";
	
	var el = document.getElementsByTagName('input');
	var len = el.length;
	
	for(var i=0; i<len; i++)
	{
		if((el[i].type=="text") && CheckRotation(el[i].value) > 0)
		{
			CheckAll = "1";
			break;
		}
	}
	
	var hiddenValue;
	var checkbValue;
	var rotationValue;
	
	//alert(document.getElementById("hidCatagory").value);
	
	var hidCatagory = document.getElementById("hidCatagory").value.split(",");
	
	//alert(hidCatagory);
	
	for(var i = 1; i < hidCatagory.length - 1; i++)
	{
		//hiddenValue = document.getElementById('hidCategory'+ i);
		
		hiddenValue = hidCatagory[i];
		
		CheckInfo = CheckInfo + "Category-" + hiddenValue + "<#>";
		
		for(var j = 1; j < 6; j++)
		{
			//alert("txtrow" + hiddenValue + "cell" + j + "-" + document.getElementById("txtrow" + hiddenValue + "cell" + j).value);
			rotationValue = CheckRotation(document.getElementById("txtrow" + hiddenValue + "cell" + j).value);
			
			if(rotationValue >= 0)
			{
				CheckInfo = CheckInfo + rotationValue + "<#>";
			}
			else
			{
				CheckInfo = CheckInfo + "0<#>";
			}
		}
		
		CheckInfo = CheckInfo + "|";
	}
	
	document.getElementById("hidCart").value = CheckInfo;
	
	if(CheckAll == "0")
	{
		alert("Please select the ads and pages you want to advertise on!");
		return false;
	}
	else
	{
		//alert(CheckInfo);
		return true;
	}
}

function CheckRotation(checkvalue)
{
	if(checkvalue == "")
	{
		return "0";
	}
	
	try
	{
		if(parseInt(checkvalue) >= 0 && parseInt(checkvalue) <= 20)
		{
			return checkvalue;
		}
		else if(parseInt(checkvalue) >= 20)
		{
			return "20";
		}
	}
	catch(err)
	{
		return "0";
	}
	
	return "-1";
}

function BindCartInfo()
{
	var ajax = GetObjects();
	
	d = new Date();
	t = d.getTime();

	var url = "/XmlHttpRequest.aspx?type=getcartinfo&checkvalue=" + t;
	
	ajax.open("GET", url, false);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    ajax.send(null);
	
	var result = ajax.responseText;	
    
	//alert(result);
    
    if(result != "")
    { 
		ShowTable();
		document.getElementById("divFrameAd").style.display = "none";
		document.getElementById("divCategory").style.display = "none";
		document.getElementById("divCategory2").style.display = "block";
		
        var allArray = result.split("|"); 
        var thisArray, cateArray;
        for(var i = 0; i < allArray.length; i++) 
        {
			thisArray = allArray[i].split("<#>");
			if(thisArray[0] == "HomePage")
			{
				if(parseInt(thisArray[1]) > 0)
				{
					document.getElementById("txtFirstRotation").value = parseInt(thisArray[1]);
				}
				if(parseInt(thisArray[2]) > 0)
				{
					document.getElementById("txtSecondRotation").value = parseInt(thisArray[2]);
				}
				if(parseInt(thisArray[3]) > 0)
				{
					document.getElementById("txtThirdRotation").value = parseInt(thisArray[3]);
				}
				if(parseInt(thisArray[4]) > 0)
				{
					document.getElementById("txtFourthRotation").value = parseInt(thisArray[4]);
				}
				if(parseInt(thisArray[5]) > 0)
				{
					document.getElementById("txtFifthRotation").value = parseInt(thisArray[5]);
				}
			}
			else
			{
				if(thisArray[0].indexOf("-") > -1)
				{
					cateArray = thisArray[0].split("-");
					
					var returnValue = GetCategoryAdRate(cateArray[1]);
					
					//alert(returnValue);
					
					BuildAdRate(returnValue);
					
					var RowCount = parseInt(document.getElementById("hidRowCount").value);
					
					//alert(RowCount);
					
					for(var j = 1; j < thisArray.length - 1; j ++)
					{
						//alert(thisArray[0]);
						document.getElementById("txtrow" + cateArray[1] + "cell" + j).value = thisArray[j];
						
						//alert("txtrow" + (parseInt(RowCount) - 1) + "cell" + j);
					}
				}
			}
        }
    }
}

function BindCartInfo2()
{
	var ajax = GetObjects();
	
	d = new Date();
	t = d.getTime();

	var url = "/XmlHttpRequest.aspx?type=getcartinfo&checkvalue=" + t;
	
	ajax.open("GET", url, false);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    ajax.send(null);
	
	var result = ajax.responseText;	
    
	//alert(result);
    
    if(result != "")
    { 		
        var allArray = result.split("|"); 
        var thisArray, cateArray;
        var showFlag = false;
        for(var i = 0; i < allArray.length; i++) 
        {
			thisArray = allArray[i].split("<#>");
			if(thisArray[0].indexOf("-") > -1)
			{
				cateArray = thisArray[0].split("-");
				
				var returnValue = GetCategoryAdRate(cateArray[1]);
				
				//alert(returnValue);
				
				BuildAdRate(returnValue);
				
				var RowCount = parseInt(document.getElementById("hidRowCount").value);
				
				//alert(RowCount);
				
				for(var j = 1; j < thisArray.length - 1; j ++)
				{
					//alert(thisArray[0]);
					document.getElementById("txtrow" + cateArray[1] + "cell" + j).value = thisArray[j];
					
					//alert("txtrow" + (parseInt(RowCount) - 1) + "cell" + j);
				}
				
				if(!showFlag)
				{
					showFlag = true;
				}
			}
        }
        
        if(showFlag)
        {
			ShowTable();
			document.getElementById("divFrameAd").style.display = "none";
			document.getElementById("divCategory").style.display = "none";
			document.getElementById("divCategory2").style.display = "block";
		}
    }
}

function ShowTable()
{
	var tableObj = document.getElementById("divCatagoryAdRates");
	
	if(tableObj.style.display == "none")
	{
		tableObj.style.display = "block";
	}
}

function ShowDivMessage(divname)
{
	var divObj = document.getElementById(divname);
	
	var bShow = true;
	
	if(divname == "divCategory" && document.getElementById("divCategory2").style.display == "block")
	{
		bShow = false;
	}
	
	if(divObj.style.display == "none" && bShow)
	{
		divObj.style.display = "block";
	}
}

function HidDivMessage(divname)
{
	var divObj = document.getElementById(divname);
	
	if(divObj.style.display == "" || divObj.style.display == "block")
	{
		divObj.style.display = "none";
	}
}

function SelectAllCategory(CategoryList, PageValue)
{	
	var CategoryArray = CategoryList.split(",");
	
	for(var i = 0 ; i < CategoryArray.length - 1; i++)
	{
		if(!document.getElementById(CategoryArray[i]).checked)
		{
			document.getElementById(CategoryArray[i]).checked = document.getElementById("SelectAll"+PageValue).checked;
			document.getElementById(CategoryArray[i]).onclick();
		}
		else
		{
			document.getElementById(CategoryArray[i]).checked = document.getElementById("SelectAll"+PageValue).checked;
			document.getElementById(CategoryArray[i]).onclick();
		}
	}
}
