var app_key_id="001";
var version_number=1;
var errMsg="";
var start=10;
var end=10;
var startDate;
var endDate;
var passwordMD5="";
var staticURL="";
var searchStartDate="";
var endStartDate="";
/** JCapcha Capturing **/
/*function getJcaptcha()
{  
    $.ajax(        
    {    
    type: "GET", url: "../public/captcha.htm?date="+new Date() ,dataType: "text", success: function (responseText)
    { 	
        var imageId=parseInt(responseText);   	
        document.getElementById('jcaptchaImage').src="../../../jcaptcha/image.htm?appKeyId=001&versionNo=1&imageId="+imageId;
        document.getElementById('hiddenCaptchaId').value=imageId;   
        document.getElementById('jcaptchachars').value="";
    }
    });   
}*/
function getJcaptcha()
{  
  // staticURL=jCapchaUrl+"?appKeyId="+app_key_id+"&versionNo="+version_number+"&imageId=";
    $.ajax(        
    {    
    type: "GET", url: "../public/captcha.htm?date="+new Date()+"&_1="+Math.random() ,dataType: "text", success: function (responseText)
    { 	
        var imageId=parseInt(responseText);   	
       // document.getElementById('jcaptchaImage').src="../../../jcaptcha/image.htm?appKeyId=001&versionNo=1&imageId="+imageId;
	    document.getElementById('jcaptchaImage').src=jCapchaUrl+"?appKeyId="+app_key_id+"&versionNo="+version_number+"&imageId="+imageId;
        document.getElementById('hiddenCaptchaId').value=imageId;   
        document.getElementById('jcaptchachars').value="";
    }
    });   
}
         

/**Get StateList by countryCode**/
function getStatesList(){
	var countryCode=document.getElementById('countryCode').value;
	$.ajax(
		   {
			   type: "GET", url: "../public/register.htm?mode=getStateList&countryCode="+countryCode+"&appKeyId=001&versionNo=1"+"&_1="+Math.random(),dataType: "text", success: function (responseText)
			   {
                                $("#stateItem").html(responseText);
			   }
		   });
	}
         

/**Get cityList by stateCode**/
function getCityList(){
	var stateCode=document.getElementById('stateCode').value;
         var countryCode=document.getElementById('countryCode').value;
	$.ajax(
		   {
			   type: "GET", url: "../public/register.htm?mode=getCityList&stateCode="+stateCode+"&countryCode="+countryCode+"&appKeyId=001&versionNo=1"+"&_1="+Math.random(),dataType: "text", success: function (responseText)
			   {
				   $("#cityItem").html(responseText);
			   }
		   });
}	


