﻿function doEdit(path, id) {
    window.location.href = "/admin/" + path + "edit.aspx?id="+ id;
}

function doEditWithType(path, id,type) {
    window.location.href = "/admin/" + path + "edit.aspx?id="+ id + "&type=" + type;
}

function doAdd(path) {
    window.location.href = "/admin/" + path + "edit.aspx";
}

//For AccommodationTypes(Imperial Suites and Royal Residence)
function doAddWithType(path,type) {
    window.location.href = "/admin/" + path + "edit.aspx?type="+type;
}

function doGalleryWithParentId(path,parentId) {    
    window.location.href = "/admin/" + path + "Gallery.aspx?pid=" + parentId;
}

function doGalleryWithIds(path,id,parentId) {    
    window.location.href = "/admin/" + path + "Gallery.aspx?id=" + id + "&pid=" + parentId ;
}

function doList(path) {
    window.location.href = "/admin/" + path + "list.aspx";
}

function doGallery(path, id) {
    window.location.href = "/admin/" + path + "Gallery.aspx?id="+ id;
}

function doImage(path, id){
    window.location.href = "/admin/" + path + "list/" + id + "/default.aspx";
}

function doActivate(frm, id) { 
    if (confirm("Are you sure you want to Activate?")) {   
        $(frm).append("<input type=\"hidden\" name=\"ID\" value=\"" + id + "\" />")
            .append("<input type=\"hidden\" name=\"__Mode__\" value=\"Activate\" />")
            .submit();
    }
}

function doDeActivate(frm, id) {
    if (confirm("Are you sure you want to DeActivate?")) {
        $(frm).append("<input type=\"hidden\" name=\"ID\" value=\"" + id + "\" />")
            .append("<input type=\"hidden\" name=\"__Mode__\" value=\"DeActivate\" />")    
            .submit();
    }
}

function doDelete(frm, id) {
    if (confirm("Are you sure you want to delete?")) {
        $(frm).append("<input type=\"hidden\" name=\"ID\" value=\"" + id + "\" />")
            .append("<input type=\"hidden\" name=\"__Mode__\" value=\"Delete\" />")    
            .submit();    
    }    
}

function doPage(frm, page) {     
    $("input[name=__Page__]", frm).val(page);
    $(frm).submit();
}
function doPageWithName(frm, page,name) {
    $("input[name=" + name + "]", frm).val(page);
    $(frm).submit();
}

//Tabs
var arrTabs = [
    {tabId:"lnkTabEn", inactiveClass:"eng", activeClass:"eng_select", contentId:"divEn", postfixElementId: "En"}, 
    {tabId:"lnkTabTh", inactiveClass:"thai", activeClass:"thai_select", contentId:"divTh", postfixElementId: "Th"}
];
function IntiTab(){
    $.each(arrTabs, function(i) {
        //Highlight the first tab as default
        $("#" + this.tabId).removeClass().addClass((i == 0) ? this.activeClass : this.inactiveClass);
        
        //Attach click event
        $("#" + this.tabId).click(function() {
            var myId = this.id;
            $.each(arrTabs, function() {
                if (this.tabId == myId) {
                    $("#" + this.tabId).removeClass().addClass(this.activeClass);
                    $("#" + this.contentId).show();
                } else {
                    $("#" + this.tabId).removeClass().addClass(this.inactiveClass);
                    $("#" + this.contentId).hide();                
                }
            });            
            if (myId != arrTabs[0].tabId) CopyToSecondaryLanguages();
            
            return false;
        });
    });     
}



function InitFlash(path, contorl){
    var paraObj = {allowfullscreen:false, menu:false, wmode:"transparent", allowscriptaccess:"always",allowDomain:true};
	swfobject.embedSWF(path, contorl, "250", "250", "8.0.0", "/javascripts/jQuery/Plugins/jQuery.swfobject/expressInstall.swf", paraObj);
}

