var foc = 0;
var focName='';
function getXMLHTTP()
{ 
	//fuction to return the xml http object	
	var xmlhttp=false;
	try{
	xmlhttp=new XMLHttpRequest();
	}
	catch(e) {
	try{
	xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e){
	try{
	req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e1){
	xmlhttp=false;
	}
	}
	}
	
	return xmlhttp;
}

function div_file_attach(strURL,name)
{
	var req = getXMLHTTP();
	if (req)
	{
		req.onreadystatechange = function()
	{
	if (req.readyState == 4)
	{		
		if (req.status == 200)
		{
			try {
				
					if(parseInt(document.getElementById(name + "file_count").value) > 0)
					{
						//document.getElementById("file_count").value = parseInt(document.getElementById("file_count").value) - 1;	
						var file_count = parseInt(document.getElementById(name + "file_count").value) - 1;	
					}
					else
						file_count =  1 ;
				//var file_count = parseInt(document.getElementById("file_count").value) ;	

				var divTag = document.createElement("div");
				divTag.innerHTML=req.responseText;
				document.getElementById(name + "file_attach_exclusion").appendChild(divTag);
				document.getElementById(name + "img" + file_count).src  = HOST_ROOT_PATH_VAR + "images/DefaultImage/sub_blue_icon.gif";
				//document.getElementById("lnk" + file_count).onClick  = function () { alert('here'); };
				document.getElementById(name + "lnk" + file_count).href = "javascript:removeField('" + file_count +"','" + name +"')";
				
				
							/////  New Requirements ////////////////
	//alert(window.parent.document.getElementById('childframe1'));						
							
if(window.parent.document.getElementById('childframe1'))
	{	window.parent.document.getElementById('childframe1').style.height = '';
		
		window.parent.document.getElementById('childframe1').style.height = (parseInt(window.parent.document.getElementById('childframe1').contentWindow.document.body.scrollHeight) + parseInt(35)) + "px";
		
	}
	
 if(window.parent.document.getElementById('providerFrame1'))
	{	window.parent.document.getElementById('providerFrame1').style.height = '';
		
		window.parent.document.getElementById('providerFrame1').style.height = (parseInt(window.parent.document.getElementById('providerFrame1').contentWindow.document.body.scrollHeight) + parseInt(35)) + "px";
		
	}


//////////////////////////////
				
			// alert( document.getElementById("file1").value)
			}
			catch (e) 
			{
				//alert(file_count + "ravi");
				// IE fails unless we wrap the string in another element.			
				document.getElementById(name + "file_attach_exclusion").innerHTML = req.responseText;	
				
	
				
			}
		}
		else
		{
			alert("There was a problem while using XMLHTTP:\n" + req.statusText);
		}
	}
	}
	req.open("GET", strURL, true);
	req.send(null);


	}
	
	
}




function addMore(ele,count_file,name)
{
//alert(ele+ " "+  count_file+" " +name)
	var file_count=document.getElementById( name + "file_count").value;	
	//alert(file_count+"     xvxvxvxvxvx     "+(file_count+1));
	
	
	if(count_file==0)
	{
		select_And_Or = $('#' + name + 'AndOr_'+file_count).attr('value');
	}
	else
	{
		select_And_Or = $('#' + name + 'AndOr_'+file_count).attr('value');
	}
	


	if(select_And_Or != 'none')
	{
			$('#resetSpan'+file_count).html('');
		
		var file_count=document.getElementById(name + "file_count").value;	
		var campaign_id= document.getElementById( name + "campaign_id").value;	

		document.getElementById(name + "file_count").value= parseInt(file_count) + 1;
	
	div_file_attach(HOST_ROOT_PATH_VAR+"code/CreateExclusionRule/AjexCreateExclusionRule.php?file_count="+file_count+'&campaignId='+campaign_id + "&name=" + name ,name);
		document.getElementById( name + "file_attach_exclusion").style.display='block';
		
		
			if($('#' + name + 'storeExclusionRulesIds').attr('value')=='')
			{
				$('#' + name + 'storeExclusionRulesIds').attr('value', parseInt(file_count) + 1)
			}
			else
			{
				$('#' + name + 'storeExclusionRulesIds').attr('value',$('#' + name + 'storeExclusionRulesIds').attr('value')+","+(parseInt(file_count) + 1))
			}
			
			 var selector = "[value='none']";
		$("#" + name + "AndOr_"+file_count).children(selector).remove();
		
		$('#exclusionDiv').html('');
	}
	else
	{
		$('#exclusionDiv').html('Please select And/Or from the drop down');
		//displayMessage("exclusionDiv","Please select And from the drop down",2);
	}
		// The Particular code is used  to remove option 'none' from the combo box
		//var temp2= file_count+1;
		//$('#'+name+'FieldName_'+file_count).focus();
		
		//window.scrollTo(600,200);
		
	// var selector = "[value='none']";
//	$("#" + name + "AndOr_"+count_file).children(selector).remove();
	foc = parseInt(count_file)+1;
	focName =name
	//console.log(foc)
//alert(foc);
 setTimeout("setFous()", 430)
///////////////////////////////////////////////////////////////////////////////


}

