// JavaScript Document
   
    /***********************************************
    * Email Validation script- © Dynamic Drive (www.dynamicdrive.com)
    * This notice must stay intact for legal use.
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/


    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

    function checkmail(e){
    var returnval=emailfilter.test(e.value)
    if (returnval==false){
    //alert("Please enter a valid email address.")
    e.select()
    }
    return returnval
    }
    
   
   
    function TakeTheChallenge()
    {
        var vIsLoggedIn = document.getElementById("ctl00_hdnIsLoggedIn");
        var vHasDogs = document.getElementById("ctl00_hdnHasDogs");
        if(vIsLoggedIn!=null && vIsLoggedIn.value=="0" )
        {
            //user is not logged in        
            $.blockUI({ message: $('#box_1_jc') }); 
        }
        else
        {
            //user is logged in        
            if(vHasDogs!=null && vHasDogs.value=="0" )
            {
                //user has no dogs: redirect to PetsInfo
                location="PetsInfo.aspx";
            }
            else
            {
                //user has dogs: redirect to JointTracker
                location="JointTracker.aspx";
            }
        }
    }
    

   
    
	function CloseSignin()
	{
		document.getElementById('ctl00_TopSignup1_signin_form').style.display="none";
	}
	function CloseForgotPassword()
	{
		document.getElementById('ctl00_TopSignup1_signin_forgot_password_form').style.display="none";
	}
	function CloseThankyou()
	{
		document.getElementById('ctl00_TopSignup1_signin_thankyou_form').style.display="none";
	}
	function OpenSignin()
	{
		document.getElementById('ctl00_TopSignup1_signin_form').style.display="block";
	}
	function OpenForgotPassword()
	{
		document.getElementById('ctl00_TopSignup1_signin_forgot_password_form').style.display="block";
	}
	function OpenThankyou()
	{
		document.getElementById('TopSignup1_signin_thankyou_form').style.display="block";
	}

	function OpenSignOut()
	{
	}
    
    function isEmpty(obj)
    {
        if(obj==null || obj.value=="" )
        {
            return true;
            }
        else  {  
            return false;
            }
    }
    
    
    //forgot your password validation
    function ForgotPwdValidation(el)
    {
        var bResp = 0; 
        var obj = null;
        //your email (required and valid)
        obj = document.getElementById("txtForgotPwdEmail");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -1;
        bResp = checkmail(obj)
        if(bResp==false)
            return -2;
        return 0;
    }
    

    //forgot your password validation
    function ForgotPwdValidation_jc(el)
    {
        var bResp = 0; 
        var obj = null;
        //your email (required and valid)
        obj = document.getElementById("txtForgotPwdEmail_jc");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -1;
        bResp = checkmail(obj)
        if(bResp==false)
            return -2;
        return 0;
    }

    
    //post your story validation   
    function PostStoryValidation(el)
    {
        var bResp = false; 
        var obj = null;
        //first name
        obj = document.getElementById("ctl00_Main_txtFirstName");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -1;
        //last name
        obj = document.getElementById("ctl00_Main_txtLastName");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -2;
        //your email (required and valid)
        obj = document.getElementById("ctl00_Main_txtEmail");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -3;
        bResp = checkmail(obj)
        if(bResp==false)
            return -4;
        //captcha image text
//        obj = document.getElementById("ctl00_hdnCaptcha");
//        var obj2 = document.getElementById("ctl00_Main_CodeNumberTextBox");
//        if(obj.value!=obj2.value)
//            return -5;
//        //story title
        obj = document.getElementById("ctl00_Main_txtTitle");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -6;
        //story itself
        obj = document.getElementById("ctl00_Main_txtStory");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -7;
  
        //alert("PostStoryValidation:  "  + el);
        return 0;
    }
    
    
    
    //sign-in validation
    function SignInValidation(el)
    {
        var bResp = 0; 
        var obj = null;
       // debugger
        //sign-in email (required and valid)
        obj = document.getElementById("txt_email");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -1;
        bResp = checkmail(obj)
        if(bResp==false)
            return -2;
        //your password (required)
        obj = document.getElementById("txt_password");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -3;
        bResp = checkmail(obj)
        return 0;
    }


   
    //sign-in validation
    function SignInValidation_jc(el)
    {
        var bResp = 0; 
        var obj = null;
       // debugger
        //sign-in email (required and valid)
        obj = document.getElementById("txt_email_jc");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -1;
        bResp = checkmail(obj)
        if(bResp==false)
            return -2;
        //your password (required)
        obj = document.getElementById("txt_password_jc");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -3;
        bResp = checkmail(obj)
        return 0;
    }



//    //tell a friend validation
//    function TAFValidation(el) {
//        var bResp = 0; 
//        var obj = null;
//        //your name
//        obj = document.getElementById("ctl00_Main_txtTAFYourName");
//        bResp = isEmpty(obj)
//        if(bResp==true)
//            return -1;
//        //your email (required and valid)
//        obj = document.getElementById("ctl00_Main_txtTAFYourEmail");
//        bResp = isEmpty(obj)
//        if(bResp==true)
//            return -2;
//        bResp = checkmail(obj)
//        if(bResp==false)
//            return -3;
//        //friend name
//        obj = document.getElementById("ctl00_Main_txtTAFFriendName1");
//        bResp = isEmpty(obj)
//        if(bResp==true)
//            return -4;
//        //friend email (required and valid)
//        obj = document.getElementById("ctl00_Main_txtTAFFriendEmail1");
//        bResp = isEmpty(obj)
//        if(bResp==true)
//            return -5;
//        bResp = checkmail(obj)
//        if(bResp==false)
//            return -6;
//        //your message
//        obj = document.getElementById("ctl00_Main_txtTAFMsg");
//        bResp = isEmpty(obj)
//        if(bResp==true)
//            return -7;
//        return 0;
//    }
//    
    
    //tell a friend validation (joint care): without message
    function TAFJointCareValidation(el)
    {
        var bResp = 0; 
        var obj = null;
        //your name
        obj = document.getElementById("ctl00_Main_txtTAFYourName");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -1;
        //your email (required and valid)
        obj = document.getElementById("ctl00_Main_txtTAFYourEmail");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -2;
        bResp = checkmail(obj)
        if(bResp==false)
            return -3;
        //friend name
        obj = document.getElementById("ctl00_Main_txtTAFFriendName");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -4;
        //friend email (required and valid)
        obj = document.getElementById("ctl00_Main_txtTAFFriendEmail");
        bResp = isEmpty(obj)
        if(bResp==true)
            return -5;
        bResp = checkmail(obj)
        if(bResp==false)
            return -6;
        return 0;
    }
    
    
    
    

    //check if the user is older than 13 or under 18 for Maine
    function CheckAge()
    {
    //debugger
        //current date
        var time=new Date();
        var curMonth=time.getMonth()+1;
        var curDate=time.getDate();
        var curYear=time.getYear() + "";
        //for firfox we need to add 1900 to the current year (?)
        var browserName=navigator.appName; 
        if (browserName=="Netscape")
        { 
            curYear = parseInt(curYear) + 1900;
            //alert("curYear=" + curYear);
        }
        //input date
        var vmonth=document.getElementById("cmbMonths");
        var vdate=document.getElementById("cmbDays");
        var vyear=document.getElementById("cmbYears");
        var vstate=document.getElementById("cmbStates");
        
        //logic to compare
        if(vmonth!=null && vdate!=null && vyear!=null)
        {
            if(vmonth.selectedIndex>0 && vdate.selectedIndex>0 && vyear.selectedIndex>0)
            {
                var vmonthVal = vmonth.selectedIndex + "";
                var vdateVal = vdate[vdate.selectedIndex].value;
                var vyearVal = vyear[vyear.selectedIndex].value + "";
                var vstateVal = vstate[vstate.selectedIndex].value;
 
                //compare y+13 YYYYMMDD
                var vYearPlus13 = parseInt(vyearVal) + 13;
                //state Maine
                if(vstateVal=='ME') 
                    vYearPlus13 = parseInt(vyearVal) + 18;
                
                var vAgePlus13 = vYearPlus13 + leading_zero(vmonthVal,2) + leading_zero(vdateVal,2);
                var vCurAge = curYear + leading_zero(curMonth,2) + leading_zero(curDate,2);
                //alert("vCurAge=" + vCurAge + ",  vAgePlus13=" + vAgePlus13);
                if(vCurAge<vAgePlus13)
                {
                    //1. prompt
                    alert("<asp:Literal  runat='server' Text='<%$ Resources:Resource, Reg_PeopleUnder13%>' />");
                     //2. set the add new user flag
                    var el = document.getElementById("HiddenField2");
                    el.value="Under13";
                    //3. kick out
                    createCookie("Under13","1",1000);
                    window.close();
                }
            }
        }
    }

    function leading_zero(number, num_digits) {
	    var zero = '0';
	    var nat_log = Math.floor(Math.log(number)/Math.log(10));
	    return zero.rep(num_digits - nat_log - 1) + '' + number;
    }
    function str_rep(n) {
	    var s = "", t = this.toString()
	    while (--n >= 0) s += t
	    return s
    }
    String.prototype.rep = str_rep

    
   function popup(mylink, windowname)
   {
    debugger    
        if (! window.focus)return true;
        var href;
        if (typeof(mylink) == 'string')
           href=mylink;
        else
           href=mylink.href;
        window.open(href, windowname, 'width=758,height=850,scrollbars=yes');
        return false;
   }   
        
        function popup2(mylink, windowname)
        {
        
        if (! window.focus)return true;
        var href;
        if (typeof(mylink) == 'string')
           href=mylink;
        else
           href=mylink.href;
        window.open(href, windowname, 'width=900,height=725,scrollbars=yes');
        return false;
        }

    
    function  HideMain()
    {
        var vmain = document.getElementById("form1");
        if(vmain!=null)
            vmain.style.display='none';
        var iframe1 = document.getElementById("FRAME1");
        if(iframe1!=null)
            iframe1.style.display="block";
    }
    
        function  DisplayMain()
    {
        var vmain = document.getElementById("form1");
        if(vmain!=null)
            vmain.style.display='none';
        var iframe1 = document.getElementById("FRAME1");
        if(iframe1!=null)
            iframe1.style.display="block";
    }

function WebForm_DoPostBackWithOptions(options) 
{ 
//alert("options: " + options);  

    var validationResult = true;   
    if (options.validation) {   
        if (typeof(Page_ClientValidate) == 'function') {   
            validationResult = Page_ClientValidate(options.validationGroup);   
        }   
    }   
    if (validationResult) {   
        if ((typeof(options.actionUrl) != "undefined") &&    
                           (options.actionUrl != null) &&    
                           (options.actionUrl.length > 0))    
        {   
            theForm.action = options.actionUrl;   
        }   
        if (options.trackFocus) {   
            var lastFocus = theForm.elements["__LASTFOCUS"];   
            if ((typeof(lastFocus) != "undefined") && (lastFocus != null)) {   
                if (typeof(document.activeElement) == "undefined") {   
                    lastFocus.value = options.eventTarget;   
                }   
                else {   
                    var active = document.activeElement;   
                    if ((typeof(active) != "undefined") && (active != null)) {   
                        if ((typeof(active.id) != "undefined") &&    
                                           (active.id != null) &&    
                                           (active.id.length > 0))    
                        {   
                            lastFocus.value = active.id;   
                        }   
                        else if (typeof(active.name) != "undefined") {   
                            lastFocus.value = active.name;   
                        }   
                    }   
                }   
            }   
        }   
    }   
    if (options.clientSubmit && validationResult) {   
        __doPostBack(options.eventTarget, options.eventArgument);   
    }   
}   

function WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit) {   
    this.eventTarget = eventTarget;   
    this.eventArgument = eventArgument;   
    this.validation = validation;   
    this.validationGroup = validationGroup;   
    this.actionUrl = actionUrl;   
    this.trackFocus = trackFocus;   
    this.clientSubmit = clientSubmit;   
}   

  function Page_ClientValidate(validationGroup) {   
    Page_InvalidControlToBeFocused = null;   
    if (typeof(Page_Validators) == "undefined") {   
        return true;   
    }   
    var i;   
    for (i = 0; i < Page_Validators.length; i++) {   
        ValidatorValidate(Page_Validators[i], validationGroup, null);   
    }   
    ValidatorUpdateIsValid();   
    ValidationSummaryOnSubmit(validationGroup);   
    Page_BlockSubmit = !Page_IsValid;   
    return Page_IsValid;   
}   


 function doPostBack2() {  
     var theform = document.aspnetForm;  
     theform.submit();  
 }  
 
 
 function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function checkCookie()
{
    if (readCookie('Under13'))
    {
//        location="AError.aspx";
        location="EligibilityError.aspx";
    }    
}



//phone change event handler
function OnPhoneChange()
{
    var phone1 = document.getElementById("ctl00_Main_txtPhone1");
    var phone2 = document.getElementById("ctl00_Main_txtPhone2");
    var phone3 = document.getElementById("ctl00_Main_txtPhone3");
    var i=1000;
    var i2 = i + 2000;
    var phone = document.getElementById("ctl00_Main_txtPhone");
    phone.value= phone1.value + phone2.value + phone3.value;  
   
}

//mobile phone change event handler
function OnMobileChange()
{
    var phone1 = document.getElementById("ctl00_Main_txtMobile1");
    var phone2 = document.getElementById("ctl00_Main_txtMobile2");
    var phone3 = document.getElementById("ctl00_Main_txtMobile3");
    var i=1000;
    var i2 = i + 2000;
    var phone = document.getElementById("ctl00_Main_txtMobile");
    phone.value= phone1.value + phone2.value + phone3.value;  
}


//postal code change event handler
function OnPostalCodeChange()
{
    var pcode1 = document.getElementById("ctl00_Main_txtPostalCode1");
    var pcode2 = document.getElementById("ctl00_Main_txtPostalCode2");
    var pcode = document.getElementById("ctl00_Main_txtPostalCode");
    pcode.value=pcode1.value+pcode2.value;
}



//function OnBodyLoad()
//{
////debugger

//    var phone1 = document.getElementById("txtPhone1");
//    var phone2 = document.getElementById("txtPhone2");
//    var phone3 = document.getElementById("txtPhone3");
//    var phone = document.getElementById("txtPhone");
//    
//// alert(phone);
//// phone.value="3333333333";
//// 
//    
//    if(phone!=null && phone1!=null && phone2!=null && phone3!=null) 
//    {
//        phone1.value = phone.value.substring(0,3)
//        
////         alert(phone1.value);
////         
//         
//        phone2.value = phone.value.substring(3,6)
//        phone3.value = phone.value.substring(6,10)
//    }
//}

function ClearText(cntrlName)
{
    if(cntrlName!=null)
    {
    //alert(cntrlName);
        var cntrl = document.getElementById(cntrlName);
//        var cntrl = document.getElementById("ctl00_mainContent_"+cntrlName);
   //alert(cntrl);
        if(cntrl!=null)
        {
            cntrl.value="";
        }
    }
}

//PY: 2008-11-24
//generic function allowing to restore the text box by losing the focus
function RestoreText(cntrlName,cntrlText)
{
    if(cntrlName!=null)
    {
    //alert(cntrlName);
   // debugger
        var cntrl = document.getElementById(cntrlName);
        if(cntrl!=null)
        {
            if(cntrl.value=="")
            {
                cntrl.value=cntrlText;
            }
        }
    }
}





function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}	



function checkFields() {
	var flag=false;
	var tvalue = "";
	var vStreetNum = document.getElementById("ctl00_Main_txtStreetNum");
	if(flag==false && vStreetNum!=null)
	    flag = !IsEmpty(vStreetNum);
	var vStreet = document.getElementById("ctl00_Main_txtStreet");
	if(flag==false && vStreet!=null)
	    flag = !IsEmpty(vStreet);
	var vAptSuite = document.getElementById("ctl00_Main_txtAptSuite");
	if(flag==false && vAptSuite!=null)
	    flag = !IsEmpty(vAptSuite);
	var vCity = document.getElementById("ctl00_Main_txtCity");
	if(flag==false && vCity!=null)
	    flag = !IsEmpty(vCity);
   	var vProvince = document.getElementById("ctl00_Main_cmbProvinces");
	if(flag==false && vProvince!=null)
	{
	    if(vProvince.selectedIndex>0)
    	    flag = true;
	}   
    var vPostalCode = document.getElementById("ctl00_Main_txtPostalCode");
	if(flag==false && vPostalCode!=null)
	    flag = !IsEmpty(vPostalCode);
	var vFontProvince = document.getElementById("font_province");
	if(vFontProvince!=null)
	{
	    vFontProvince.style.display= (flag) ? "block" : "none";
	}
	var vFontPostalCode = document.getElementById("font_postal_code");
	if(vFontPostalCode!=null)
	    vFontPostalCode.style.display= (flag) ? "block" : "none";
}


