var xmlHttp1;
var eleToSelectFrom=""
var priority = 0;
var newkeyword="";
var status = true;	// tells the status of image thats has to be updated in database
function fillColor(image_id,color_code){
	document.getElementById(image_id).style.backgroundColor = color_code;
}
function getElementId(imgid)
{
	// all hidden fields
	imagestatusid = "imagestatus_"+imgid;
	
	// all text boxes
	captionid = "caption_"+imgid;
	copyrightid = "copyrightinfo_"+imgid;
	// textarea
	keyword_textareaid = "keyword_textarea_"+imgid;
	// all div ids
	errorcaptionid = "errorcaption_"+imgid;
	errorkeywordid = "errorkeyword_"+imgid;
	// hidden
	hidkeywordsid = "hid_keywords_"+imgid;
	hiddencaptionid = "hiddencaption_"+imgid;
}


function makeReadOnly(objId)
{
		document.getElementById(objId).style.backgroundColor = "#dbdbdb";
		document.getElementById(objId).disabled = true;

}
function makeReadOnlyOff(objId)
{
		document.getElementById(objId).style.backgroundColor = "#ffffff";
		document.getElementById(objId).disabled = false;
}


function save_eps_preview(element_name,img_id){
	if(document.getElementById(element_name).checked){
		params = "eps_preview_value=2&imgid="+img_id+"&action=save_eps_preview";
		divid = "errorcaption_"+img_id;
		makeReadOnlyOff('keyword_textarea_'+img_id);
		makeReadOnlyOff('caption_'+img_id);
		showColor(img_id);
		ajaxpage('save_edit.php?'+params, divid,false);
	}else{
		params = "eps_preview_value=0&imgid="+img_id+"&action=save_eps_preview";
		divid = "errorcaption_"+img_id;	
		makeReadOnly('keyword_textarea_'+img_id);
		makeReadOnly('caption_'+img_id);
		document.getElementById('keyword_textarea_'+img_id).style.backgroundColor = "#dbdbdb";
		fillColor('caption_'+img_id , "#dbdbdb");		
		ajaxpage('save_edit.php?'+params, divid,false);

	}
}

function validateEpsDescription(imgId,elementName,errorMessage){
	var errorMsg = errorMessage.split("|");	
	var description = document.getElementById("description_" + imgId).value;		
	action = true;
	if(description.length > 250){
		fillColor("description_"+imgId , "#FFFF99");		
		document.getElementById("errorcaption_"+imgId).innerHTML=errorMsg[1];
		document.getElementById("description_" + imgId).value = "";
		document.getElementById("description_" + imgId).focus();		
		action = false;
	}
	if(action){
			params = "elementName="+elementName+"&description_value="+escape(description)+"&imgid="+imgId+"&action=save_description";
			divid = "response_"+imgId;
			ajaxpage('save_edit.php?'+params, divid,false);
	}
	return action;

}
function save_edit(imgid,elementName,errorMessage)
{
	var errorMsg = new Array();
	var changed = false;
	var isKeywordCheck = 0;
	status = true;
	getElementId(imgid);
	var params="";
	var copyright = trim(document.getElementById(copyrightid).value);
	var caption = trim(document.getElementById(captionid).value);
	var newvalue = trim(document.getElementById(elementName+"_"+imgid).value);
	var oldvalue = trim(document.getElementById("hidden"+elementName+"_"+imgid).value);
	var imagestatus = document.getElementById(imagestatusid).value;
	
	errorMsg  = errorMessage.split("|");  
	if(newvalue != oldvalue){	
		changed = true;
		}

			status_str = validateForm(caption,imgid,errorMessage,0);
			status_arr = status_str.split("|");
			if(status_arr[1] == errorMsg[0])
		//if(status_arr[1] == 'caption fail')
			{
				//document.getElementById(captionid).focus();
				return;
			}
		else
		{
			
			if(changed)
			{
				params = "elementName="+elementName+"&newvalue="+escape(newvalue)+"&imgid="+imgid+"&imagestatus="+imagestatus+"&action=saveimage"+"&status="+status_arr[0];
				divid = "response_"+imgid;
				ajaxpage('save_edit.php?'+params, divid,false);
				document.getElementById("hidden"+elementName+"_"+imgid).value =trim(escape(newvalue)) ;
				
			}
		}
	}

