// JavaScript Document
/**
 * @author GeekTantra
 * @date 20 September 2009
 */
(function(jQuery){
    var ValidationErrors = new Array();
    jQuery.fn.validate = function(options){
        options = jQuery.extend({
            expression: "return true;",
            message: "",
            error_class: "ValidationErrors",
            error_field_class: "ErrorField",
            live: true
        }, options);
        var SelfID = jQuery(this).attr("id");
        var unix_time = new Date();
        unix_time = parseInt(unix_time.getTime() / 1000);
        if (!jQuery(this).parents('form:first').attr("id")) {
            jQuery(this).parents('form:first').attr("id", "Form_" + unix_time);
        }
        var FormID = jQuery(this).parents('form:first').attr("id");
        if (!((typeof(ValidationErrors[FormID]) == 'object') && (ValidationErrors[FormID] instanceof Array))) {
            ValidationErrors[FormID] = new Array();
			
        }
        if (options['live']) {
            if (jQuery(this).find('input').length > 0) {
                jQuery(this).find('input').bind('blur', function(){
                    if (validate_field("#" + SelfID, options)) {
                        if (options.callback_success) 
                            options.callback_success(this);
                    }
                    else {
                        if (options.callback_failure) 
                            options.callback_failure(this);
                    }
                });
                jQuery(this).find('input').bind('focus keypress click', function(){
                    jQuery("#" + SelfID).next('.' + options['error_class']).remove();
                    jQuery("#" + SelfID).removeClass(options['error_field_class']);
					
                });
            }
            else {
                jQuery(this).bind('blur', function(){
                    validate_field(this);
                });
                jQuery(this).bind('focus keypress', function(){
                    jQuery(this).next('.' + options['error_class']).fadeOut("fast", function(){
                        jQuery(this).remove();
                    });
					
                    jQuery(this).removeClass(options['error_field_class']);
                });
            }
        }
        /*jQuery(this).parents("form").submit(function(){
            if (validate_field('#' + SelfID)) {
				$JQ_Obj('#Common_hidden_box_for_validation').val('1');
                return true;
			}else {
				$JQ_Obj('#Common_hidden_box_for_validation').val('');
                return false;
			}
        });*/
		  jQuery(this).parents("form").submit(function(){
				/*$JQ_Obj('#Common_ERRONR').html("");
				$JQ_Obj('#Common_ERRONR').removeClass(options['error_field_class']);*/
			//$JQ_Obj('#Common_hidden_box_for_validation').val('');
			//alert(SelfID);
            if (validate_field('#' + SelfID)) {
				//alert("if"); 
				//$JQ_Obj('#Common_hidden_box_for_validation').val('1');
				//$JQ_Obj( '#Validation_field' ).focus(); 
                return true;
			}else {
				//alert("els"); 
			//	$JQ_Obj('#Common_hidden_box_for_validation').val('');
				//$JQ_Obj( '#Validation_field' ).focus(); 
                return false;
			}
        });
        function validate_field(id){
			
            var self = jQuery(id).attr("id");

            var expression = 'function Validate(){' + options['expression'].replace(/VAL/g, 'jQuery(\'#' + self + '\').val()') + '} Validate()';
            var validation_state = eval(expression);
            if (!validation_state) {
				$JQ_Obj('#Common_ERRONR').slideDown('medium');
                if (jQuery(id).next('.' + options['error_class']).length == 0) {
                    jQuery(id).after('<span class="' + options['error_class'] + '">' + options['message'] + '</span>');
                    jQuery(id).addClass(options['error_field_class']);
					//$JQ_Obj('#Common_ERRONR').show();
					
					//$JQ_Obj('#Common_ERRONR').html("Some values you have entered in the form are invalid. Please check the form above and correct it.<input type='button' value='Ok' onclick='CloseCOmmon_Msg()'>");
					$JQ_Obj('#Common_ERRONR').html("Some values you have entered in the form are invalid. Please check the form above and correct it.");
					$JQ_Obj('#Common_ERRONR').addClass(options['error_field_class']);
					
					setTimeout("$JQ_Obj('#Common_ERRONR').slideUp('medium')",5000);

                }
                if (ValidationErrors[FormID].join("|").search(id) == -1) {
                    ValidationErrors[FormID].push(id);
					setTimeout("$JQ_Obj('#Common_ERRONR').slideUp('medium')",5000);
				}
                return false;
            }
            else {
                for (var i = 0; i < ValidationErrors[FormID].length; i++) {
					
                    if (ValidationErrors[FormID][i] == id) 
                        ValidationErrors[FormID].splice(i, 1);
                }
				setTimeout("$JQ_Obj('#Common_ERRONR').slideUp('medium')",5000);
                return true;
            }
				
        }
    };
    jQuery.fn.validated = function(callback){

        jQuery(this).each(function(){
            if (this.tagName == "FORM") {
                jQuery(this).submit(function(){
                    if (ValidationErrors[jQuery(this).attr("id")].length == 0) 
                        callback();
					return false;
                });
            }
        });
    };
})(jQuery);

