﻿function AjaxDataPostNoLoad(ctrldata, strUrl, ctrlUpdate, strType) {    
    $.ajax({
        url: strUrl,
        type: strType,
        data: ctrldata,
        success: function(resp) { $('#' + ctrlUpdate).html(resp); }
    });
}
function AjaxRequest(ctrlLoad, ctrlUpdate, strUrl, strType) {
    $('#' + ctrlLoad).show();
    $.ajax({
        url: strUrl,
        type: strType,
        success: function(resp) {$('#' + ctrlUpdate).html(resp); $('#' + ctrlLoad).hide(); }
    });
}
function AjaxRequestNoLoad(ctrlUpdate, strUrl, strType) {
    $.ajax({
        url: strUrl,
        type: strType,
        success: function(resp) {$('#' + ctrlUpdate).fadeIn(); $('#' + ctrlUpdate).html(resp);}
    });
}
function AjaxFormPost(ctrlform, ctrlLoad, strUrl, ctrlUpdate, strType) {   
    $('#' + ctrlLoad).show();
    $.ajax({
        url: strUrl,
        type: strType,
        data: $('#' + ctrlform).serialize() + '&call=ajax',
        success: function(resp) { $('#' + ctrlUpdate).html(resp); $('#' + ctrlLoad).hide(); }
    });
}
function AjaxDataPost(ctrldata, ctrlLoad,strUrl, ctrlUpdate,  strType) {
    $('#' + ctrlLoad).show();
    $.ajax({
        url: strUrl,
        type: strType,
        data: ctrldata,
        success: function(resp) { $('#' + ctrlUpdate).html(resp); $('#' + ctrlLoad).hide(); }
    });
}
 function DisableDropdownItem(drpCtrl,drpValue)
 {
    $('#'+drpCtrl+' option[value='+drpValue+']').attr('disabled','disabled');
 }

function ValidateDate(day,Month,year)
{   
    var monthfield=Month;
    var dayfield=day;
    var yearfield=year;
    var dayobj = new Date(yearfield, monthfield-1, dayfield)
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
        return false;
    return true;
}

function ChngMemCountwiseState(CtrlCountry,CtrlUs,CtrlAus,CtrlCA,CtrlInd,TxtOther,drpAllState)
{
    CtrlUs.style.display='none';
    CtrlAus.style.display='none';
    CtrlCA.style.display='none';
    CtrlInd.style.display='none';
    TxtOther.style.display='none';
    drpAllState.style.display='none';
    TxtOther.value='';    
    switch(parseInt(CtrlCountry.value))
    {
        case 226:
            CtrlUs.style.display='';
            break;
        case 14:
            CtrlAus.style.display='';
            break;
        case 37:
            CtrlCA.style.display='';
            break;
        case 103:
            CtrlInd.style.display='';
            break;
         case -1:
            drpAllState.style.display='';            
            break;
        default:
    
            TxtOther.style.display='';
            break;
    }
    
}

function ChangeCountrywiseState(CtrlCountry,CtrlUs,CtrlAus,CtrlCA,CtrlInd,TxtOther,LblTitle)
{
    $('#' + CtrlUs).hide();
    $('#' + CtrlAus).hide();
    $('#' + CtrlCA).hide();
    $('#' + CtrlInd).hide();
    $('#' + TxtOther).hide();
    $('#' + TxtOther).val('');    
    $('#' + LblTitle).html('State/provinece');
    switch(parseInt($('#' + CtrlCountry).val()))
    {
        case 226:
            $('#' + CtrlUs).show();
            break;
        case 14:
            $('#' + CtrlAus).show();
            break;
        case 37:
            $('#' + CtrlCA).show();
            break;
        case 103:
            $('#' + CtrlInd).show();
            break;       
        default:
            $('#' + TxtOther).show();
            $('#' + LblTitle).html('Other State');
            break;
    }    
}
function ChangeCountrywiseStateForSearch(CtrlCountry,CtrlUs,CtrlAus,CtrlCA,CtrlInd,TxtOther,CtrlAllState)
{
    $('#' + CtrlUs).hide();
    $('#' + CtrlAus).hide();
    $('#' + CtrlCA).hide();
    $('#' + CtrlInd).hide();
    $('#' + TxtOther).hide();
    $('#' + TxtOther).val('');    
    $('#' + CtrlAllState).hide();
    switch(parseInt($('#' + CtrlCountry).val()))
    {
        case 226:
            $('#' + CtrlUs).show();
            break;
        case 14:
            $('#' + CtrlAus).show();
            break;
        case 37:
            $('#' + CtrlCA).show();
            break;
        case 103:
            $('#' + CtrlInd).show();
            break;  
        case -1:
            $('#' + CtrlAllState).show();          
            break;     
        default:
            $('#' + TxtOther).show();
            break;
    }    
}
function trim(strComp)
{	
	return jQuery.trim(strComp);
}

