// JavaScript Document


function showEmailIdOnPageExecutive(id, elem){
	var MouseposLeft=($(elem).position().left);
	var MouseposTop=($(elem).position().top);
	var MouseposLeftMinus=MouseposLeft-120;
	var divId=id+"emailDiv";
var emailIdLimit=document.getElementById("emailIdLimit").value;
var downloadedEmailId=document.getElementById("downloadedEmailId").value;
var emailIdLimit=document.getElementById("emailIdLimit").value;
 		
 		if(emailIdLimit != null && typeof emailIdLimit != "undefined" && parseInt(emailIdLimit) != 0){
	if(parseInt(emailIdLimit) < parseInt(downloadedEmailId)){
	alert("You have exceeded Max Email Id View Count as Per Your Subscription Limit.Please contact your administrator for further details");
	}else{
	$.ajax({
				type:"post",
				url:"companydetails.do?param=getEmailIdView",
				data:{"categoryDetailId":id},
				dataType:"json",
				success:function (data) {
					//populate page with data
					var param = data.data;
					if(param!=null && typeof param != "undefined" && param != '-1')
					{
					var ehtm='';
						ehtm+='<div id="'+divId+'"  style="display: none;z-index:999999; cursor:default;overflow:hidden">';
						ehtm+='<table class="emailBg" border="0">';
						ehtm+='<tr>';
						ehtm+='<td align="right" class="emailBgImg" width="162">';
						ehtm+='</td>';
						ehtm+='<td  width="28" align="right" style="border:none" title="Close" class="emailBgcross" onclick=javascript:showHideEmailDivS("0","'+divId+'");>';
						ehtm+='</td>';
						ehtm+='</tr>';
						ehtm+='<tr >';
						ehtm+='<td align="center" class="emailFont" colspan="2">'+param+'</td>';
						ehtm+='</tr>';
						ehtm+='</table>';				
						ehtm+='</div>';
						document.getElementById("emailView").innerHTML=ehtm;
						$("#"+divId).show().css({"position":"absolute","left":MouseposLeftMinus,"top":MouseposTop+25+"px"});
						
						/*document.getElementById(id).style.left=xcrd1;			
						document.getEleme
						ntById(id).style.top=ycrd1;
						document.getElementById(id).style.position="absolute";
					    document.getElementById(id).style.display="";*/
					
					}else{
					
					alert("You have exceeded Max Email Id View Count as Per Your Subscription Limit.Please contact your administrator for further details");
					}
				},
			error:function () {
			
				}
			}); 
	}
	}else{
 		alert("You are not the authorised user for this action.Please contact your administrator for further details.")
 	}	

}

var _sListMailIds ='';

function clickURL(_href){
 window.open(_href);
}


function pageHeight()
{
var screenHeight=document.body.clientHeight;

if(screenHeight<565)
{
	screenHeight=565;
}
document.getElementById("bodyContainer").style.height=screenHeight-110+"px";
}


function showHideDetails(boxName,divName) 
{ 
if(boxName.checked == true)
{ 

document.getElementById(divName).style.display="";

} 
else 
{
document.getElementById(divName).style.display="none";
}

} 


function checkAllZone() {
	var inputs = document.getElementsByName('zoneList');
	
	for (var i = 0; i < inputs.length; i++){
		inputs[i].checked = true;
	}
}

function uncheckAllZone() {
	var inputs = document.getElementsByName('zoneList');
	
	for (var i = 0; i < inputs.length; i++){
		inputs[i].checked = false;
	}
}


function checkAllDirectory(per) {
	var inputs = document.getElementsByName('directoryList');
	for (var i = 0; i < inputs.length; i++){
		inputs[i].checked = true;
	}
	
	var inputs2 = document.getElementsByName('industryId');
	for (var i = 0; i < inputs2.length; i++){
		inputs2[i].checked = true;
	}
	
	
	
}

