var tempPageNo="";

var tdId;
var formField
var divId;
var verifyResult;
var verifyFlag=false;


function verify(form) {	
	formField = form;
	//alert( "inside  varify method ");
	imageText = formField.verificationCode.value;
	
	var url = "/verifyImage.do?ImageText=" + imageText;
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	//alert(req.readyState);
	req.open("POST", url, true);
	//alert(req.readyState);
	req.onreadystatechange = getArticles12;
	req.send(null);	
	return verifyFlag;
 }
 
function getArticles12() {
	//alert( "inside  varify method ");
	if (req.readyState == 4) {
		
		if (req.status == 200) {
			//alert(req.responseText);
			var text = req.responseText;
			//alert("aaa"+text+"aaa");
			//alert('text ------- '+text);
			
			if(text=='true'){
				verifyFlag=true;
				formField.submit();
			}
			else{
			alert("Please enter correct Verification code");
			 formField.verificationCode.focus();
			// document.photoBean.verificationCode.focus();
			// alert('vvvvvvvv');
			 return false;
			}
		}
	}
}


function getMostPopularPhoto(pageNo) {
  tempPageNo=pageNo;
	var url = "/mphoto.do?sub=MostPopularPhotoAjax&pageNo="+pageNo;
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	req.open("POST", url, true);
	req.onreadystatechange = getPOPPhoto;
	req.send(null);	
	
	
 }
 function getPOPPhoto() {
	if (req.readyState == 4) {
		if (req.status == 200) {
	
			
			populatePopularPhoto()
			
		}
	}
}

 function populatePopularPhoto() {
     
		var div = document.getElementById('pop');
         var div1 = document.getElementById('title');
         var div2=document.getElementById('paging');
       
        
        
       
       
        var MostPopularPhoto =  req.responseXML.getElementsByTagName("mostpops")[0];
		var mostPops=MostPopularPhoto.childNodes[0];
		var imageID=(mostPops.getElementsByTagName("imageID")[0]).firstChild.nodeValue;
		var imageName=(mostPops.getElementsByTagName("imageName")[0]).firstChild.nodeValue;
	    var title = (mostPops.getElementsByTagName("title")[0]).firstChild.nodeValue;
	    //var title1= (mostPops.getElementsByTagName("title")[1]).firstChild.nodeValue;
	    var userName=(mostPops.getElementsByTagName("userName")[0]).firstChild.nodeValue;
	    var myurl=(mostPops.getElementsByTagName("myurl")[0]).firstChild.nodeValue;
	    var hits=(mostPops.getElementsByTagName("hits")[0]).firstChild.nodeValue;
	     
	   //alert("imageID-------"+imageID);
		 
		 var temp="";
         
            temp+= '<a href=/newPhotoLanding.jsp?imageID='+imageID+'>';
			temp+= '<img src="/upload/imageGallery/thumbImage/'+imageName+'" width="64" height="51" border="0" class="pic2"/></a>';
			temp+= '<a href="/newPhotoLanding.jsp?imageID='+imageID+'" class="small"><strong>'+title+'</strong></a><br />';
			temp+= '<span class="grey">Contributed by <a href="/cj/'+myurl+'">'+userName+'</a><br />';
			temp+= 'Views: '+hits+'</span>';
			temp+= '<div class="clear"></div>';
		 
		 
    var MostPopularPhoto1 =  req.responseXML.getElementsByTagName("mostpops")[0];
	var mostPops1=MostPopularPhoto1.childNodes[1];
    var title1= (mostPops1.getElementsByTagName("title")[0]).firstChild.nodeValue;
    
    var temp1="";
    
	if(tempPageNo < 5){
	temp1 +='<strong>Next:</strong><a class="small" href="#">'+title1+'</a>';
	}
   var temp2='';
   
   if(tempPageNo==1){
   		 temp2 +='<a href="javascript:getMostPopularPhoto(1)" style="background-color: #FDBD2A;">1</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(2)">2</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(3)">3</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(4)">4</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(5)">5</a>&nbsp;&nbsp'; 
   }else if(tempPageNo==2){
  
	     temp2 +='<a href="javascript:getMostPopularPhoto(1)" >1</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(2)" style="background-color: #FDBD2A;">2</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(3)">3</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(4)">4</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(5)">5</a>&nbsp;&nbsp'; 
   }else if(tempPageNo==3){
   
   temp2 +='<a href="javascript:getMostPopularPhoto(1)" >1</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(2)" >2</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(3)" style="background-color: #FDBD2A;">3</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(4)">4</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(5)">5</a>&nbsp;&nbsp'; 
   
   }else if(tempPageNo==4){
   
   temp2 +='<a href="javascript:getMostPopularPhoto(1)" >1</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(2)" >2</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(3)">3</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(4)" style="background-color: #FDBD2A;">4</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(5)">5</a>&nbsp;&nbsp'; 
   
   }else if(tempPageNo==5){
         temp2 +='<a href="javascript:getMostPopularPhoto(1)" >1</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(2)" >2</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(3)">3</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(4)">4</a>&nbsp;<font color="#FFFFFF">.</font>';
		 temp2 +='<a href="javascript:getMostPopularPhoto(5)" style="background-color: #FDBD2A;">5</a>&nbsp;&nbsp'; 
   
   }
   		  div.innerHTML = temp;
	      div2.innerHTML = temp2;
	      div1.innerHTML = temp1;
}



