function getXMLHTTP(){
	var _R = null;
	try{
		_R = new ActiveXObject("Msxml2.XMLHTTP")
	}catch(e){
		try{
			_R = new ActiveXObject("Microsoft.XMLHTTP")
		} catch(oc){
			_R = null
		}
	}
	
	if(!_R && typeof XMLHttpRequest != "undefined") {
		_R = new XMLHttpRequest();
	}
	return _R;
}

	function check_holiday (dateVal) {
		var DateParts = dateVal.split( "/" );

        if( DateParts[0] > 0 && DateParts[0] < 13 &&
            DateParts[1] > 0 && DateParts[1] < 32 && 
            DateParts[2] > 1900 && DateParts[2] < 2100 
        ){
            var dt_date = new Date();
            dt_date.setFullYear( DateParts[2], DateParts[0] - 1, DateParts[1] );
        } else {
        	alert('Invalid Date');
        }
         
        // check weekend
        if (dt_date.getDay() == 0 || dt_date.getDay() == 6) return true;  
         
		// check simple dates (month/date - no leading zeroes)
		var n_date = dt_date.getDate(),
		n_month = dt_date.getMonth() + 1;
		var s_date1 = n_month + '/' + n_date;
		
		if ( s_date1 == '1/1' // New Year's Day
				//|| s_date1 == '6/14' // Flag Day
				|| s_date1 == '7/4' // Independence Day
				|| s_date1 == '11/11' // Veterans Day
				|| s_date1 == '12/25' // Christmas Day
		) return true;
		
		// weekday from beginning of the month (month/num/day)
		var n_wday = dt_date.getDay(),
		n_wnum = Math.floor((n_date - 1) / 7) + 1;
		var s_date2 = n_month + '/' + n_wnum + '/' + n_wday;
		
		if ( s_date2 == '1/3/1' // Birthday of Martin Luther King, third Monday in January
			//|| s_date2 == '2/3/1' // Washington's Birthday, third Monday in February
			//|| s_date2 == '5/3/6' // Armed Forces Day, third Saturday in May
			|| s_date2 == '9/1/1' // Labor Day, first Monday in September
			|| s_date2 == '10/2/1' // Columbus Day, second Monday in October
			|| s_date2 == '11/4/4' // Thanksgiving Day, fourth Thursday in November
		) return true;
		
		// weekday number from end of the month (month/num/day)
		var dt_temp = new Date (dt_date);
		dt_temp.setDate(1);
		dt_temp.setMonth(dt_temp.getMonth() + 1);
		dt_temp.setDate(dt_temp.getDate() - 1);
		n_wnum = Math.floor((dt_temp.getDate() - n_date - 1) / 7) + 1;
		var s_date3 = n_month + '/' + n_wnum + '/' + n_wday;
		
		if ( s_date3 == '5/1/1' // Memorial Day, last Monday in May
		) return true;
		
		/*
		// misc complex dates
		if (s_date1 == '1/20' && (((dt_date.getFullYear() - 1937) % 4) == 0)
		// Inauguration Day, January 20th every four years, starting in 1937.
		) return true;
		
		if (n_month == 11 && n_date >= 2 && n_date < 9 && n_wday == 2
		// Election Day, Tuesday on or after November 2.
		) return true;
		*/
		return false;
	}

    function CalculateSecondPaydate(field, channel){
    	 if ((field == 'payDate1' || field == 'payDate2') && channel == 5) {
	    		//alert("dada"+$('#field_'+field).val());
	    		if (check_holiday($('#field_'+field).val())) {
	    			alert('Paydate can\'t be in a week-end or holiday!');
	    			$('#field_'+field).val('');
	    			return true;
	    		}
    	}
    	if (field == 'payDate2' || field == 'UKpayDate2') {
    		return true;
    	}
    	if ($('#field_payDate1').length > 0) {
   			CalculateSecondPaydateUSA(channel);
    	} else if ($('#field_UKpayDate1').length > 0) {
    		CalculateSecondPaydateUK();
    	} else {
    		return true;
    	} 	
	}
	
    function CalculateSecondPaydateUSA(channel){
        if( $('#field_payDate1').length > 0 && $('#field_payDate1').val()  != "" && $('#field_payPeriod').length > 0) {
            if ($('#field_payPeriod').length < 0){
            	var PayFreq  = 'Bi_Weekly';	
            } else {
            	var PayFreq  = $('#field_payPeriod').val();	
            }

            var PayDate1 = $('#field_payDate1').val();
            
			
            var DateParts = PayDate1.split( "/" );

            if( DateParts[0] > 0 && DateParts[0] < 13 &&
                DateParts[1] > 0 && DateParts[1] < 32 && 
                DateParts[2] > 1900 && DateParts[2] < 2100 
            ){
                var Date1 = new Date();
                Date1.setFullYear( DateParts[2], DateParts[0] - 1, DateParts[1] );

                var Date2 = new Date();
                Date2 = Date1;

                switch( PayFreq ){
                    case 'Weekly':
                        Date2.setDate( Date2.getDate() + 7 );
                        break;

                    case 'Bi_Weekly':
                        Date2.setDate( Date2.getDate() + 14 );
                        break;

                    case 'Twice_Monthly':
                        Date2.setDate( Date2.getDate() + 14 );
                        break;

                    case 'Monthly':
                        Date2.setMonth( Date2.getMonth() + 1 );
                        break;
                    
                    default:
                        Date2.setDate( Date2.getDate() + 14 );
                        break;
                }
				
                $('#field_payDate2').val( (Date2.getMonth() + 1 ) + "/" + Date2.getDate() + "/" + Date2.getFullYear());
                if (check_holiday($('#field_payDate2').val()) && channel == 5) {
	    			alert('Second paydate can\'t be in a week-end or holiday!');
	    			$('#field_payDate2').val('');
	    			return true;
	    		}
            }

        }
    }

    function CalculateSecondPaydateUK(){
        //First we make sure that we actually have these values on our form
        //alert("0");    
        if( $('#field_UKpayDate1').length > 0 && $('#field_UKpayDate1').val()  != "") {
            if ($('#field_payFrequency').length < 0){
            	var PayFreq  = '6';	
            } else {
            	var PayFreq  = $('#field_payFrequency').val();	
            }            
            
            var PayDate1 = $('#field_UKpayDate1').val();

            var DateParts = PayDate1.split( "/" );
			//alert(DateParts[0] + DateParts[1] + DateParts[2]);

            if( DateParts[1] > 0 && DateParts[1] < 13 &&
                DateParts[0] > 0 && DateParts[0] < 32 && 
                DateParts[2] > 1900 && DateParts[2] < 2100 
            ){
                var Date1 = new Date();
                Date1.setFullYear( DateParts[2], DateParts[1], DateParts[0] );

                var Date2 = new Date();
                Date2.setFullYear( DateParts[2], DateParts[1] - 1, DateParts[0] );

				var Date5 = new Date ( Date2.getFullYear(), Date2.getMonth(), 0);
				//alert(Date5);

                switch( PayFreq ){
                    //Last working day of the month
                    case '1':
                        Date2.setMonth( Date1.getMonth() + 1 );
                        Date2 = new Date ( Date2.getFullYear(), Date2.getMonth(), 0);

                        while( Date2.getDay() == 0 || Date2.getDay() == 6 ){
                            Date2.setDate( Date2.getDate() - 1 );
                        }

                        break;
                    
                    //Last X of the month
                    case '2':
                    	Date2.setMonth( Date1.getMonth());
                        Date2 = new Date ( Date1.getFullYear(), Date2.getMonth(), Date1.getDate());
                        break;
                    
                    //4 weekly
                    case '3':
                        Date2.setDate( Date2.getDate() + 28 );
                        break;
                    
                    //Specific Day of the Month
                    case '4':
                        Date2.setMonth( Date2.getMonth() + 1 );
                        break;
                   
                    //Twice Monthly
                    case '5':
                        Date2.setDate( Date2.getDate() + 14 );
                        break;
                   
                    //Bi-weekly
                    case '6':
                        Date2.setDate( Date2.getDate() + 14 );
                        break;

                    default:
                    	//alert("here");
                        Date2.setDate( Date2.getDate() + 14 );
                        break;
                }

                $('#field_UKpayDate2').val(Date2.getDate() + "/" + ( ( Date2.getMonth() * 1 ) + ( 1 * 1 ) ) + "/" + Date2.getFullYear());
            }

        }
    }
	
	function daysInMonth(month,year) {
		var dd = new Date(year, month, 0);
		return dd.getDate();
	}