function uncheckAllDirectory() {
	var inputs = document.getElementsByName('directoryList');
	for (var i = 0; i < inputs.length; i++){
		inputs[i].checked = false;
	}
	
	var inputs2 = document.getElementsByName('industryId');
	for (var i = 0; i < inputs2.length; i++){
		inputs2[i].checked = false;
	}
}

function checkAllCategory() {
	var inputs = document.getElementsByName('categoryList');
	for (var i = 0; i < inputs.length; i++)
	{
		inputs[i].checked = true;
	}
}

function unCheckAllCategory() {
	var inputs = document.getElementsByName('categoryList');
	for (var i = 0; i < inputs.length; i++)
	{
		inputs[i].checked = false;
	}
}

function validateInputData(){
	var inputs = document.getElementsByName('directoryList');
	var selectValue=null;
	for (var i = 0; i < inputs.length; i++)
		{
			if(inputs[i].checked==true) {
				selectValue=true;
			} 
		}
	if(selectValue==true) {
			preloaderControl();
			return true;
		} else {
			alert("Please select at least one industry");
			return false;
		}
}
function validateCheckBox(frm) {
	var inputs = document.getElementsByName('directoryList');
	var selectValue=null;
	for (var i = 0; i < inputs.length; i++)
		{
			if(inputs[i].checked==true) {
				selectValue=true;
			} 
		}
	if(selectValue==true) {
			frm.submit();
		} else {
			alert("Please select at least one industry");
		}
}


function validateCategoryChkBox(cTypeName) {
	var chkBoxList = document.getElementsByName('categoryList');
	var showMsg;
	var categoryChkValue;
	var indutoryChkValue;
	for (var i = 0; i < chkBoxList.length; i++)
		{
			if(chkBoxList[i].checked==true) {
				categoryChkValue=true;	
			}
		}
	var inputs = document.getElementsByName('directoryList');
	
	for (var i = 0; i < inputs.length; i++)
		{
			if(inputs[i].checked==true) {
				indutoryChkValue=true;
			}
		}
		if(categoryChkValue!=true) {
			showMsg = "Please select at least one "+cTypeName+" type";
		} else if(indutoryChkValue!=true) {
			showMsg = "Please select at least one Industry";
		} 
		
		if(categoryChkValue==true && indutoryChkValue==true) {
			preloaderControl();
			document.getElementById('fSearchForm').submit();
		} else {
			alert(showMsg);
		}
}

function checkEmail(emailStr)
{
       var emailPat=/^(.+)@(.+)$/;
       var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
       var validChars="\[^\\s" + specialChars + "\]";
       var quotedUser="(\"[^\"]*\")";
       var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
       var atom=validChars + '+';
       var word="(" + atom + "|" + quotedUser + ")";
       var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
       var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
       var matchArray=emailStr.match(emailPat);
       if (matchArray == null) {
           return false;
       }
       var user=matchArray[1];
       var domain=matchArray[2];
       if (user.match(userPat) == null) {
           return false;
       }
       var IPArray = domain.match(ipDomainPat);
       if (IPArray != null) {
           for (var i = 1; i <= 4; i++) {
              if (IPArray[i] > 255) {
                 return false;
              }
           }
           return true;
       }
       var domainArray=domain.match(domainPat);
       if (domainArray == null) {
           return false;
       }
       var atomPat=new RegExp(atom,"g");
       var domArr=domain.match(atomPat);
       var len=domArr.length;
       if ((domArr[domArr.length-1].length < 2) ||
           (domArr[domArr.length-1].length > 3)) {
           return false;
       }
       if (len < 2) {
           return false;
       }
       return true;
}