function ValidateControl(formObject, fieldDescription,lblObject) 
{
	var tempFormValue;
	var strError ="";	
	var iFocus =-1;	
	var ErrCount=1;
    
	for (var i =0; i < ValidateControl.arguments.length; i=i+3)
	{	   	   		
	    tempFormValue =trim($('#' + ValidateControl.arguments[i]).val());	   	   	    
	    if (tempFormValue.length == 0)
		{
		    if(strError!='')		    
		        strError = strError+  "<br/> - " + ValidateControl.arguments[i+1];
		    else
		        strError= " - " + ValidateControl.arguments[i+1];
		    ErrCount = ErrCount + 1;
		    $('#' + ValidateControl.arguments[i+2]).attr('class','alert');		    
		    if (iFocus == -1)
			    iFocus = i;
		}
		else
		{
		     $('#' + ValidateControl.arguments[i+2]).attr('class','');
		}
	}

	if (strError.length != 0)
	{				
		return strError;
	}
	else
		return strError;
}

    function isValidURL(url){
        var RegExp = /^http(s)?\:\\\/\\\/([\\w\-]+\.)+([\\w\-]{2,4})+([\\w\-\ \.\\\/\?\%\&amp;\=]*)$/;
        if(RegExp.test(url)){
            return true;
        }else{
            return false;
        }
    }
    function isEmailAddress(emField) { //reference to email field passed as argument
        var temField = emField.split('@');
        if (temField.length != 2) {
            return false;
        }
        else {
            if (temField[0].length == 0) {
                return false;
            }
            else {
                var lstIndex = temField[1].lastIndexOf('.');
                if (lstIndex == -1) {
                    return false;
                }
                else {
                    if (temField[1].substring(lstIndex + 1).length == 2 || temField[1].substring(lstIndex + 1).length == 3) {
                        return true;
                    }
                    else {
                        return false;
                    }
                }
            }
        }
        return true;
    }
function ShowHideSlideCtrl(ShowCtrl,HideCtrl,Duration)
{
    $('#' + HideCtrl).slideUp();
    $('#' + ShowCtrl).slideDown();
    return false;
}
function ShowHideCtrl(ShowCtrl,HideCtrl)
{
    $('#' + HideCtrl).slideUp('slow');
    $('#' + ShowCtrl).slideDown('slow');    
}

function CharacterCount(obj, total)
        {
            if(total == null) total = 500;
            var len = obj.value.length;
            var newdiv;
        	
            if(document.getElementById("note" + obj.id) == null){
	            newdiv = document.createElement('div');
	            newdiv.id = "note" + obj.id;
	            newdiv.className="";
                obj.parentNode.appendChild(newdiv);
            }
            else{
	            newdiv = document.getElementById("note" + obj.id);
            } 
        	
            if(len == 0){
                obj.parentNode.removeChild(newdiv);
            }
        		
            if(len >= total){
	            var temp = obj.value.substring(0, total)
	            obj.value = temp;
	            newdiv.innerHTML = "You have reached maximum characters limit of <strong>" + total + "</strong>."
            }
            else{
	            newdiv.innerHTML = "Your maximum characters limit is: <strong>" + total + "</strong>.<br> Current character count: <strong>" + len + "</strong>.";
            }
        }

function DisplayMessage(ctrl,clsName,Msg)
{
    $('#' + ctrl).show();
    $('#' + ctrl).attr('class',clsName +' clear');
    $('#' + ctrl).html(Msg);       
    setTimeout('$(\'#'+ctrl+'\').hide();',10000);
}