///// For SubCategory -----------------------------------------------




var categoryID="";

function getSubCategory(categoryID) {
	
  tempPageNo=categoryID;
	
	var url = "/mphoto.do?sub=SubCategoryPhotoTitleAjax&catID="+categoryID;
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	req.open("POST", url, true);
	req.onreadystatechange = getSubCategoryState;
	req.send(null);	
	
	
 }
 function getSubCategoryState() {
	if (req.readyState == 4) {
		if (req.status == 200) {
	
			
			populateSubCategory()
			
		}
	}
}

 function populateSubCategory() {
     
     
		var divaaa = document.getElementById('subcat1');
        var temp='';
         
        var SubCategories =  req.responseXML.getElementsByTagName("subCategory")[0];
        
        for(loop=0; loop<SubCategories.childNodes.length; loop++){
        
		var subCatName=SubCategories.childNodes[loop];
		//alert(subCatName);
		                                                  
		var description=(subCatName.getElementsByTagName("description")[0]).firstChild.nodeValue;
		var catID=(subCatName.getElementsByTagName("catID")[0]).firstChild.nodeValue;
		var subcatID=(subCatName.getElementsByTagName("subcatID")[0]).firstChild.nodeValue;
		  
		  
		  if(loop == (parseInt(SubCategories.childNodes.length)-1) )
		  temp+= '<a href=/photoSubCat.jsp?catID='+catID+'&subCatID='+subcatID+'>'+description+'</a> ' ;
		  else
		  temp+= '<a href=/photoSubCat.jsp?catID='+catID+'&subCatID='+subcatID+'>'+description+'</a> | ' ;
		  
		 
		 }
		//alert("categoryID--  "+catID);
		var temptop = getElementPosition(catID).left;
		//alert("on js" + temptop);
		var nextEle = getElementPosition(parseInt(catID)+1).left;
		//alert("nextEle=" + nextEle);
		divaaa.style.marginLeft="0px";
		
		if(nextEle == 0){
		
		divaaa.style.marginLeft=(temptop-200)+"px";
		}else{		
		divaaa.style.marginLeft=(temptop-90)+"px";
		}
		//alert("divaaa.style.marginLeft" + divaaa.style.marginLeft);
		
		//temp+='<span>Get Photo Alert</span>&nbsp;<a href="#"><img src="/newPhotos/images/rss.gif" width="16" height="29" border="0" align="absmiddle"/></a>'
 divaaa.innerHTML = temp;
		  
}

function removeSubCategory() {

 var div = document.getElementById('subcat1');
 div.innerHTML="";

}
  
  
 function removeSubCategoryList() {

 alert();
 var div = document.getElementById('subcat1');
 div.innerHTML="";
   
    
 
  }
   
 