function setFous()
{//console.log(foc)
//alert(focName)

//alert($('#'+focName+'criteria_'+foc).attr('value'))
	$('#'+focName+'criteria_'+foc).focus();
	foc=0;
}


function removeField(id, name)
{	
	//alert(id);
	var focRemove= parseInt(id)+1;
focName =name
	
	$("#" + name + "div_" + id).remove();
	
	
	if(window.parent.document.getElementById('childframe1'))
	{	window.parent.document.getElementById('childframe1').style.height = '';
		
		window.parent.document.getElementById('childframe1').style.height = (parseInt(window.parent.document.getElementById('childframe1').contentWindow.document.body.scrollHeight) + parseInt(35)) + "px";
		
	}
	
	 if(window.parent.document.getElementById('providerFrame1'))
	{	window.parent.document.getElementById('providerFrame1').style.height = '';
		
		window.parent.document.getElementById('providerFrame1').style.height = (parseInt(window.parent.document.getElementById('providerFrame1').contentWindow.document.body.scrollHeight) + parseInt(35)) + "px";
		
	}
	
	
		var exclusionFieldIdIdArry =  $("#" + name + "storeExclusionRulesIds").attr('value').split(",");
				
				tempArrayIndex = findArrayUser(id, name );
				
				exclusionFieldIdIdArry.splice(tempArrayIndex,1);
				
				$("#" + name + "storeExclusionRulesIds").attr('value',exclusionFieldIdIdArry);
				
				//console.log(focRemove);
		$('#criteria_'+focRemove).focus();
	//document.getElementById("div_"+id).style.display='none';
}

// This function is used to find the postion if the value in an array
function findArrayUser(value,name)
{
	var exclusionFieldIdIdArry =  $('#' + name +'storeExclusionRulesIds').attr('value').split(",");
	
		tempArray = exclusionFieldIdIdArry;
	
		for(i=0;i<tempArray.length;i++)
		{
			if(tempArray[i] == value)
				{
					return i;
				}
		}
}

////////////////////////////////////////////////////// Ravi ////////////////////////////////////////////////
function exclusionValidation(dataType,name,imgCount)
{

	if(trim(dataType) != "")
		$("#" + name + "criteria_" + imgCount).attr('disabled',"");
	else
	{
		$("#" + name + "criteria_" + imgCount).attr('value',"");
		$("#" + name + "criteria_" + imgCount).attr('disabled',"disabled");
	}
	if(trim(dataType) == "int")
	{
			document.getElementById(name + "criteria_" + imgCount).onkeypress= function (event) { return criteriaKeyEvent(event);};
			// $"return numbersonly(event)"
	}
	else
	{
		document.getElementById(name + "criteria_" + imgCount).onkeypress = "";
	}
	
	//$("#" + name + "criteriaValidate_" + imgCount).attr('value',dataType + "|" + name + "|" + imgCount);
}