function validateForm(frm) {

var name =  trim(document.getElementById('name').value);
var email =  trim(document.getElementById('email').value);
var phoneNo =  trim(document.getElementById('phoneNo').value);
var sourceInfo =  trim(document.getElementById('sourceInfoName').value);
var displayStr = "";

	if(name.length<1)
	{
		displayStr   =  displayStr + "Please enter Name.\n" ;
	}
	else{
		var iChars = "_!@#$%^&*()+=-[]\\\';,./{}|\":<>?1234567890";
		var flag = "false";
		for (var i = 0; i < name.length; i++) {  
			if (iChars.indexOf(name.charAt(i)) != -1) 
			{
				displayStr   =  displayStr + "Only alphabets are allowed for Name.\n"; 
				break;
			}               
		}
	}
		if(email.length<1)
	{
		displayStr   =  displayStr + "Please enter Email.\n" ;
	} else	{
		if(!checkEmail(email))
		 	displayStr = displayStr + "Email is not entered properly.\n";
	}
		

	if(phoneNo.length<1)
	{
		displayStr   =  displayStr + "Please enter Phone number.\n";
	}else if (checkInternationalPhone(phoneNo)==false){
		displayStr = displayStr + "Please Enter a Valid Phone Number";

	}
	if(sourceInfo == '-1')
	{
		displayStr   =  displayStr + "Please select hear about us option.\n" ;
		document.getElementById('sourceInfoName').focus();
	}
	if(displayStr != "") {
		alert(displayStr);
	} else {
		frm.submit();
	}
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){

	// Declaring required variables
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()- ";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = 10;
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function showCompanyNameDivInGeneric(e){
//alert("showCompanyNameDivInGeneric");	
	if(!e) e=window.event;
	var key = e.keyCode ? e.keyCode : e.which;
	if(key!=40 && key!=38 && key!=13 && key!=9){
	var companyName = document.getElementById("genericSearchCompanyName").value;
	
	var url = "search.do?param=getCompanyNameList&companyName="+companyName;
	sendAJAXCallInGeneric(url,null,"POST");
	}	
}

function sendAJAXCallInGeneric(url, parameter, method) {
	aReq = xmlhttpPost();
	aReq.onreadystatechange = acceptIncomingCallInGeneric;
	aReq.open(method, url, true);
	if(parameter == ""){
		aReq.send(null);
    }else{
		aReq.send(parameter);
    }
}

function xmlhttpPost() {
	if(window.XMLHttpRequest){
    	xmlHttpReq = new XMLHttpRequest();
 	}else if(window.ActiveXObject){
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	return xmlHttpReq;  
}

function acceptIncomingCallInGeneric(){
	if(aReq.readyState == 4) {
    	if(aReq.status == 200) {
        	parseDataRetrievedInGeneric(aReq.responseXML);
		} else {
	  		alert ( "Not able to retrieve description" );
        }
    }
    
    return;
}

function parseDataRetrievedInGeneric(xmlObject){
	var companyNameList = xmlObject.getElementsByTagName('name');
	
	var str = "";
	str = str + "<ul id='companyListSelect' style='width: 240px;margin:0px; padding:0px;' size=15>";
	for(i = 0;i<companyNameList.length;i++ ){
		if(isMSIE){ // IE
			str = str + "<li style='margin:0px; padding:0px;cursor:pointer;cursor:pointer;' onmouseover='javascript: mouseOverOnLiInGeneric("+i+");' onmouseout='mouseOutOnLiInGeneric("+i+");'  onclick='onClickOnLiInGeneric("+i+");' onmousedown='onClickOnLiInGeneric("+i+");' id='InGenericli_"+i+"'>"+companyNameList.item(i).text+"</li>";
		}else{ // FireFox
			str = str + "<li style='margin:0px; padding:0px;cursor:pointer;cursor:pointer;' onmouseover='javascript: mouseOverOnLiInGeneric("+i+");' onmouseout='mouseOutOnLiInGeneric("+i+");'  onclick='onClickOnLiInGeneric("+i+");' onmousedown='onClickOnLiInGeneric("+i+");' id='InGenericli_"+i+"'>"+companyNameList.item(i).textContent+"</li>";
		}
	}
	
	str = str + "</ul>";
	document.getElementById("CompanyNameDivInGeneric").innerHTML = str; 
	if(companyNameList.length > 0){
		document.getElementById("CompanyNameInGenericBg").style.display = "block";
		document.getElementById("CompanyNameDivInGeneric").style.display = "block";	
	}else{
		document.getElementById("CompanyNameDivInGeneric").style.display = "none";
		document.getElementById("CompanyNameInGenericBg").style.display = "none";
	}
}

var globalIndexInGeneric = -1;

function mouseOverOnLiInGeneric(index){
	if(document.getElementById("InGenericli_"+globalIndexInGeneric) != null){
		document.getElementById("InGenericli_"+globalIndexInGeneric).style.color = "black";
		document.getElementById("InGenericli_"+globalIndexInGeneric).style.backgroundColor = "";
	}
	
	globalIndexInGeneric = index;
	document.getElementById("InGenericli_"+index).style.color = "red";
	document.getElementById("InGenericli_"+index).style.backgroundColor = "#cccccc";
}

function mouseOutOnLiInGeneric(index){
	document.getElementById("InGenericli_"+index).style.color = "black";
	document.getElementById("InGenericli_"+index).style.backgroundColor = "";
}

function onClickOnLiInGeneric(index){
	replaceAmpNSetCompValInGeneric(document.getElementById("InGenericli_"+index).innerHTML);
	document.getElementById("CompanyNameDivInGeneric").style.display = "none";
	document.getElementById("CompanyNameInGenericBg").style.display = "none";
	globalIndexInGeneric = -1;
	//preloaderControl();
	//document.getElementById("quickSearchForm").action = "search.do?param=quickSearchResult";
	//document.getElementById("quickSearchForm").submit();
	document.getElementById("genericSearchCompanyName").focus();
}

function replaceAmpNSetCompValInGeneric(str){
	if(str.indexOf("&amp;") != -1){
		var temp = str.substring(0,str.indexOf("&amp;")+1) + "" + str.substring(str.indexOf("&amp;")+5) 
		replaceAmpNSetCompValInGeneric(temp);
	}else{
		document.getElementById("genericSearchCompanyName").value = str;
		return ;
	}
}

function onKeyDownInGeneric(e){
//alert("onKeyDownInGeneric");
if(!e) e=window.event;
var key = e.keyCode ? e.keyCode : e.which;
if(key==40 || key==38 || key==13 || key==9){
	if(key == 9){
		if(globalIndexInGeneric != -1){
			replaceAmpNSetCompValInGeneric(document.getElementById("InGenericli_"+globalIndexInGeneric).innerHTML);
		}
		
		document.getElementById("CompanyNameDivInGeneric").style.display = "none";
		document.getElementById("CompanyNameInGenericBg").style.display = "none";
		globalIndexInGeneric = -1;
	}if(key == 13){
		if(globalIndexInGeneric != -1 && document.getElementById("InGenericli_"+globalIndexInGeneric) != null){
			replaceAmpNSetCompValInGeneric(document.getElementById("InGenericli_"+globalIndexInGeneric).innerHTML);
			document.getElementById("CompanyNameDivInGeneric").style.display = "none";
			document.getElementById("CompanyNameInGenericBg").style.display = "none";
			globalIndexInGeneric = -1;
			//preloaderControl();
			//document.getElementById("quickSearchForm").action = "search.do?param=quickSearchResult";
			//document.getElementById("quickSearchForm").submit();	
		}
	}else{
		if(key==40){
			if(document.getElementById("InGenericli_"+globalIndexInGeneric) != null){
				document.getElementById("InGenericli_"+globalIndexInGeneric).style.color = "black";
				document.getElementById("InGenericli_"+globalIndexInGeneric).style.backgroundColor = "";
			}
			globalIndexInGeneric = globalIndexInGeneric + 1;
		}else if(key==38){
			if(document.getElementById("InGenericli_"+globalIndexInGeneric) != null){
				document.getElementById("InGenericli_"+globalIndexInGeneric).style.color = "black";
				document.getElementById("InGenericli_"+globalIndexInGeneric).style.backgroundColor = "";
			} 
			
			globalIndexInGeneric = globalIndexInGeneric - 1;			
		}
	
		if(document.getElementById("InGenericli_"+globalIndexInGeneric) != null){
			document.getElementById("InGenericli_"+globalIndexInGeneric).style.color = "red";
			document.getElementById("InGenericli_"+globalIndexInGeneric).style.backgroundColor = "#cccccc";
		}else{
			globalIndexInGeneric = 0;
			document.getElementById("InGenericli_"+globalIndexInGeneric).style.color = "red";
			document.getElementById("InGenericli_"+globalIndexInGeneric).style.backgroundColor = "#cccccc";
		}
	}
	}
}


function hideCompanyNameDivInGeneric(e){
//alert("hideCompanyNameDivInGeneric");
	if(!e) e=window.event;
	var positionX = e.clientX;
	var positionY = e.clientY;
	//var positionX = event.clientX;
	//var positionY = event.clientY;
	if(!((positionX > 340 && positionX < 610) && (positionY > 220 && positionY < 490))){
		document.getElementById("CompanyNameDivInGeneric").style.display = "none";
		document.getElementById("CompanyNameInGenericBg").style.display = "none";
		globalIndexInGeneric = -1;
	}else{
		document.getElementById("genericSearchCompanyName").focus();
	}
}

function viewDetails(execId,compId){
//document.getElementById("quickSearchForm").action = "companydetails.do?param=viewExecDetails&companyId="+compId+"&categoryId="+execId;
//document.getElementById("quickSearchForm").submit();

}
function sendMail(){
var html="";
var selectedId=globalIdForCheckBox;
var sentId;
if(selectedId === ''){
		alert("Please Select at least one Executive ");
		return false;
	}else{
$.blockUI({ 
				message:$("#sendMail"),
				css:{
						cursor:"default",
						border:"none",
						width:"auto",
						height:"auto",
						backgroundColor:"transparent",
						opacity:"0.8",
						top:"16%",
						color:"#000",
						left:"25%"
					},
				overlayCSS:{
					backgroundColor:"#000",
					opacity:"0.7",
					cursor: 'auto'
					},
				showOverlay:true,
				focusInput:true,
				allowBodyStretch: false,
				applyPlatformOpacityRules:false,
				centerY: true,
				centerX: true
			});
			}
$.ajax({
				type:"post",
				url:"mailPost.do?param=getAjaxCountForMailPost",
				data:{"selectedIds":selectedId},
				dataType:"json",
				success:function (data) {
				 _sListMailIds = data.selectedId;
				
				html+="<div>"
				if(data.emailCount == null || typeof data.emailCount == 'undefined'  || data.emailCount < 1){
							html+=" &nbsp; &nbsp; &nbsp;  &nbsp;   No Executive contains emailId under your selection";
						document.getElementById("IconOfMail").style.dispaly="none";
				}/*else if(data.emailCount > 50){
				 		html+=" &nbsp; &nbsp; &nbsp;  &nbsp;  Please select less than 50 mail ids for single mail campaign.<br/> You have selected "+data.emailCount+" executive for Mail Post. ";
					document.getElementById("IconOfMail").style.dispaly="none";
				}*/else{
						html+=" &nbsp; &nbsp; &nbsp;  &nbsp;   You have selected "+data.emailCount+" executive for Mail Post.";
					document.getElementById("IconOfMail").style.dispaly="";
				}
				html+="</div>";
				document.getElementById("dataOfMail").innerHTML=html;	
			}
		, error:function (ex) {
				//alert(ex);
			}
		});


	
	
}