function DisplMsg(Ctrl,ErrMsg,Msgclass)
{    
    $('#'+ Ctrl).show();
    $('#'+ Ctrl).html(ErrMsg);
    $('#'+ Ctrl).attr('class',Msgclass);
    //fourPop(Ctrl);
}
function DisplMsgFront(Ctrl,ErrMsg,Msgclass)
{    
    $('#'+ Ctrl).show();
    $('#'+ Ctrl).html(ErrMsg);
    //$('#'+ Ctrl).attr('class',Msgclass);
    fourPop(Ctrl);
    closePopup();
}
function closePopup() {
    setTimeout(function() {
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();
        });
    }, 3000)
}
 
function DisplMsgIframeFront(Ctrl,ErrMsg,Msgclass)
{    
    Ctrl.show();
    Ctrl.html(ErrMsg);    
    fourPopIframe(Ctrl);
}
 
 function fourPop(popID) {
    $('#' + popID).addClass('popup_block');
    var popWidth = 500;    
    $('#' + popID).fadeIn().css().prepend('<a href="#" class="close"><img src="' + WebSiteUrl + 'images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');    
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;    
    
    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>');
    $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn();

    return false;
};

function fourPopCompose(popID) {
    $('#' + popID).addClass('popup_block');
    var popWidth = 500;    
    $('#' + popID).fadeIn().css().prepend('<a href="#" class="close"><img src="' + WebSiteUrl + 'images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');    
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;    
    
    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>');
    $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn();
   
};

function fourPopIframe(popID) {    
    popID.addClass('popup_block');
    var popWidth = 300;    
    popID.fadeIn().css().prepend('<a href="#" class="Ifrmfadeclose"><img src="' + WebSiteUrl + 'images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');    
    var popMargTop = (popID.height() + 80) / 2;
    var popMargLeft = (popID.width() + 80) / 2;    
    
    //Apply Margin to Popup
    popID.css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    parent.$('body').append('<div id="Ifrmfade"></div>');
    parent.$('#Ifrmfade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn();

    return false;
};

//Close Popups and Fade Layer
$('a.Ifrmfadeclose, #Ifrmfade').live('click', function() {
    $('#Ifrmfade , .popup_block').fadeOut(function() {
        $('#Ifrmfade, a.Ifrmfadeclose').remove();
    }); //fade them both out

    return false;
}); 

//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() {
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();
    }); //fade them both out

    return false;
}); 





function DisplMsgParent(Ctrl,ErrMsg,Msgclass)
{    
    parent.$('#'+ Ctrl).show();
    parent.$('#'+ Ctrl).html(ErrMsg);
    parent.$('#'+ Ctrl).attr('class',Msgclass);
}

 

//--to show the error message of required valiation--//
function ValidateControlAdmin(formObject, fieldDescription,lblObject)
{
    var tempFormValue;
	var strError ="";	
	var iFocus =-1;	
	var ErrCount=1;
	for (var i =0; i < ValidateControlAdmin.arguments.length; i=i+3)
	{
	    if(typeof(ValidateControlAdmin.arguments[i]) == 'undefined')
		{		    
		    return 'Error';
		}	
		tempFormValue = jQuery.trim(ValidateControlAdmin.arguments[i].val());
		
		 if (tempFormValue.length < 15)
	        {deleteLoop = tempFormValue.length}
	     else
	        {deleteLoop = 15}
	        
	     for (var j = 0; j < deleteLoop; j++) 
		{
		    tempFormValue = tempFormValue.replace(/ / , "");
		}   
		
		if (tempFormValue.length == 0)
		{
		    if(strError!='')		    
		        strError = strError+  "<br/> - " +ValidateControlAdmin.arguments[i+1];
		    else
		        strError= " - " + ValidateControlAdmin.arguments[i+1];
		    ErrCount = ErrCount + 1;
		    ValidateControlAdmin.arguments[i+2].addClass('alert');
		    if (iFocus == -1)
			    iFocus = i;
		}
		else
		{
		     ValidateControlAdmin.arguments[i+2].addClass('');
		}
	}
	if (strError.length != 0)
	{				
		return strError;
	}
	else
		return strError;
}