/**Update User Profile **/
function updateProfile(password){	
 	var status= false;
	if(document.getElementById('firstName').value!=document.getElementById('oldFirstName').value){
		status=true;
	}
	if(document.getElementById('lastName').value!=document.getElementById('oldLastName').value){
		status=true;
	}
	if(document.getElementById('streetAddress').value!=document.getElementById('oldStreetAddress').value){
		status=true;
	}
	if(document.getElementById('countryCode').value!=document.getElementById('oldCountryCode').value){
		status=true;
	}
	if(document.getElementById('stateCode').value!=document.getElementById('oldStateCode').value){
		status=true;
	}
	if(document.getElementById('cityCode').value!=document.getElementById('oldCityCode').value){
		status=true;
	}
	if(document.getElementById('postalCode').value!=document.getElementById('oldPostalCode').value){
		status=true;
	}
	if(document.getElementById('phoneNumber').value!=document.getElementById('oldPhoneNumber').value){
		status=true;
	}
	if(document.getElementById('oldEmailAddress').value!=document.getElementById('emailId').value){
		status=true;
	}
	if(document.getElementById('password').value!=""){
		status=true;
	}
	
	if(status){
		feedBack('prograssBar');
        var params='<user>'
        +'<partyId>'+partyId+'</partyId>'
        +'<partyTypeId>2</partyTypeId>'
        +'<isActive>true</isActive>'   
        +'<loginId>'+document.getElementById('loginId').value+'</loginId>'
        +'<password>'+password+'</password>'                   
        +'<person>'
        +'<DOB>1987-09-13</DOB>'
        +'<qualification></qualification>'
        +'<sex>M</sex>'
        +'</person>'
        +'<nameList>'                   
        +'<name type="3">'+document.getElementById('firstName').value+'</name>'
        +'<name type="5">'+document.getElementById('lastName').value+'</name>'
        +'</nameList>'
        +'<relationShipList>'
        +'<relationShip xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="relationShip">'
        +'<relationShipStatusId>1</relationShipStatusId>'
        +'<relationShipTypeId>2</relationShipTypeId>'
        +'<toParty>2</toParty>'                   
        +'</relationShip>'
        +'<relationShip xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="relationShip">'
        +'<relationShipStatusId>1</relationShipStatusId>'
        +'<relationShipTypeId>4</relationShipTypeId>'
        +'<toParty>2</toParty>'                   
        +'</relationShip>'
        +'</relationShipList>'     
        +'<contactsList>'                   
        +'<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="phoneNumber">'            
        +'<contactType>1</contactType>'
        +'<phoneNo>'+document.getElementById('phoneNumber').value+'</phoneNo>'
        +'</contact>'  
        +'<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="emailAddress">'            
        +'<contactType>2</contactType>'
        +'<emailId>'+document.getElementById('emailId').value+'</emailId>'
        +'</contact>'  
        +'<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="postalAddress">'                   
        +'<contactType>4</contactType>'
        +'<streetAddress>'+document.getElementById('streetAddress').value+'</streetAddress>'
        +'<country>'
        +'<countryCode>'+document.getElementById('countryCode').value+'</countryCode>'
        +'</country>'
        +'<state>'
        +'<stateCode>'+document.getElementById('stateCode').value+'</stateCode>'
        +'</state>'
        +'<postal>'
        +'<postalCode>'+document.getElementById('postalCode').value+'</postalCode>'
        +'</postal>'
        +'<city>'
        +'<cityCode>'+document.getElementById('cityCode').value+'</cityCode>'
        +'</city>'
        +'</contact>'
        +'</contactsList>'
        +'<appKeyId>'+app_key_id+'</appKeyId>'
        +'<versionNo>'+version_number+'</versionNo>'
        +'</user>';                 
        $.ajax(
            {
                type: "GET", url: "../secure/edit-profile.htm?mode=update&param="+params+"&password="+document.getElementById('password').value+"&_1="+Math.random(),dataType: "text", success: function (responseText)
                {
					TB_remove();
                    alert("Updated Successfully");
                    window.location.href="../secure/account-summary.htm";
                }
            });
	}
	else {
		 window.location.href="../secure/account-summary.htm";
	}
} 

/*Check MD5 */
function checkJcaptcha(){
		   checkMD5(document.getElementById('jcaptchachars').value);       
		   var reqTxt= '<jCaptcha>'                   
                   +'<jcaptchaId>'+document.getElementById('hiddenCaptchaId').value+'</jcaptchaId>'                   
                   +'<captchaWord>'+md5+'</captchaWord>'
                   +'<appKeyId>'+app_key_id+'</appKeyId>'
                   +'<versionNo>'+version_number+'</versionNo>'
                   +'</jCaptcha>';                 
				   urls="../public/register.htm?mode=jcapValid&data="+reqTxt;               
                 $.ajax({
            		type:"GET", url:urls+"&_1="+Math.random(),dataType:"textvalue",cache: false,success: function(response){  
                		if(response=="success"){     	    	     
        	             checkUserAlreadyExist();                         
	                    }else{
          				document.getElementById("reg_error_main").style.visibility = "visible";
						document.getElementById("reg_error_main").style.display = "block";
						document.getElementById("reg_error_main").innerHTML = "Invalid Image Code";
            		    }
				   	 },error : function(XMLHttpRequest,textStatus,errorThrown){
				       alert("Not Loaded Properly")
     				 }				
         		 });  
}

/*Check user Id Already Exists*/
function checkUserAlreadyExist(){
var loginId=document.getElementById('loginId').value;
$.ajax(
    {
    type: "GET", url: "../public/register.htm?mode=loginExist&loginId="+loginId+"&_1="+Math.random(),dataType: "text", success: function (responseText)
    {
        if(trim(responseText)=="Available"){
            checkMailIdExist();
        }else{            
		    document.getElementById("reg_error_main").style.visibility = "visible";
			document.getElementById("reg_error_main").style.display = "block";
			document.getElementById("reg_error_main").innerHTML = "Sorry , the same login Id already exists .Please try some other Login Id";
            getJcaptcha();
        }
    }
});
}