function addKeywords(imgid,errorMessage)
{
	var errorMsg = new Array();
	var keywordstr = "";
	var hid_keywordstr = "";
	var isKeywordCheck = 1;
	getElementId(imgid);
	var copyright = trim(document.getElementById(copyrightid).value);
	var caption = trim(document.getElementById(captionid).value);
	var imagestatus = document.getElementById(imagestatusid).value;
	
	xmlHttp1=GetXmlHttpObject();
	if (xmlHttp1==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	
		
	errorMsg  = errorMessage.split("|");  
	status_str = validateForm(caption,imgid,errorMessage,1);
	
	status_arr = status_str.split("|");
	if(status_arr[0] == "false")
	{
		//document.getElementById(keyword_textareaid).focus();
		return;
	}
	else
	{
	//alert("status for keywords::")
		document.getElementById("errorkeyword_"+imgid).innerHTML="&nbsp;";
		keywordstr = trim(document.getElementById(keyword_textareaid).value);
		keywordstr = keywordstr.replace(/\n/g,",");
		keywordstr = ((keywordstr.charAt(keywordstr.length-1,1) == ",") ? keywordstr.substring(0,keywordstr.length-1) : keywordstr);
		hid_keywordstr = document.getElementById(hidkeywordsid).value;
		var url = "save_edit.php?";
		var params = "kstr="+keywordstr+"&imgid="+imgid+"&imagestatus="+imagestatus+"&status="+status_arr[0];
	
		params = params + "&action="+document.getElementById(hidkeywordsid).value;
		params=params+"&sid="+Math.random();
		url = url+params;
		//alert('url::'+url);
		xmlHttp1.onreadystatechange=function(){
			stateChanged_keywords(xmlHttp1,imgid);
			};
		xmlHttp1.open("GET",url,true);
		xmlHttp1.send(null);
	}

}
function stateChanged_keywords(xmlHttp1,imgid)
{
	var response;
	var keywordArr = new Array();
	var captionArr = new Array();
	var captionVal = document.getElementById('caption_'+imgid).value;
	var keywordVal = document.getElementById(keyword_textareaid).value;
	captionArr = captionVal.split(" ");
	keywordArr = keywordVal.split(",");
	
	if (xmlHttp1.readyState==4 && xmlHttp1.status==200)
	{ 
		//alert(xmlHttp1.responseText)
		if(xmlHttp1.responseText != "")
		{
			response = xmlHttp1.responseText.split("|");
			document.getElementById("errorcaption_"+imgid).innerHTML = response[0];
			document.getElementById("hid_keywords_"+imgid).value = response[1];
			document.getElementById("hid_keyword_exist_"+imgid).value = response[2];

			if(keywordArr.length >= 3 && captionArr.length >= 1 && captionArr != "")
			{
					xmlHttp2=GetXmlHttpObject();
					if (xmlHttp2==null)
					{
						alert ("Your browser does not support AJAX!");
						return;
					}
					var url = "save_edit.php?";
					var params = "imgid="+imgid+"&onHold=0";
					params = params + "&action=holdImage";
					params=params+"&sid="+Math.random();
					url = url+params;
					xmlHttp2.onreadystatechange=function()
						{
							document.getElementById('submitStatus_'+imgid).checked=true;
							document.getElementById('submitStatus_'+imgid).title='image ready for submission';
							document.getElementById("errorcaption_"+imgid).innerHTML='image ready for submission';
						};
					xmlHttp2.open("GET",url,true);
					xmlHttp2.send(null);
				
			
			}
		}
	}
	else
	{
			document.getElementById("errorcaption_"+imgid).innerHTML='<img src="images/spinner_grey.gif" />';
	}
}
function validateForm(caption,imgid,errorMessage,isKeywordCheck)	
{
		var action = true;
		var isfocus = 0;
		var focustext = "";
		
		var keywordstr = "";
		var keywordAlreadyExist = document.getElementById('hid_keyword_exist_'+imgid).value;
		
		var check_fail ="";
		var captionarr = new Array();
		var hidden_caption = document.getElementById(hiddencaptionid).value;
		var errMsg = new Array();
		var keywordArr = new Array();
		var captionArr = new Array();
		
		
		errMsg  = errorMessage.split("|");
		
		// check for caption
	if(caption == "" && hidden_caption == "")	
	{
		//alert('caption blank');
		action=false;
		xmlHttp2=GetXmlHttpObject();
		if (xmlHttp2==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url = "save_edit.php?";
		var params = "imgid="+imgid+"&onHold=1";
		params = params + "&action=holdImage";
		params=params+"&sid="+Math.random();
		url = url+params;
		xmlHttp2.onreadystatechange=function()
		{
			document.getElementById('submitStatus_'+imgid).checked=false;
			document.getElementById('submitStatus_'+imgid).title='image not ready for submission';	
		};
		xmlHttp2.open("GET",url,true);
		xmlHttp2.send(null);
		if(!isKeywordCheck)
		check_fail = errMsg[0];
	}
	else if(!isKeywordCheck)
	{
		// check for caption	
		captionarr = caption.split(" ");
		var captionOK = 0;
		/*check if every entry of caption is right*/
		for(i=0 ; i<captionarr.length ; i++)
		{
			if(trim(captionarr[i]) != '')
				captionOK++;
		}
		
		if(caption != hidden_caption)
		{
			
			if(captionOK < 1)
			{
				if(!isKeywordCheck)
				{
					
					xmlHttp2=GetXmlHttpObject();
					if (xmlHttp2==null)
					{
						alert ("Your browser does not support AJAX!");
						return;
					}
					var url = "save_edit.php?";
					var params = "imgid="+imgid+"&onHold=1";
					params = params + "&action=holdImage";
					params=params+"&sid="+Math.random();
					url = url+params;
					xmlHttp2.onreadystatechange=function()
					{
						document.getElementById("errorcaption_"+imgid).innerHTML=errMsg[1];					
						document.getElementById('submitStatus_'+imgid).checked=false;
						document.getElementById('submitStatus_'+imgid).title='image not ready for submission';	
					};
					xmlHttp2.open("GET",url,true);
					xmlHttp2.send(null);
					check_fail = errMsg[0];
					document.getElementById('caption_'+imgid).style.backgroundColor = "#FFFF99";
					document.getElementById('caption_'+imgid).focus();
				}
				action =false;
				if(!isfocus)
					{
						isfocus = 1;
						focustext = document.getElementById("caption_"+imgid);
					}
					
				//alert('caption less than 3');
			}
			else
			{
				document.getElementById("caption_"+imgid).style.backgroundColor = "#FFFFFF";
				keywordVal = document.getElementById(keyword_textareaid).value;
				keywordArr = keywordVal.split(",");
				if(keywordArr.length >= 3)
				{
					xmlHttp2=GetXmlHttpObject();
					if (xmlHttp2==null)
					{
						alert ("Your browser does not support AJAX!");
						return;
					}
					var url = "save_edit.php?";
					var params = "imgid="+imgid+"&onHold=0";
					params = params + "&action=holdImage";
					params=params+"&sid="+Math.random();
					url = url+params;
					xmlHttp2.onreadystatechange=function()
						{
							document.getElementById('submitStatus_'+imgid).checked=true;
							document.getElementById('submitStatus_'+imgid).title='image ready for submission';
							document.getElementById("errorcaption_"+imgid).innerHTML='image ready for submission';
						};
					xmlHttp2.open("GET",url,true);
					xmlHttp2.send(null);
						
			
				}
			}
		}
		// if no chnage in caption
		else if(captionarr.length < 1)
		{
			document.getElementById('caption_'+imgid).style.backgroundColor = "#FFFF99";
			document.getElementById('caption_'+imgid).focus();
			action = false;
			xmlHttp2=GetXmlHttpObject();
			if (xmlHttp2==null)
			{
				alert ("Your browser does not support AJAX!");
				return;
			}
			var url = "save_edit.php?";
			var params = "imgid="+imgid+"&onHold=1";
			params = params + "&action=holdImage";
			params=params+"&sid="+Math.random();
			url = url+params;
			xmlHttp2.onreadystatechange=function()
			{
				document.getElementById('submitStatus_'+imgid).checked=false;
				document.getElementById('submitStatus_'+imgid).title='image not ready for submission';	
			};
			xmlHttp2.open("GET",url,true);
			xmlHttp2.send(null);
			if(!isKeywordCheck)
				check_fail = errMsg[0];
		}
	}
			
		// check for keyword		
		keywordstr = trim(document.getElementById(keyword_textareaid).value);
		keywordstr = keywordstr.replace(/\n/g,",");			
		if(keywordstr == '' && keywordAlreadyExist == 0)
		{
			//alert('keyword str blank');
			action = false;	
			xmlHttp2=GetXmlHttpObject();
			if (xmlHttp2==null)
			{
				alert ("Your browser does not support AJAX!");
				return;
			}
			var url = "save_edit.php?";
			var params = "imgid="+imgid+"&onHold=1";
			params = params + "&action=holdImage";
			params=params+"&sid="+Math.random();
			url = url+params;
			xmlHttp2.onreadystatechange=function()
			{
				document.getElementById('submitStatus_'+imgid).checked=false;
				document.getElementById('submitStatus_'+imgid).title='image not ready for submission';	
			};
			xmlHttp2.open("GET",url,true);
			xmlHttp2.send(null);
			if(isKeywordCheck)
				check_fail = errMsg[2];
		}
		else
		{		
			keywordstr = ((keywordstr.charAt(keywordstr.length-1,1) == ",") ? keywordstr.substring(0,keywordstr.length-1) : keywordstr);
			keywordarr = keywordstr.split(",");
			
			var keywordOK = true;
			for(i=0 ; i < keywordarr.length ; i++)
			{
					if(trim(keywordarr[i]) == '')
						keywordOK = false;
			}
			if(!keywordOK && isKeywordCheck)
			{
				action = false;
				xmlHttp2=GetXmlHttpObject();
				if (xmlHttp2==null)
				{
					alert ("Your browser does not support AJAX!");
					return;
				}
				var url = "save_edit.php?";
				var params = "imgid="+imgid+"&onHold=1";
				params = params + "&action=holdImage";
				params=params+"&sid="+Math.random();
				url = url+params;
				xmlHttp2.onreadystatechange=function()
				{
					document.getElementById("errorcaption_"+imgid).innerHTML=errMsg[4];					
					document.getElementById('submitStatus_'+imgid).checked=false;
					document.getElementById('submitStatus_'+imgid).title='image not ready for submission';	
				};
				xmlHttp2.open("GET",url,true);
				xmlHttp2.send(null);
				check_fail = errMsg[2];
				
			}
			else if(keywordarr.length < 3)
			{
				action = false;
				if(isKeywordCheck)
				{
					xmlHttp2=GetXmlHttpObject();
					if (xmlHttp2==null)
					{
						alert ("Your browser does not support AJAX!");
						return;
					}
					var url = "save_edit.php?";
					var params = "imgid="+imgid+"&onHold=1";
					params = params + "&action=holdImage";
					params=params+"&sid="+Math.random();
					url = url+params;
					xmlHttp2.onreadystatechange=function()
					{
						document.getElementById("errorcaption_"+imgid).innerHTML=errMsg[3];					
						document.getElementById('submitStatus_'+imgid).checked=false;
						document.getElementById('submitStatus_'+imgid).title='image not ready for submission';	
					};
					xmlHttp2.open("GET",url,true);
					xmlHttp2.send(null);			
					check_fail = errMsg[2];
				}
			}
			else
			{
				document.getElementById(keyword_textareaid).style.backgroundColor = "#FFFFFF";
				if(captionArr.length >= 1 && captionArr != "")
				{
					xmlHttp2=GetXmlHttpObject();
					if (xmlHttp2==null)
					{
						alert ("Your browser does not support AJAX!");
						return;
					}
					var url = "save_edit.php?";
					var params = "imgid="+imgid+"&onHold=0";
					params = params + "&action=holdImage";
					params=params+"&sid="+Math.random();
					url = url+params;
					xmlHttp2.onreadystatechange=function()
						{
							document.getElementById('submitStatus_'+imgid).checked=true;
							document.getElementById('submitStatus_'+imgid).title='image ready for submission';
							document.getElementById("errorcaption_"+imgid).innerHTML='image ready for submission';
						};
					xmlHttp2.open("GET",url,true);
					xmlHttp2.send(null);
							
				}
			}
		}
	
		if(focustext != "")
		{
		focustext.focus();
		}
		//document.getElementById(keyword_textareaid).value = action+"|"+check_fail;
		return action+"|"+check_fail;
	}
	
	
function addNew(imgid,action,errorMsg)
{
	var errorMsgArr = errorMsg.split("|");
	getImageStatus(imgid);
	var oldselect = document.getElementById(olddataid);
	var newvalue = document.getElementById(textaddid).value;
	var imagestatus = document.getElementById(imagestatusid).value;
	//alert("id of img ststua in add new:"+imagestatusid);
	status = false;
	// check whether exist or not
	if(newvalue == "")
	{
		document.getElementById(managekeywordid).innerHTML=errorMsgArr[0];
		document.getElementById(textaddid).focus();
		return;
	}
	else if(keywordExist(imgid,newvalue))
	{
		// when keyword exists
		document.getElementById(managekeywordid).innerHTML=errorMsgArr[1];
		document.getElementById(textaddid).focus();
		return;
	}
	else
	{
		document.getElementById(managekeywordid).innerHTML="";
		// when keyword does not exists
		divid = "showimage1_"+imgid;
		var copyright = trim(document.getElementById(copyrightid).value);
		var caption = trim(document.getElementById(captionid).value);
		oldselect = document.getElementById(olddataid);
		primaryselectdata = document.getElementById(primarydataid);
		secondaryselectdata = document.getElementById(secondarydataid);
		// get the total keywords
		var tot =oldselect.length+ primaryselectdata.length+secondaryselectdata.length;
		//alert("total while adding:::"+tot);
		if((caption != "")	&& (copyright !="") && (tot >= 2))
		{
			status=true;
		}
		
			params = "imgid="+imgid+"&newvalue="+newvalue+"&action="+action+"&status="+status+"&imagestatus="+imagestatus;
			ajaxpage('save_edit.php?'+params,divid,false);
			var selectlen = oldselect.length++;
			oldselect.options[selectlen].text = newvalue;
			oldselect.options[selectlen].value = newvalue;
			document.getElementById(textaddid).value = '';
			document.getElementById(textaddid).focus();
	}
		//alert("status after adding::"+status);
}

function keywordExist(imgid,newvalue,action)
{
	var isExist = false;
	getElementId(imgid);
	oldselect = document.getElementById(olddataid);
	primaryselectdata = document.getElementById(primarydataid);
	secondaryselectdata = document.getElementById(secondarydataid);

	//looping through olsselectto find the keyword        
    for (var i = 0; i < oldselect.length; i++)
   {
      if (trim(oldselect.options[i].text.toLowerCase()) == trim(newvalue.toLowerCase()))
			isExist = true;
	} 
	
	    for (var i = 0; i < primaryselectdata.length; i++)
   {
      if (trim(primaryselectdata.options[i].text.toLowerCase()) == trim(newvalue.toLowerCase()))
			isExist = true;
	}
	
	    for (var i = 0; i < secondaryselectdata.length; i++)
   {
      if (trim(secondaryselectdata.options[i].text.toLowerCase()) == trim(newvalue.toLowerCase()))
			isExist = true;
	} 
	return isExist;
}
function expand(imgid)
{
	//ajaxpage('index.php?page=editkeyword','maincontent',false);
	getElementId(imgid);
	document.getElementById(divid).style.display='none';
	document.getElementById(keyworddivid).style.display="block";
	fillKeywords(imgid);
}

function CkAllNone(obj,id){
	//var tbs = document.getElementById(id).getElementsByTagName('INPUT');
	var tbs = document.getElementsByTagName('INPUT');
	for (var zxc0 = 0;zxc0<tbs.length;zxc0++)
	{
		if(!tbs[zxc0].disabled){
		tbs[zxc0].checked = obj.checked;
		}
	}
}

var imageId = new Array();

function submitImages(containerid,type)	
{
	var selectedImages="";
	
	if(type == "")
	{
		var checkboxes = document.getElementsByName('chk_submitimage');
		var checkboxvalue = new Array();
		var cnt = 0;
		for(i=0;i<checkboxes.length;i++)
		{
			if(checkboxes[i].checked == true){
				checkboxvalue[cnt] = checkboxes[i].value;
				cnt++;
			}
		}

		document.getElementById('hid_selectedimagestr').value = checkboxvalue;
	}
	else{
		document.getElementById('hid_selectedimagestr').value = document.getElementById('imagestr').value;
		}
	
	if((cnt == 0) && (type == "")) 
	{
	document.getElementById('showresponse').innerHTML = "<strong>Please select the image to submit</strong>";	
	return false;
	}
	else{
		//alert("selected images::"+document.getElementById('hid_selectedimagestr').value);
		document.submitimages.submit();
	
	}
}

function deleteMessage(containerid)
{
		var params="";
		var checkboxes = document.getElementById(containerid).getElementsByTagName('INPUT');
		var checkboxvalue = new Array();
		var cnt = 0;
		var mailtype = document.getElementById('mailtype').value;
		
		for(i=0;i<checkboxes.length-2;i++)
		{
			if(checkboxes[i].checked == true){
				checkboxvalue[cnt] = checkboxes[i].value;
				cnt++;
		}
		}
		if(cnt == 0){
		document.getElementById('msgsend').innerHTML = "<strong><font color='#FF0000'>Please select message to delete.</font></strong>";
		}
		else{
			var res = confirm('Are you sure?');
    		if(res==true)
    		{
				params="msgid="+checkboxvalue+"&mailtype="+mailtype;
				document.getElementById('msgsend').innerHTML = "<strong><font color='#FF0000'>Deleting ...</font></strong>";
				ajaxpage('scripts/micro/messages/message_delete.php?'+params,'msgsend',false);
			}
		}
}

function messageReply()
{
	var uname = document.getElementById('UserFirstName').value;
	var subject = document.getElementById('subject').value;
	var email = document.getElementById('email').value;
	var userid = document.getElementById('userid').value;
	var senderid = document.getElementById('senderid').value;
	var message= document.getElementById('message').value;
	
	var params="";
	document.getElementById('emptymessage').innerHTML = "";
	if(message == "")
	{
		document.getElementById('emptymessage').innerHTML = "Please enter the message.";
	}
	else{
		params="uname="+uname+"&subject="+subject+"&email="+email+"&userid="+userid+"&senderid="+senderid+"&message="+message;
//		document.getElementById('sending').style.display = 'block';
		ajaxpage('scripts/micro/messages/message_send.php?'+params,'msgsend',false);
		redirect('1');
	}
}

//Function to add keyword on keypress
function showcharcode(e,imgid,actiontyp,errorMsg){
	if(window.event)
    {
		 key = window.event.keyCode;     //IE
	}
    else
	{
    	key = e.which; 					//Mozilla
	}	
	if(key == 13)
	{
	    if(actiontyp == 'add')
		{
			addNew(imgid,actiontyp,errorMsg);
		}
		else if(actiontyp == 'edit')
		{
			saveKeyword(imgid,actiontyp,errorMsg)
		}
	}
}

// this function will be called on blur of each control;
function errorDiv(obj,divID)
{
	if((trim(obj.value) != '') && (obj.value != 0)){
		document.getElementById(divID).innerHTML='';
	}	
}