function PasswordChk(ctrl1, ctrl2, errMsg) {
    var digit = 0, chr = 0;   
    if ($('#' + ctrl1).val() == $('#' + ctrl2).val()) {
        if ($('#' + ctrl1).val().length < 6) {
            return 'Password must be at least 6 characters long and should contain at least one number and a letter.';
        }
        else {
            var tmp = $('#' + ctrl1).val();
            for (var i = 0; i < tmp.length; i++) {
                if (tmp.charCodeAt(i) >= 48 && tmp.charCodeAt(i) <= 57) {
                    digit++;
                }
                else if ((tmp.charCodeAt(i) >= 65 && tmp.charCodeAt(i) <= 90) || (tmp.charCodeAt(i) >= 97 && tmp.charCodeAt(i) <= 122)) {
                    chr++;
                }
            }
            if (!(digit > 0 && chr > 0)) {
                return 'Password must be at least 6 characters long and should contain at least one number and a letter.';
            }
        }
    }
    else {
        return 'Password and confirm password must be same.';
    }
    return '';
}

function DirValCtrl()
{
    var lbl;
    var inctrl;
    var ErrMsg='';
    var IsFirst=0;
    $('span[class="red"]').each(function() {
        lbl = $(this).siblings();
        if ($(this).siblings().length > 0) {
            inctrl = $(lbl).attr('for');

            if (inctrl.length > 0 && $(lbl).attr('for').length > 0) {
                
                switch ($('#' + inctrl).attr('type')) {
                    case "textarea":
                    case "text":
                    case "file":
                    case "password":
                        if (jQuery.trim($('#' + inctrl).val()) == '' && $('#' + inctrl).is(':visible') == true) {
                            $('#' + inctrl).attr('style', 'border:solid 1px #FF0000 !important');
                            ErrMsg = ErrMsg + '<br/> - ' + $(lbl).html();
                            if (IsFirst == 0)
                                $('#' + inctrl).focus();
                            IsFirst = 1;
                        }
                        else
                            $('#' + inctrl).attr('style', '');
                        break;
                    case "select":
                        if ($('#' + inctrl).val() == '') {
                            ErrMsg = ErrMsg + '<br/> - ' + $(lbl).html();
                            $('#' + inctrl).attr('style', 'border:solid 1px #FF0000 !important');
                        }
                        break;
                    case "select-one":
                        if ($('#' + inctrl).val() == '') {
                            ErrMsg = ErrMsg + '<br/> - ' + $(lbl).html();
                            $('#' + inctrl).attr('style', 'border:solid 1px #FF0000 !important');
                        }
                        break;
                    case "checkbox":
                        if (!$('#' + inctrl).attr('checked'))
                            ErrMsg = ErrMsg + '<br/> - ' + $(lbl).html();
                        break;
                }
            }
        }
    });   
   return ErrMsg;   
} 

function isValidURL(url){
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    if(RegExp.test(url)){
        return true;
    }else{
        return false;
    }
} 

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 findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
function EncodeText(UrlText)
{
    UrlText= UrlText.replace(/%/g,'-per-');
    UrlText = UrlText.replace(/&/g,'-and-');
    UrlText = UrlText.replace(/\?/g,'-que-');
    UrlText = replaceCharacters(UrlText,'/','-sla-');
    return UrlText;
}
function replaceCharacters(MainStr,ScanChar,RepChar) { 
  var newString = MainStr.split(ScanChar);
  newString = newString.join(RepChar);
  return newString;
}
function CreateTag(TagName,TagProp,TagValue)
{
    var ctrl = document.createElement(TagName);    
    for(var i=0;i<CreateTag.arguments.length-1;i++)
    {        
        if(document.all)
        {            
            if(CreateTag.arguments[i+1]=='style')
            {
                var tmpStyle = CreateTag.arguments[i+2].split(':');
                switch(tmpStyle[0])
                {                    
                    case "display":
                        ctrl.style.display=tmpStyle[1];                        
                        break;
                }                
            }
            else if(CreateTag.arguments[i+1]=='class')
                ctrl.setAttribute('className',CreateTag.arguments[i+2]); 
            else
                ctrl.setAttribute(CreateTag.arguments[i+1],CreateTag.arguments[i+2]);        
        }
        else
        {
            ctrl.setAttribute(CreateTag.arguments[i+1],CreateTag.arguments[i+2]);
        }
        i=i+1;
    }    
    return ctrl;
}

function isImage(obj)
	{	    	    
		if (obj.value.length>0)
		{
			if (obj.value.length>4)
			{
			    var tmpExt= obj.value.split('.');
				var ext = tmpExt[tmpExt.length-1];		
				if (ext.toLowerCase() == 'jpg'  || ext.toLowerCase() == 'jpeg' || ext.toLowerCase() == 'gif' || ext.toLowerCase() == 'png')
				{
					return true;
				}
				else
				{										
					return false;
				}
			}
			else
			{			
				return false;
			}
		}
		return false;
	}
	
	