function cellImg(idCell,myurl) {
  
  
  //var existingURL=document.getElementById('linkingURL').href;
  //var linkURL=myurl;
  var li='li';
  var img='cjImg';
  for(var i=0;i<3;i++)
  {
  if(idCell==i)
  document.getElementById(li+i).style.backgroundImage = 'url(/newPhotos/images/cjbuton.gif)';
  else
  document.getElementById(li+i).style.backgroundImage = 'url(/newPhotos/images/cjbut.gif)';
 }
  var imgSrc=  document.getElementById(img+idCell).value;
  document.getElementById('cjImg').src =imgSrc;
  document.getElementById('linkingURL').href='/cj/'+myurl;
}

var formName;

function saveComment(param, param1){
		//alert(param);
		formName = param;
		
		//alert(param1);
		//alert(document.getElementById(param1).value);
		var methodCall = document.getElementById(param1).value;
		
		var name = param.name.value;
		var email = param.email.value;
		var userID = param.userID.value;
		var comment = param.comment.value;
		var verificationCode = param.verificationCode.value;
		var imageID = param.imageID.value;
		
		if(name==""){
			alert("Please Enter your name");
			param.name.focus();
			return false;
		}
		if(email==""){
		alert("Please Enter your email");
		param.email.focus();
			return false;
		}
		if(notValidEmail(email)){
        alert( "Please enter correct email address");
		param.email.focus();
        return false;
    	}
		
		if(comment==""){
		alert("Please Enter your comment");
		param.comment.focus();
			return false;
		}
		if(verificationCode==""){
		alert("Please Enter Verification code");
		param.verificationCode.focus();
			return false;
		}
		
		var pcID = "";
		
		//alert(name+"||"+email+"||"+userID+"||"+comment+"||"+verificationCode+"||"+imageID);
		var url="";
		if(methodCall=="savecomment"){
		 url = "/mphoto.do?sub=setComments&name="+name+"&email="+email+"&userID="+userID+"&comment="+comment+"&verificationCode="+verificationCode+"&imageID="+imageID;
		 }
		 else if(methodCall=="replycomment"){
		 pcID = param.pcID.value;
		 url = "/mphoto.do?sub=setReplyComments&name="+name+"&pcID="+pcID+"&email="+email+"&userID="+userID+"&comment="+comment+"&verificationCode="+verificationCode+"&imageID="+imageID;
		 }else if(methodCall=="reportabuse"){
		  pcID = param.pcID.value;
		  //alert(pcID);
		 url = "/mphoto.do?sub=reportAbuse&name="+name+"&email="+email+"&userID="+userID+"&comment="+comment+"&verificationCode="+verificationCode+"&imageID="+imageID+"&pcID="+pcID;
		 }
		 
		// alert(url)
		var req = getAjexRequest();
		req.open("POST", url, true);
		//alert(req.readyState);
		req.onreadystatechange = responseSaveComment;
		req.send(null);
	}
	
	function responseSaveComment(){
		if (req.readyState == 4) {
			if (req.status == 200) {
				if(req.responseText=='true'){
					formName.name.value="";
					formName.email.value="";
					formName.userID.value="";
					formName.comment.value="";
					formName.verificationCode.value="";
					alert("your comment is posted");
					window.location.reload();
				}else{
					alert("Comment is not saved");
				}
			}
		}
	}
	
	
	
function saveVote(param){
		
		formName = param;
		var vote="";
		
		
		for (counter = 0; counter < document.voteBean1.vote.length; counter++)
        {
          if (document.voteBean1.vote[counter].checked){ 
         
         vote=document.voteBean1.vote[counter].value;
        }

     }
		
		var page = document.voteBean1.page.value;
		
		
		
		
		
		var url="";
		
		 url = "/vote.do?sub=SaveVote&vote="+vote+"&page="+page;
		
		var req = getAjexRequest();
		req.open("POST", url, true);
		//alert(req.readyState);
		req.onreadystatechange = responseSaveVote;
		req.send(null);
	}
	
	function saveVoteMum(){
		var url = "/vote.do?sub=SaveVote&vote=Yes&page=mumbai attack";
		var req = getAjexRequest();
		req.open("POST", url, true);
		req.send(null);
	}
	
	function responseSaveVote(){
	
		if (req.readyState == 4) {
			if (req.status == 200) {
			
			//alert(req.responseText);
				if(req.responseText=='true'){
					
					alert("your vote is saved");
					
				}else if(req.responseText=='false'){
					alert("You have already voted");
				}
			}
		}
	}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	function getAjexRequest(){
		if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return req;
}