/** checkMailIdExist **/
function checkMailIdExist(){
    var email=document.getElementById('emailId').value;
    $.ajax(
    {
    type: "GET", url: "../public/register.htm?mode=mailIdCheck&emailId="+email+"&_1="+Math.random(),dataType: "text", success: function (responseText)
    {
    if(responseText.length>2){     
            if(trim(responseText)=="ok"){				
                createUser();
            }else{ 
				document.getElementById("reg_error_main").style.visibility = "visible";
				document.getElementById("reg_error_main").style.display = "block";
				document.getElementById("reg_error_main").innerHTML = "Sorry , The same email id already exists.Please use another one.";
                getJcaptcha();
            }
        }
    }});
}

/**Create User**/
function createUser(){
	feedBack('prograssBar');
    var params='<user><partyTypeId>2</partyTypeId><isActive>true</isActive>'
                   +'<loginId>'+document.getElementById('loginId').value+'</loginId>'
                   +'<password>'+checkMD5(document.getElementById('confirmPassword').value)+'</password>'
                   +'<person>'
                   +'<DOB>1987-09-13</DOB>'
                   +'<qualification></qualification>'
                   +'<sex>M</sex>'
                   +'</person>'
                   +'<nameList>'                   
                   +'<name type="3">'+document.getElementById('firstName').value+'</name>'
                   +'<name type="5">'+document.getElementById('lastName').value+'</name>'
                   +'</nameList>'
                   +'<relationShipList>'
                   +'<relationShip xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="relationShip">'
                   +'<relationShipStatusId>7</relationShipStatusId>'
                   +'<relationShipTypeId>4</relationShipTypeId>'
                   +'<fromParty>2</fromParty>'                   
                   +'</relationShip>'                   
                   +'</relationShipList>'     
                   +'<contactsList>'                   
                   +'<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="phoneNumber">'            
                   +'<contactType>1</contactType>'
                   +'<phoneNo>'+document.getElementById('phoneNumber').value+'</phoneNo>'
                   +'</contact>'  
                   +'<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="emailAddress">'            
                   +'<contactType>2</contactType>'
                   +'<emailId>'+document.getElementById('emailId').value+'</emailId>'
                   +'</contact>'  
                   +'<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="postalAddress">'                   
                   +'<contactType>4</contactType>'
                   +'<streetAddress>'+document.getElementById('streetAddress').value+'</streetAddress>'
                   +'<country>'
                   +'<countryCode>'+document.getElementById('countryCode').value+'</countryCode>'
                   +'</country>'
                   +'<state>'
                   +'<stateCode>'+document.getElementById('stateCode').value+'</stateCode>'
                   +'</state>'
                   +'<postal>'
                   +'<postalCode>'+document.getElementById('postalCode').value+'</postalCode>'
                   +'</postal>'
                   +'<city>'
                   +'<cityCode>'+document.getElementById('cityCode').value+'</cityCode>'
                   +'</city>'
                   +'</contact>'
                   +'</contactsList>'
                   +'<appKeyId>'+app_key_id+'</appKeyId>'
                   +'<versionNo>'+version_number+'</versionNo>'
                   +'</user>';   
                   $.post("../public/register.htm", "mode=register&password="+document.getElementById('confirmPassword').value+"&param="+params+"&_1="+Math.random(),function(data){
					 TB_remove();
                     alert("Successfully Registered");
                     window.location.href="../public/login.htm";
                   }, "text");		
}

/*Feed Back Send Mail*/
function sendMessage(){
    var yourName=document.getElementById("yourName").value;
    var yourComment=document.getElementById("yourComment").value;
    if(yourName!="" && yourComment!=""){
      $.ajax(
          {
              type: "GET", url: httpUrl+"/public/communication-home.htm?type=sendFeedBack&name="+yourName+"&comment="+yourComment+"&_1="+Math.random(),dataType: "text", success: function (responseText)
              {
                  alert("Thank you for your feedback. Please stay tuned for more services from NRIneeds.");                  
                  TB_remove();
              }
          });  
    }else{
                alert("Enter the required fields to send");
    }  
}