function CopyToSecondaryLanguages() {
    if ($("input[id$=txtId]:hidden").val() != "0") return; //Copy in "ADD" mode only
    if (!$.isArray(arrTabs)) alert("Error");
    if (arrTabs.length <= 1) return;    
    
    var $main = $("#" + arrTabs[0].contentId);
    $main.find(":input").filter(":text, textarea").each(function() {
        var regex = new RegExp("(.+)" + arrTabs[0].postfixElementId);
        var result = regex.exec(this.id);
        var $source = $(this);
        if (result) {
            var prefix = result[1];
            $.each(arrTabs, function(i, val) {
                if (i > 0) {
                    $target = $("#" + prefix + val.postfixElementId, "#" + val.contentId);
                    if ($target.is(":input") && !/\S/.test($target.val())) {
                        $target.val($source.val());
                    }
                }
            });
        }
    });
}

function initialValidator() {
    //jQuery.validate()
    $.metadata.setType("attr", "validate");
//    $.extend($.validator.messages, {
//        required: "This field is required."
//    });
    $.validator.setDefaults({
        debug: false,
        ignoreTitle: true,
        errorPlacement: function(label, $el) {
                            if ($el.parents("td").length > 0) {
                                $el.parents("td").find("div.error").append(label);
                            } else {  
                               $el.parent("div").find("div.error").append(label);
                            }
                        },
        invalidHandler: function(form, validator) {
                            var errorList = validator.invalid;
                            var errEn = false, errTh = false;
                            for(obj in errorList) {
                                errEn |= /En$/.test(obj);
                                errTh |= /Th$/.test(obj);
                            }
                            if (errEn) {
                                $("#" + arrTabs[0].tabId).triggerHandler("click");
                            } else if (errTh) {
                                $("#" + arrTabs[1].tabId).triggerHandler("click");
                            }
                        }
    });
    $.validator.addMethod("dateTH", function(value, element) { 
        return this.optional(element) || (Date.parse(value) != null); 
    }, "Please enter date in format MM/dd/yyyy."); 
    $.validator.addMethod("dateTH1", function(value, element, params) {        
        var date1 = Date.parse(value);
        var date2 = Date.parse($("input[id$=" + params + "]").val());
        if ((date1 == null) || (date2 == null)) return true;
        //return false;
        return (date1.compareTo(date2) <= 0);
    }, "Must be less than end date.");    
    $.validator.addMethod("dateTH2", function(value, element, params) {
        var date1 = Date.parse($("input[id$=" + params + "]").val());
        var date2 = Date.parse(value);
        if ((date1 == null) || (date2 == null)) return true;
        return (date2.compareTo(date1) > 0);
    }, "Must be more than start date.");
	$.validator.addMethod("regexCheck", function(value, element, params) {
		var regex = new RegExp(params);
		return regex.test(value);		
	}, "You must explicitly declare your error.");
   
	$.validator.addMethod("phoneReservations", function(value, element) {
	    if(value!=''&&value.length<6){
	        return false;
	    }else
	        return true;
	}, 'Must be more than 5 digits.');
	
	$.validator.addMethod("telephoneLen", function(value, element) {
		return (value!=''&&value.length<6?false:true);	
	}, 'Must be more than 5 digits.');
	
	$.validator.addMethod("phoneRequired", function(value, element,params) {
	    var phone = $("input[id$=" + params + "]").val();
		return (value==''&&phone==''?false:true);   
	}, "Please enter email.");
}

$(document).ready(function() {
    //Validator
    initialValidator();
    
    //Tabs
    IntiTab();
    
    //Bind __Save__ button
    $("input[name=__Save__]").click(CopyToSecondaryLanguages);
    
    //Add odd/even classes to table.result
    $("table#result tr.list:odd").addClass("even");
    
    //Attach event to back button
    $("input[name=__Back__]").click(function() {
        window.history.back();
    });
});