﻿// JScript File

var emailFormVisibility="hidden";
var emailHeight = 0;
//var detailHeight=0;

var _main = {
   load:function(){
      var imgStock = ["images/mainstock/main_stock.jpg",
                     "images/mainstock/main_stock2.jpg",
                     "images/mainstock/main_stock3.jpg",
                     "images/mainstock/main_stock4.jpg",
                     "images/mainstock/main_stock5.jpg"];
      if ($("stockImage")) {
            new Fader("stockImage", imgStock, {
        	    fadeInDuration: 1.0,
	            fadeOutDuration: 1.0,
	            displayDuration: 1.0
        });
      };      
      var e=$("fromEmail");
	  if (e) {
	      e.onfocus = function() {Element.addClassName(this, "inputHighlight");}
	      e.onblur = function() {Element.removeClassName(this, "inputHighlight");}
	      e.title="Required field";
	  };
	  e=$("emailModelNo");
	  if (e) {
	      e.onfocus = function() {Element.addClassName(this, "inputHighlight");}
	      e.onblur = function() {Element.removeClassName(this, "inputHighlight");}
	  };
	  e=$("emailQuestion");
	  if (e) {
	      e.onfocus = function() {Element.addClassName(this, "inputHighlight");}
	      e.onblur = function() {Element.removeClassName(this, "inputHighlight");}
	  };
	  
	  e= ($("navTab"));
	  if(e) {
	   var hTabValue = $F("ctl00_ContentPlaceHolder1_hTab");
       $A($("navTab").getElementsByTagName("A")).each( function(elA){
            //if tab matches hidden field value
            if(elA.id.split(" ")[0] == "a" + hTabValue.split(" ")[0]) {
                Element.removeClassName(elA,'inActiveTab')
                Element.addClassName(elA,'selectedTab')
            } 
            else {
                if (Element.hasClassName(elA,"selectedTab")){
                    Element.removeClassName(elA,'selectedTab')
                    Element.addClassName(elA,'inActiveTab')
                }
	            elA.onmouseover = function() {  
	                Element.removeClassName(this,'inActiveTab');
	                Element.addClassName(this,'selectedTab');
	            }
	            elA.onmouseout = function() {
	                Element.removeClassName(this,'selectedTab');
	                Element.addClassName(this,'inActiveTab')
                }
            }
		});
      };
     
    }
};  //end of _main object
/*
var Detail = {
    open:function(modelNo,serialNo){
        $("detailInfo").style.visibility="visible";
        if(serialNo != undefined){$("hSerialNumber").value = serialNo;};
        if(modelNo != undefined){$("hModelNumber").value = modelNo;};
        if (detailHeight <= 155) {
			detailHeight += 20;
			$("detailInfo").style.height = detailHeight.toString() + 'px';
			window.setTimeout("Detail.open('" + modelNo + "','" + serialNo +  "')",1);
		}else{
		    $("detailModelNo").innerText = $("hModelNumber").value;
		};
    },
    close:function(){
        if (detailHeight > 0) {
			detailHeight -= 20;
			$("detailInfo").style.height = detailHeight.toString() + 'px';
			window.setTimeout("Detail.close()",1);
		} else {	
            $("detailInfo").style.visibility="hidden";
        }
    },
    email:function(){
        Detail.close();
        Email.openEmailForm($F("hModelNumber"),$F("hSerialNumber"));
    }
};
*/
var Email = {
   openEmailForm: function(modelNo, serialNo){
   //     $("detailInfo").style.visibility="hidden";
        $("emailForm").style.visibility="visible";
        emailFormVisibility = "visible";
        if(serialNo!=undefined){$("hSerialNumber").value = serialNo};
        if (emailHeight <= 250) {
			emailHeight += 20;
			$("emailForm").style.height = emailHeight.toString() + 'px';
			window.setTimeout("Email.openEmailForm('" + modelNo +  "')",1);
		}else {
	        if($("emailModelNo")!= undefined) {$("emailModelNo").value = "Question about item " + modelNo;}
	        if($("emailHeaderText")!= undefined) {$("emailHeaderText").innerHTML = "<div>Question about item " + modelNo + "</div>"; }
		    $("emailFields").style.display="block";
		    $("fromEmail").focus();
		    //Detail.close();
		};
    },
           
    closeEmailForm: function(){
        $("emailFields").style.display="none";
        if (emailHeight > 0) {
			emailHeight -= 20;
			$("emailForm").style.height = emailHeight.toString() + 'px';
			window.setTimeout("Email.closeEmailForm()",1);
		} else {	
            $("emailForm").style.visibility="hidden";
            emailFormVisibility = "hidden";
            emailHeight=0;
        }
    },
    
    showSentMsg:function(){
	    $("emailFields").style.display="none";
        if (emailHeight >= 45) {
			emailHeight -= 20;
			$("emailForm").style.height = emailHeight.toString() + 'px';
			window.setTimeout("Email.showSentMsg()",1);
        } else {
            $("emailSent").style.display="block";
            window.setTimeout("Email.closeAfterSent()",3000);
        }
	},
	
	closeAfterSent:function(){
	    $("emailForm").style.visibility="hidden";
	    $("emailSent").style.display="none";
	    emailFormVisibility = "hidden";
	    emailHeight=0;
	},
	
    sendEmail:function(){
	//makes an Ajax call- static method invoked in the webserver with arguments.  Calls callback client function after request is done.
	    if (Email.Validate($("fromEmail"))){
           //static method invoked in the webserver with arguments.  Calls Email.callback client function after request is done. 
           usedCatGenerator.RentalEquip.SendEmail_Click($F("fromEmail"),$F("chkSendCopy"),$F("emailModelNo"),$F("emailQuestion"),$F("hSerialNumber"),Email.callback,Email.reportError)
        };
	},
	
	reportError:function(res){
	    alert(res);
	},
	
	callback:function(res){
        var eMailRes = res.value;
        if (eMailRes=="OK"){
            Email.showSentMsg();
        } else {
            alert(eMailRes);
        }
    },
    
/** DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)*/
    echeck:function(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }
 		 return true;
    },

    Validate:function (fEmail){
        if ((fEmail.value==null)||(fEmail.value=="")){
		    alert("Please enter your email address")
		    fEmail.focus();
		    return false;
	    }
	    if (Email.echeck(fEmail.value)==false){
		    fEmail.focus();
		    return false;
	    }
	    return true;
    }  
}