/*Order History Search*/
/*function orderSearch(){
    if(document.getElementById('orderCounter').value==0){
	alert("No Records Available");	
	}else{
            var enddate=document.getElementById('endDate').value.split("-");
            var startdate=document.getElementById('startDate').value.split("-");
            var newendDate=new Date(enddate[0]+'/'+enddate[1]+'/'+enddate[2]);
            var newstartDate=new Date(startdate[0]+'/'+startdate[1]+'/'+startdate[2]);
            var today = new Date();
            if(document.getElementById('startDate').value==""){
                errMsg=errMsg+"Please enter start date\n"
            }
            else if(document.getElementById('endDate').value==""){
                errMsg=errMsg+"Please enter end date\n"
            }
            else if(document.getElementById('endDate').value!="" && document.getElementById('startDate').value!=""){
                if(newstartDate>newendDate){
                    errMsg=errMsg+"Start date should be less than end date\n"
                }
                else if(newendDate>today){
                    errMsg=errMsg+"End date should be less than or equal to today\n";	
                }
            }
                         
            if(errMsg!=""){
                alert(errMsg);
                errMsg="";
            }
            else {
                $.ajax(
                      {
                          type: "GET", url: "../secure/order-history.htm?mode=search&sdate="+document.getElementById('startDate').value+"&cdate="+document.getElementById('endDate').value,dataType: "text", success: function (responseText)
                          {                  
							  $("#orderhistory").html(responseText);
                               var pager = new Pager('orderhistorys', 10);
                                pager.init();
                                pager.showPageNav('pager', 'orderHistoryNavPosition');
                                pager.showPage(1);
                          }
                      }); 
            }
                
	}   

}*/

/*Usage History Search*/
function usageHistorySearch(){    
	    var enddate=document.getElementById('enddate').value.split("-");
            var startdate=document.getElementById('start_date').value.split("-");
            var newendDate=new Date(enddate[0]+'/'+enddate[1]+'/'+enddate[2]);
            var newstartDate=new Date(startdate[0]+'/'+startdate[1]+'/'+startdate[2]);
            var today = new Date();
            if(document.getElementById('start_date').value==""){
                errMsg=errMsg+"Please enter start date\n"
            }
            else if(document.getElementById('enddate').value==""){
                errMsg=errMsg+"Please enter end date\n"
            }
            else if(document.getElementById('enddate').value!="" && document.getElementById('start_date').value!=""){             
                if(newstartDate>newendDate){
                    errMsg=errMsg+"Start date should be less than end date\n"
                }
                else if(newendDate>today){
                    errMsg=errMsg+"End date should be less than or equal to today\n";	
                }
            }
                         
            if(errMsg!=""){
                alert(errMsg);
                errMsg="";
            }
            else {
                endDate=document.getElementById('enddate').value;
                startDate=document.getElementById('start_date').value;
                searchStartDate=document.getElementById('start_date').value;
		endStartDate=document.getElementById('enddate').value;
                $.ajax(
                      {
                          type: "GET", url: "../secure/usage-history.htm?mode=search&sdate="+startDate+"&cdate="+endDate+"&_1="+Math.random(),dataType: "text", success: function (responseText)
                          {                 
				$("#usageTab").html(responseText);
                                 var count=parseInt($("#usageCount").val());
				 var position=parseInt($("#position").val());
				 hidePrevNext(count,position,20,'prevUsage','nextUsage');
                          }
                      }); 
            }                

}

/*Send Password*/
function sendPasswordRequest(){
	$.ajax(
          {
              type: "GET", url: "../public/forgot-password.htm?mode=reqpass&loginReq="+document.getElementById('loginId').value+"&date="+new Date()+"&_1="+Math.random(),dataType: "text", success: function (responseText)
              {
			  alert(responseText);
              }
          }); 
	}

/** Trim String **/
function trim(str){
return str.replace(/^\s*|\s*$/g,"");
}