function getPopularArticles(day,recordNo) {
   var url ;
    if(day==1)
 	 url = "/mphoto.do?sub=populateBestArticleAJAX&recordNo="+recordNo;
 	 else
 	 url = "/mphoto.do?sub=populateBestArticleAJAX&day="+day+"&recordNo="+recordNo;
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", url, true);
	req.onreadystatechange = getPopularArticle;
	req.send(null);	

	//currentStartPosition=start;
	//currentEndPosition=end;

 }
function getPopularArticle() {

	if (req.readyState == 4) {
		if (req.status == 200) {
			populatePopularArticles()
		}
	}
}
 function populatePopularArticles() {
 		//alert("populateRecentArticles");
     myTable = document.getElementById("content");
     myTable.innerHTML="";	
	 
	
	
	 var ReccentArticles =  req.responseXML.getElementsByTagName("PopularArticles")[0];
	 
	 var i = 0;
	 var temp = "<ul>";
	  for (loop = 0;loop<ReccentArticles.childNodes.length;  loop++)
	  {
	   var ReccentArticle = ReccentArticles.childNodes[loop];
	   var articleID = (ReccentArticles.getElementsByTagName("articleID")[loop]).firstChild.nodeValue;
	   var title = (ReccentArticles.getElementsByTagName("title")[loop]).firstChild.nodeValue;	
	
	  
	  temp += "<li>&#187;&nbsp;<a href='http://www.merinews.com/catFull.jsp?articleID="+articleID+"'>"+title+"</a>"+"</li>"; 
	  
	  }
	 
	 temp +="</ul>";
	 //alert(temp);
	myTable.innerHTML=temp;
}
function makeactive(tab) { 
document.getElementById("tab1").className = ""; 
document.getElementById("tab2").className = ""; 
document.getElementById("tab"+tab).className = "active";

}

function notValidEmail( str ){
    mailRE = new RegExp( );
    mailRE.compile( '^[\._a-z0-9-]+@[\.a-z0-9-]+[\.]{1}[a-z]{2,4}$', 'gi' );
    return !(mailRE.test( str ));
} 