function criteriaKeyEvent(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if(charCode == 45)
		return true;
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	
	return true;

}

function filedCriteriaValidate(ele)
{
	var tempItem = ele.value.split("|");
	if(trim(tempItem[0]) == "")
	{
		$("#" + tempItem[1] + "criteria_" + tempItem[2]).attr('disabled',"disabled");
	}
	
}

var countPrice2=0;
function fetchCampField(name,countPrice)
{countPrice2=countPrice;
	//alert(countPrice);
	campaignId= $('#'+name+'CampaignName_'+countPrice).attr("value");
	//alert(campaignId);
		$.post( HOST_ROOT_PATH_VAR + 'code/CreateExclusionRule/AjexFetchField.php',{"countPrice":countPrice,"campaignId":campaignId,"name":name}, reponseFetchCampField, "html");
}
/////////////////////////////////////////////////////// END ////////////////////////////////////////////////
function reponseFetchCampField(d,s)
{
	//alert(d);
	$('#campField_'+countPrice2).html(d);
}
var countPrice1=0;

function fetchCampFieldAjex(name,countPrice)
{
	//alert(countPrice);
	countPrice1=countPrice;
		campaignId= $('#'+name+'CampaignName_'+countPrice).attr("value");
	//alert(campaignId);
	
		$.post( HOST_ROOT_PATH_VAR + 'code/CreateExclusionRule/AjexFetchField.php',{"countPrice":countPrice1,"campaignId":campaignId,"name":name}, reponseFetchCampFieldAjex, "html");
}
/////////////////////////////////////////////////////// END ////////////////////////////////////////////////
function reponseFetchCampFieldAjex(d,s)
{
	//alert(d)
	$('#campFieldAjex_'+countPrice1).html(d);
}

var countPrice4=0;

function showDivValueOther(checkDiv,countPrice,name,modeClear,condition_id)
{
	//alert(checkDiv +"  "+ countPrice+"  "+name)
	countPrice4=countPrice;
	if(checkDiv=='val')
	{	
		if(modeClear!='update')
		{
			$('#'+name+'CampaignName_'+countPrice).attr("value",'');
			$('#'+name+'FieldNameOther_'+countPrice).attr("value",'');
		}
		
		$('#'+name+'excusion_type_'+countPrice).attr("value",'v');
		$('#showValue_'+countPrice).show();
		//$('#showOther_'+countPrice).hide();
		$('#showOther1_'+countPrice).hide();
		$.post( HOST_ROOT_PATH_VAR + 'code/CreateExclusionRule/AjexFetchConditions.php',{"type":'v',"countPrice":countPrice4,"name":name,"condition_id":condition_id}, reponseShowDivValueOther, "html");
	}
	else if (checkDiv=='oth')
	{//alert(checkDiv)
		$('#'+name+'excusion_type_'+countPrice).attr("value",'o');
		
		if(modeClear!='update')
		{
		$('#criteria_'+countPrice).attr('value',"");
		}
		$('#showValue_'+countPrice).hide();
				$('#showOther1_'+countPrice).show();
				
		$.post( HOST_ROOT_PATH_VAR + 'code/CreateExclusionRule/AjexFetchConditions.php',{"type":'o',"countPrice":countPrice4,"name":name,"condition_id":condition_id}, reponseShowDivValueOther, "html");		
		//document.getElementById(showOther1).style.display == 'block';
		//document.getElementById(showOther1).style.display == 'block'
	}
}

function reponseShowDivValueOther(d,s)
{
	$('#conditionDiv_'+countPrice4).html(d);
}

function resetExclusion(name,count)
{
	//alert(count);
	
		$('#'+name+'FieldName_'+count).attr('value','');
		$('#'+name+'FieldNameOther_'+count).attr('value','');
		$('#'+name+'conditionName_'+count).attr('value','');
		$('#'+name+'criteria_'+count).attr('value','');
		$('#'+name+'CampaignName_'+count).attr('value','');
		$('#'+name+'AndOr_'+count).attr('value','none');
}