/*Send Contact Us*/
function sendContact(){
valContact();
			if(iFlag=="0"){			
			document.getElementById("contact_error_main").style.visibility = "visible";
			document.getElementById("contact_error_main").style.display = "block";
			document.getElementById("contact_error_main").innerHTML = "Please correct all the errors. Errors are mentioned below respective fields";
			iFlag=1;			
			}else{
			$.ajax(
					  {
						  type: "GET", url: "../public/contact-us.htm?mode=contact&name="+document.getElementById('name').value+"&cname="+document.getElementById('companyname').value+"&email="+document.getElementById('email').value+"&comments="+escape(document.getElementById('comments').value)+"&_1="+Math.random(),dataType: "text", success: function (responseText)
						  {
							/*document.getElementById("contact_error_main").style.visibility = "visible";
							document.getElementById("contact_error_main").style.display = "block";
							document.getElementById("contact_error_main").innerHTML = "Thanks Being Part of Us";*/
							alert("Thanks for being part of us");
							window.location.href="../public/nrineeds-home.htm";
						  }
					  }); 
			}	
		    
}
/*Validation Part for Contact Us*/
function valContact(){
	    document.getElementById("contact_error_main").style.display = "none";
		document.getElementById("contact_error_main").innerHTML ="";
		/*Validation for Name*/
		if ((document.getElementById('name').value==null)||(isBlank(document.getElementById('name').value)==true)){	
			document.getElementById("errName").innerHTML="<span class='err_txt1'>" + sMsg + "</span>";
			iFlag=0;			
		}else if (isalpha(document.getElementById('name').value)==false){
			document.getElementById("errName").innerHTML="<span class='err_txt1'>" + sAlphaMsg + "</span>";
			iFlag=0;						
		}else{
			document.getElementById("errName").innerHTML="";			
		}
		/*Validation for Company Name*/
		if ((document.getElementById('companyname').value==null)||(isBlank(document.getElementById('companyname').value)==true)){	
			document.getElementById("errCompanyName").innerHTML="<span class='err_txt1'>" + sMsg + "</span>";
			iFlag=0;			
		}else if (isalpha(document.getElementById('companyname').value)==false){
			document.getElementById("errCompanyName").innerHTML="<span class='err_txt1'>" + sAlphaMsg + "</span>";
			iFlag=0;						
		}else{
			document.getElementById("errCompanyName").innerHTML="";			
		}
		
		/*Validation for Email Id*/
		if ((document.getElementById('email').value==null)||(isBlank(document.getElementById('email').value)==true))
		{
			document.getElementById("errEmailAddress").innerHTML="<span class='err_txt1'>"+ sMsg +"</span>";
			iFlag=0;
		}else{
			document.getElementById("errEmailAddress").innerHTML="";			
		}
		
		if(false==isBlank(document.getElementById('email').value))
		{		 
			if (eCheck(document.getElementById('email').value)==false)
			{
					document.getElementById("errEmailAddress").innerHTML="<span class='err_txt1'> Invalid E-mailId </span>";
					iFlag=0;
			}
		}else{
			document.getElementById("errEmailAddress").innerHTML="";			
		}
		
		
}

function previousRateSheet(endPos){
     var startPos=parseInt(document.getElementById("position").value)-endPos;
     var count=parseInt(document.getElementById("rateCount").value);
     if(startPos>((-1)*endPos) && startPos<0){
		 startPos=0
     }
     if(startPos<0)
     {
         alert("No comments to display");
     }
     else
         {
         $.ajax(
             {
                type: "GET", url: "../public/rate-sheet.htm?mode=more&position="+startPos+"&_1="+Math.random(),dataType: "text",cache: false, success: function (responseText)
                 {
		   $("#rateTab").html(responseText);
		   hidePrevNext(count,startPos,endPos,'prevRateSheet','nextRateSheet');

		 }
             }
          );
     }

 }
function rateSheetNext(endPos){
     var startPos=parseInt(document.getElementById("position").value)+endPos;
     var count=parseInt(document.getElementById("rateCount").value);
     if(startPos>count)
     {
         alert("No comments to display");
     }
     else
         {
		  $.ajax(
             {
                type: "GET", url: "../public/rate-sheet.htm?mode=more&position="+startPos+"&_1="+Math.random(),dataType: "text",cache: false, success: function (responseText)
                {
				     $("#rateTab").html(responseText);
					 hidePrevNext(count,startPos,endPos,'prevRateSheet','nextRateSheet');

			      }
               }
          );
     }
}

function previousUsageRecord(endPos){
     var startPos=parseInt(document.getElementById("position").value)-endPos;
     var count=parseInt(document.getElementById("usageCount").value);
     if(startPos>((-1)*endPos) && startPos<0){
		 startPos=0
     }
     if(startPos<0)
     {
         alert("No comments to display");
     }
     else
         {
         $.ajax(
             {
                type: "GET", url: "../secure/usage-history.htm?mode=more&position="+startPos+"&_1="+Math.random(),dataType: "text",cache: false, success: function (responseText)
                 {
		   $("#usageTab").html(responseText);
		   hidePrevNext(count,startPos,endPos,'prevUsage','nextUsage');

		 }
             }
          );
     }

 }
function nextUsageRecord(endPos){
     var startPos=parseInt(document.getElementById("position").value)+endPos;
     var count=parseInt(document.getElementById("usageCount").value);
     if(startPos>count)
     {
         alert("No comments to display");
     }
     else
         {
		  $.ajax(
             {
                type: "GET", url: "../secure/usage-history.htm?mode=more&position="+startPos+"&_1="+Math.random(),dataType: "text",cache: false, success: function (responseText)
                {
				     $("#usageTab").html(responseText);
					 hidePrevNext(count,startPos,endPos,'prevUsage','nextUsage');

			      }
               }
          );
     }
}