function showPreview(){
		document.photoBean.sub.value="previewEcards";
		var senderName = document.photoBean.senderName.value;
		var senerEmail = document.photoBean.senerEmail.value;
		
		var recipientName = document.photoBean.recipientName.value;
		var recipientEmail = document.photoBean.recipientEmail.value;
		
		var cardHeader = document.photoBean.cardHeader.value;
		var cardMessage = document.photoBean.cardMessage.value;
		var cardFooter = document.photoBean.cardFooter.value;
		var title = document.photoBean.title.value;
		var bigImage = document.photoBean.bigImage.value;
		var imageID = document.photoBean.imageID.value;
		
		if(validateThis()){
		var url="/newPhotos/jsp/ecardPreview/ecardPreview.jsp?senderName="+senderName+"&senerEmail="+senerEmail+"&recipientName="+recipientName+"&recipientEmail="+recipientEmail+"&cardHeader="+cardHeader+"&cardMessage="+cardMessage+"&cardFooter="+cardFooter+"&title="+title+"&bigImage="+bigImage+"&imageID="+imageID;
		window.open(url, "mywin")
		}
	}
	
	function validateThis(){
	var senderName = document.photoBean.senderName.value;
		var senerEmail = document.photoBean.senerEmail.value;
		
		var recipientName = document.photoBean.recipientName.value;
		var recipientEmail = document.photoBean.recipientEmail.value;
		
		var cardHeader = document.photoBean.cardHeader.value;
		var cardMessage = document.photoBean.cardMessage.value;
		var cardFooter = document.photoBean.cardFooter.value;
		var title = document.photoBean.title.value;
		var bigImage = document.photoBean.bigImage.value;
		var imageID = document.photoBean.imageID.value;
		var verificationCode =  document.photoBean.verificationCode.value;
		
		
		
		
		if(trim(senderName)=="" || trim(senderName)=="Name"){
			alert("Enter sender name");
			document.photoBean.senderName.focus();
			return false;
		}
		
		if(senerEmail=="" || senerEmail=="Email"){
			alert("Enter sender email");
			document.photoBean.senerEmail.focus();
			return false;
		}
		if(notValidEmail(senerEmail)){
			alert( "Please enter correct email address");
			document.photoBean.senerEmail.focus();
	        return false;
		}
		
		if(trim(recipientName)=="" || trim(recipientName)=="recipientName"){
			alert("Enter recipient name");
			document.photoBean.recipientName.focus();
			return false;
		}
		
		if(recipientEmail=="" || recipientEmail=="Email"){
			alert("Enter recipient email");
			document.photoBean.recipientEmail.focus();
			return false;
		}
		if(notValidEmail(recipientEmail)){
			
			
			alert( "Please enter correct email address");
			document.photoBean.recipientEmail.focus();
	        return false;
		}
		/*if(cardHeader=="" || cardHeader=="Card Header"){
			alert("Enter card Header name");
			document.photoBean.cardHeader.focus();
			return false;
		}*/
		if(cardMessage=="" || cardMessage=="Message"){
			alert("Enter Message name");
			document.photoBean.cardMessage.focus();
			return false;
		}
		/*if(cardFooter=="" || cardFooter=="Card Footer"){
			alert("Enter Card Footer name");
			document.photoBean.cardFooter.focus();
			return false;
		}*/
		if(verificationCode=="" || verificationCode=="Enter Code"){
			
			alert("Enter verification Code");
			document.photoBean.verificationCode.focus();
			return false;
		}
		//alert(verify(document.photoBean));
		
		if(verify(document.photoBean)==false){
		return false;
		}
		  
		 
		return true;
	}
	function sendCard(){
	
		document.photoBean.sub.value="sendEcards";
		
		var senderName = document.photoBean.senderName.value;
		var senerEmail = document.photoBean.senerEmail.value;
		
		var recipientName = document.photoBean.recipientName.value;
		var recipientEmail = document.photoBean.recipientEmail.value;
		
		var cardHeader = document.photoBean.cardHeader.value;
		var cardMessage = document.photoBean.cardMessage.value;
		var cardFooter = document.photoBean.cardFooter.value;
		var title = document.photoBean.title.value;
		var bigImage = document.photoBean.bigImage.value;
		var imageID = document.photoBean.imageID.value;
		
		
		document.photoBean.greatingUrl.value="newPhotos/jsp/ecardPreview/ecardPreview.jsp?senderName="+senderName+"&senerEmail="+senerEmail+"&recipientName="+recipientName+"&recipientEmail="+recipientEmail+"&cardHeader="+cardHeader+"&cardMessage="+cardMessage+"&cardFooter="+cardFooter+"&title="+title+"&bigImage="+bigImage+"&imageID="+imageID;
		
	    //alert('validateThis -- '+validateThis());
		if(validateThis()){
		
		document.photoBean.submit();
		}
	}
	function cancel(){
	
	}
	
	function setValues(param, param1){
		//alert(param);
		//alert(param1.value);
		if(param==param1.value){
			param1.value="";
		}
	}
	

function getElementPosition(elemID){
var offsetTrail = document.getElementById(elemID);
//alert(offsetTrail);
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail){
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
//alert(offsetLeft);
return {left:offsetLeft,top:offsetTop};
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }
  
function voteRigts(param){



if(param=="voted"){

alert("You have already voted");


}



}
	
	
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
	
}	