jQuery.fn.watermark = function(message){
    $(this).css('color','grey');
    $(this).val(message).focus(function(){ $(this).css('color','Black');if($(this).val() == message)$(this).val('');}).blur(function(){if (jQuery.trim($(this).val()) == ''){$(this).css('color','grey');$(this).val(message);} });
};
   
   
   
 // check date JavaScript function
// if date is valid then function returns true, otherwise returns false
function isDate(txtDate){
  var objDate;  // date object initialized from the txtDate string
  var mSeconds; // milliseconds from txtDate

	// date length should be 10 characters - no more, no less
  if (txtDate.length != 10) return false;

	// extract day, month and year from the txtDate string
	// expected format is mm/dd/yyyy
	// subtraction will cast variables to integer implicitly
  var day   = txtDate.substring(3,5)  - 0;
  var month = txtDate.substring(0,2)  - 1; // because months in JS start with 0
  var year  = txtDate.substring(6,10) - 0;

	// third and sixth character should be /
	if (txtDate.substring(2,3) != '/') return false;
	if (txtDate.substring(5,6) != '/') return false;

  // test year range
  if (year < 999 || year > 3000) return false;

  // convert txtDate to the milliseconds
  mSeconds = (new Date(year, month, day)).getTime();

  // initialize Date() object from calculated milliseconds
  objDate = new Date();
  objDate.setTime(mSeconds);

  // compare input parameter date and created Date() object
  // if difference exists then date isn't valid
  if (objDate.getFullYear() != year)  return false;
  if (objDate.getMonth()    != month) return false;
  if (objDate.getDate()     != day)   return false;

	// otherwise return true
  return true;
} 

function DisplMsgNote(Ctrl,ErrMsg,Msgclass)
{    
    $('#'+ Ctrl).show();
    $('#'+ Ctrl).html(ErrMsg);
    //$('#'+ Ctrl).attr('class',Msgclass);
    fourPop(Ctrl);
}

function DisplMsgNoteCompose(Ctrl,ErrMsg,Msgclass)
{    
    $('#'+ Ctrl).show();
    $('#'+ Ctrl).html(ErrMsg);
    //$('#'+ Ctrl).attr('class',Msgclass);
    fourPopCompose(Ctrl);
}

function DisplMsgComment(Ctrl,ErrMsg,Msgclass)
{    
    $('#'+ Ctrl).show();
    $('#'+ Ctrl).html(ErrMsg);
    //$('#'+ Ctrl).attr('class',Msgclass);
    fourPopAdmin(Ctrl);
}
 function fourPopAdmin(popID) {
    $('#' + popID).addClass('popup_block');
    var popWidth = 700;    
    $('#' + popID).fadeIn().css().prepend('<a href="#" class="close"><img src="' + WebSiteUrl + 'images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');    
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;    
    
    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>');
    $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn();

    return false;
};

// common page code for checking password.
function CheckPasswordCode(UserID, tbxPasswordClientID, tbxConfirmPasswordClientID) {
    var ErrMsg = '';
    var strPassword = $('#' + tbxPasswordClientID).val();
    var strConfPassword = $('#' + tbxConfirmPasswordClientID).val();
    if (UserID == 0 && strPassword.length > 0 && strConfPassword.length > 0) {
        var msgPass = '';
        msgPass = PasswordChk(tbxPasswordClientID, tbxConfirmPasswordClientID, '');
        if (msgPass.length > 0) {
            ErrMsg = ErrMsg + '<br/ > - ' + msgPass;
        }
    }
    else {
        if (strPassword != '') {
            if (!IsValidPassword(strPassword)) {
                ErrMsg = ErrMsg + '<br/ > - Invalid Password';
            }
        }
    }
    return ErrMsg;
}

function IsValidPassword(Password) {
    var regex = /^.[^\']{5,20}$/;
    if (regex.test(Password)) return true;
    else return false;
} 
// function noBack(){window.history.forward();}
//        noBack();
//        window.onload=noBack;
//        window.onpageshow=function(evt){if(evt.persisted)noBack();}
//        window.onunload=function(){void(0);}