function CloseCOmmon_Msg(){
			//$JQ_Obj('#Common_ERRONR').html("--");
			$JQ_Obj('#Common_ERRONR').slideUp('medium');

}

/**
 * @author GeekTantra
 * @date 24 September 2009
 */
/*
 * This functions checks where an entered date is valid or not.
 * It also works for leap year feb 29ths.
 * @year: The Year entered in a date
 * @month: The Month entered in a date
 * @day: The Day entered in a date
 */
function isValidDate(year, month, day){
    var date = new Date(year, (month - 1), day);
    var DateYear = date.getFullYear();
    var DateMonth = date.getMonth();
    var DateDay = date.getDate();
    if (DateYear == year && DateMonth == (month - 1) && DateDay == day) 
        return true;
    else 
        return false;
}
/*
 * This function checks if there is at-least one element checked in a group of check-boxes or radio buttons.
 * @id: The ID of the check-box or radio-button group
 */
function isChecked(id){

    var ReturnVal = false;
    $JQ_Obj("#" + id).find('input[type="radio"]').each(function(){
        if ($JQ_Obj(this).is(":checked")) 
            ReturnVal = true;
    });
    $JQ_Obj("#" + id).find('input[type="checkbox"]').each(function(){
        if ($JQ_Obj(this).is(":checked")) 
            ReturnVal = true;
    });
    return ReturnVal;
}
function isPasterPhoneValidate(dom1,dom2,dom3,chkbox)
{
	if(chkbox == 3 || chkbox == 4)
	{
		if(dom1.match(/^[0-9]{3}$/) != null && dom2.match(/^[0-9]{3}$/)  != null && dom3.match(/^[0-9]{4}$/) != null )
			return true;
		else
			return false;
	}
	else
	{
		return true;
	}
}
function isValidPasterEmail(email,chkbox)
{
	if(chkbox == 3 || chkbox == 4)
	{
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(email);
	}
	else
	{
		return true;
	}
}
function isSponsorPhoneValidate(dom1,dom2,dom3,chkbox)
{
	if(chkbox == 1 || chkbox == 2)
	{
		if(dom1.match(/^[0-9]{3}$/) != null && dom2.match(/^[0-9]{3}$/)  != null && dom3.match(/^[0-9]{4}$/) != null )
			return true;
		else
			return false;
	}
	else
	{
		return true;
	}
}
function isValidSponsorEmail(email,chkbox)
{
	if(chkbox == 1 || chkbox == 2)
	{
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(email);
	}
	else
	{
		return true;
	}
}
function getDateObject(dateString,dateSeperator)
{
	//This function return a date object after accepting 
	//a date string ans dateseparator as arguments
	var curValue=dateString;
	var sepChar=dateSeperator;
	var curPos=0;
	var cDate,cMonth,cYear;
	
	//extract year portion
	curPos=dateString.indexOf(sepChar);
	cYear=dateString.substring(0,curPos);
	
	//extract month portion    
	endPos=dateString.indexOf(sepChar,curPos+1);   
	cMonth=dateString.substring(curPos+1,endPos);
	
	
	//extract day portion    
	curPos=endPos;
	endPos=curPos+3;   
	cDate=curValue.substring(curPos+1,endPos);
	
	
	//Create Date Object
	dtObject=new Date(cYear,cMonth,cDate); 
	return dtObject;
}
function validateTwoDate(date1,date2)
{
	if(date1 == '' || date2 == '')
	{
		return false;
	}
	else
	{
		var new_date1 = getDateObject(date1,"/");
		var new_date2 = getDateObject(date2,"/");
		if(new_date1 > new_date2)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
function validateCurrentDate(date1,date2)
{
	if(date1 == '')
	{
		return false;
	}
	else if(date2 != '')
	{
		var new_date1 = getDateObject(date1,"/");
		var new_date2 = getDateObject(date2,"/");
		if(new_date1 > new_date2)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
