/*
    ###############################################################################################
    File Name : common.js
    Author    : Anamika Kanderi
    Purpose   : This file contains some javascript functions that are used more than once
                in the project and hence are common.
    Comments  : Please do not include any javascript function in this file which is not used
                more than once. Also, add functions to this file only after thorough testing.
    ###############################################################################################

**********************************************************************
      Updation Log:
*****************************************************************************************************************
S.No.    Version    | Modified By    | Date        | Purpose                |  Modification Description 
-----------------------------------------------------------------------------------------------------
*/

/*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Name        :    validateRequireds
    Purpose        :    This function checks for any blank field. If a blank field is found
                then it alerts the user to fill it.
    Usage        :    validateRequireds(document.formName, 'firstName', 'lastName', 'emailID');
    Arguments    :    variable # of arguments with first argument being the <form> reference
                (Object) and rest of the arguments as form field names(String).
    Return        :    Boolean. true if all the fields contain some data, otherwise false.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
function validateRequireds(){
    var vRFormName = "";
    var vMissingInfo= "";
    var vFocusCounter=0;

    if(arguments.length > 1){
        vRFormName = arguments[0];
        for(vRCounter=1; vRCounter<arguments.length; vRCounter++){
            if( vRFormName[arguments[vRCounter]] ){
                if(trimSpaces( vRFormName[arguments[vRCounter]].value).length == 0){
                    vMissingInfo += "\n     -  "+convertVariable(vRFormName[arguments[vRCounter]].name);
                    if(vFocusCounter==0)
                        vFocusCounter=vRCounter;
                }
            }
        }
        if (vMissingInfo != ""){
            vMissingInfo ="_____________________________\n" + "You failed to fill in your required fields:\n" +
            vMissingInfo +"\n_____________________________" + "\n Please re-enter and submit again!";
            alert(vMissingInfo);
            vRFormName[arguments[vFocusCounter]].focus();
            return false;
        }
    }
    return true;
}


/*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Name        :    validateEmail
    Purpose        :    This function checks for a valid email address.
    Usage        :    validateEmail( "some-id@some-domain.ext" );
    Arguments    :
            email    -    String. A String representing an email address.
    Return        :    Boolean. true if valid email address otherwise false.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

function validateEmail(email){
    invalidChars = " /:,;"
    if(email == ""){                 //email cannot be empty
        return false;
    }

    for(i=0; i<invalidChars.length; i++){ //check for invalid characters
        badChar = invalidChars.charAt(i);
        if(email.indexOf(badChar,0) != -1){
            return false;
        }
    }

    atPos = email.indexOf("@",1);         //there must be one "@" symbol
    if(atPos == -1){
        return false;
    }
    if(email.indexOf("@",atPos+1) != -1){ //check to make sure only one "@" symbol
        return false;
    }

    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1){ // make sure there is one "." after the "@"
        return false;
    }

    if(periodPos+3 > email.length){ // must be at least 2 chars after the "."
        return false;
    }
    return true;
}


function validateDate(dateString)
{
    var delimeter = "/";
    var dayStr;
    var monthStr;
    var yearStr;
    var strDateArray;

    if (dateString.indexOf(delimeter) != -1)
    {
        strDateArray = dateString.split(delimeter);
        if (strDateArray.length != 3)
            return false;
        else
        {
            dayStr        = strDateArray[1];
            monthStr    = strDateArray[0];
            yearStr        = strDateArray[2];
        }
    }
    else
        return false;
	
    if(dayStr.length == 0 || monthStr.length == 0 || yearStr.length != 4 )
        return false;
    if(isNaN(dayStr))
        return false;
    if(isNaN(monthStr))
        return false;
    if(isNaN(yearStr))
        return false;

    // SR#1 Start
    // Convert strings to ints .
    var day   = parseInt(dayStr,10);
    var month = parseInt(monthStr,10);
    var year  = parseInt(yearStr,10);
    // SR#1 End
  //  return getDateStatus(day,month,year);
    return getDateStatus(day,month,year);
}

/*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Name        :    getDateStatus
    Purpose        :    This function returns the no of days in particular month of particular year.
    Usage        :    getDateStatus(2,2,2002)
    Arguments    :    month,year
    Return        :    true/false
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
    function getDateStatus(day,month,year)
    {
        if(year<0 )
            return false;
        else
        {
            switch(month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                {
                    if (day <1 || day > 31)
                        return false;
                    else
                        return true;
                }
                case 4:
                case 6:
                case 9:
                case 11:
                {
                    if (day <1 || day > 30)
                        return false;
                    else
                        return true;
                }
                case 2:
                {
                    if(((year%4==0)&&(year%100!=0))||(year%400==0))
                    {
                        if(day<0 || day>29)
                            return false;
                        else
                            return true;
                    }
                    else
                    {
                        if(day < 0 || day>28)
                            return false;
                        else
                            return true;
                    }
                }
                default :
                    return false;
            }
        }
    }


/*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Name        :    validateSelects
    Purpose        :    This function forces selection of options other than first one in <select> controls.
                Prompts the user to choose another option if the user has selected the first option.
    Usage        :    validateSelects(document.formName, 'selectObj1', 'selectObj2', 'selectObjN');
    Arguments    :
            formObj    -    Object. A reference to the <form> object.
            ***    -    String. Rest of the arguments should be <select> Object names(String).
    Return        :    Boolean. true if some other option is chosen but the first one in each of the
                <select> controls, otherwise false.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

function validateSelects(formObj){
    vSSResult = true;
    for(vSSCounter=1; vSSCounter<arguments.length; vSSCounter++){
        vSSObj = formObj[arguments[vSSCounter]];
        if( vSSObj && vSSObj.selectedIndex == 0 ){
            alert( "Please choose another option from '" +convertVariable(vSSObj.name)+ "'" );
            vSSObj.focus();
            vSSResult = false;
            break;
        }
    }
    return vSSResult;
}

 function adjust()
{
    width  =  0;
    height = 0;
    if( navigator.appName.indexOf("Microsoft") != -1 )
    { // Microsoft
        width  = document.body.scrollWidth+26;
        height = document.body.scrollHeight+50;
    }
    else
    {
        width  = document.width+26;
        height = document.height+30;
    }

    // Put condition here
    width  = ( width  > screen.availWidth  ? screen.availWidth  : width );
    height = ( height > screen.availHeight ? screen.availHeight : height );
    
    x = (screen.availWidth/2)  - (width/2);
    y = (screen.availHeight/2) - (height/2);

    window.moveTo( x, y );
    window.resizeTo( width, height );

}



function popup(varUrl,sWidth,sHeight){
	window.open(varUrl,'newwindow','width='+sWidth+' height='+sHeight+' top=150 left=250');
}

function takeAction(chngUrl,varId, varAction){
		
	if(confirm("Do you want to delete this realtor permanently from the site?")){
		//var chngUrl = window.location.href;
		chngUrl = chngUrl + '?id=' + varId + '&action=' + varAction;
		//alert(chngUrl);
	   //frmtakeaction.action=chngUrl;
	   //alert(frmtakeaction.action);
	   //frmtakeaction.submit();
		window.location.href = chngUrl;
	}
}

function showCart()
 {
   newWin =window.open('thanksupload.php','new2','width=650,height=500,resizable=yes,scrollbars=yes');
   newWin.focus();
 }