﻿var ButtonImagePath = "http://localhost:9284/syndicate/";

function handleEvent(aEvent,opt){ 
	  // if aEvent is null, means the Internet Explorer event model, 
	  // so get window.event. 
		var retVal=true
	  var myEvent = aEvent ? aEvent : window.event; 
	  
		if( typeof( myEvent.which ) == 'number' ) {
			//NS 4, NS 6+, Mozilla 0.9+, Opera
			myEvent = myEvent.which;
		} else if( typeof( myEvent.keyCode ) == 'number'  ) {
			//IE, NS 6+, Mozilla 0.9+
		   myEvent= myEvent.keyCode;
		   //alert(myEvent);
		} else if( typeof( e.charCode ) == 'number'  ) {
			//also NS 6+, Mozilla 0.9+
			myEvent = myEvent.charCode;
		} else {
			//TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE
			//alert('test');
		   return true;
		}
		
		
		switch(opt) {
			case 1:
		 		if(myEvent<48 || myEvent > 57)retVal=false;
				break;
			case 2:
				if(myEvent <65 || myEvent >122)  {
					retVal=false;
				}
				if(myEvent==32) {
					retVal=true
				}
				break;
			case 3:
				if(myEvent < 48 || myEvent > 57)  {
					retVal=false;
				}
				if(myEvent==37 || myEvent==46) {
					retVal=true
				}
				break;	
		    case 4:
				if(myEvent < 48 || myEvent > 57)  {
					retVal=false;
				}
				if(myEvent==45 || myEvent==46) {
					retVal=true
				}
				break;	
				
		     case 5:
				if(myEvent < 48 || myEvent > 57)  {
					retVal=false;
				}
				if(myEvent==37) {
					retVal=true
				}
				break;	
			//case 6: alpha numeric(0-9;A-Z;a-z)
		    case 6:
				if((myEvent>47 && myEvent < 58) || (myEvent>64 && myEvent<91) ||(myEvent>96 && myEvent<123))
				{
				    retVal=true;
				}
				else
				{
				    retVal=false;
				}
				break;	
			//case 7: phone validation(0-9;space;-;)
			case 7:
			    
			    //alert(myEvent);
			    if((myEvent > 47 && myEvent < 58) || (myEvent==32) || (myEvent==45))
			    {
			        
			        retVal=true;
			        
			    }
			    else
			    {
			        retVal=false;
			    }
			    break;	
			
			case 8:
				if(myEvent < 48 || myEvent > 57)  {
					retVal=false;
				}
				if(myEvent==47) {
					retVal=true
				}
				break;
			case 9:				
				retVal=false;				
				break;	
			 case 10:
				if(myEvent > 47 && myEvent < 57)  {
					retVal=true;
				}
				else{
				    retVal=false;
				}
				break;			
			case 11:
			    
			    //alert(myEvent);
			    if(myEvent > 47 && myEvent < 58)
			    {
			        
			        retVal=true;
			        
			    }
			    else
			    {
			        retVal=false;
			    }
			    break;		
							
		}
		if(myEvent==8 || myEvent==0)  
		{
		retVal=true
		}
		
		return retVal
	}

    function countit()
    {
       // var formcontent=document.wordcount.txbStoryArea.value
        var formcontent=document.getElementById('txbStoryArea').value;
        formcontent=formcontent.split(" ");
        //int wordcount = formcontent.length;
        alert("" + formcontent.length + "");
    }
    
  
    
function CountWords (this_field, show_word_count, show_char_count) 
{
    if (show_word_count == null) {
    show_word_count = true;
    }
    if (show_char_count == null) {
    show_char_count = false;
    }
//    var char_count = this_field.value.length;
//    var fullStr = this_field.value + " ";
    var char_count = document.getElementById("ctl00_ContentPlaceHolder1_txbStory").value.length;
    var fullStr = document.getElementById("ctl00_ContentPlaceHolder1_txbStory").value + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length -1;
    if (fullStr.length <2) {
    word_count = 0;
    }
    if (word_count == 1) {
    wordOrWords = " word";
    }
    else {
    wordOrWords = " words";
    }
    if (char_count == 1) {
    charOrChars = " character";
    } else {
    charOrChars = " characters";
    }

 document.getElementById("ctl00_ContentPlaceHolder1_txbWordCount").value = word_count;
 document.getElementById("ctl00_ContentPlaceHolder1_hiddenWordCount").value = word_count;   
}


function CheckDateFormatsearchLog(sender,args)
{
   // var retValue=isDate(document.getElementById("ctl00_ContentPlaceHolder1_txbDate").value,true,"");
   var rdo = document.getElementById("ctl00_ContentPlaceHolder1_rdoDateWise");
   if (rdo.checked)
   {
          var frDT= isDate(document.getElementById("ctl00_ContentPlaceHolder1_txbFromDate").value);
          var toDT= isDate(document.getElementById("ctl00_ContentPlaceHolder1_txbToDate").value);
          var frDTNULL = false;
          if(Trim(document.getElementById("ctl00_ContentPlaceHolder1_txbFromDate").value) =="")
            {
//                alert("Enter valid date");
//                return false;
                  frDT=true;
            }
          if(Trim(document.getElementById("ctl00_ContentPlaceHolder1_txbToDate").value) =="")
            {
//                alert("Enter valid date");
//                return false;
                  toDT=true;
            }  
            
            if(frDT && toDT )
            {
                return true;
            }
            else
            {
            alert("Enter valid date");
                return false
            }
    }
    else
    {
        return true;
    }
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function Trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function CheckBasePrice(sender,args)
{
    var opt=document.getElementById("ctl00_ContentPlaceHolder1_rdoNewPrice");
    
    if(opt.checked)
    {
        if(args.Value!="")
        {
            args.IsValid = true;
            return
        }
        else
        {
            args.IsValid = false;
            return;
        }
    }
    else
    {
        args.IsValid = true;
        return;
    }
}

function CheckDateFormat(sender,args)
{
   // var retValue=isDate(document.getElementById("ctl00_ContentPlaceHolder1_txbDate").value,true,"");
   //var retValue= isDate(document.getElementById("ctl00_ContentPlaceHolder1_txbDate").value);
   //alert(args.Value);
   var retValue= isDate(args.Value);
    if(retValue)
    {
        args.IsValid = true;
        return;
    }
    else
    {
        args.IsValid = false;
        return;
    }
}

function isDate(dtStr)
{
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;
    
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}



function WordsCount(this_field, show_word_count, show_char_count,destination_field) 
{
    
    if (show_word_count == null) {
    show_word_count = true;
    }
    if (show_char_count == null) {
    show_char_count = false;
    }
    var char_count = document.getElementById(this_field).value.length;
    var fullStr = document.getElementById(this_field).value + " ";

    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length -1;
    if (fullStr.length <2) {
    word_count = 0;
    }
    if (word_count == 1) {
    wordOrWords = " word";
    }
    else {
    wordOrWords = " words";
    }
    if (char_count == 1) {
    charOrChars = " character";
    } else {
    charOrChars = " characters";
    }
    
    document.getElementById(destination_field).value = word_count;
}

function CheckSampleFormat(sender, args)
 {
   var temp = args.Value;
    var str = [];
    
   
    
    str = temp.split('.');
    var lastIndex=str.length-1;
    if (str.length >= 2)
    {
        if(str[lastIndex].toUpperCase() == 'DOC' || str[lastIndex].toUpperCase() == 'TXT' || str[lastIndex].toUpperCase() == 'RTF' || str[lastIndex].toUpperCase() == 'PDF' || str[lastIndex].toUpperCase() == 'JPG' || str[lastIndex].toUpperCase() == 'GIF' || str[lastIndex].toUpperCase() == 'ZIP')
        {
            args.IsValid = true;
            return;
        }
        else
        {
            args.IsValid = false;
            return;
        }
    }
    else
    {
        args.IsValid = false;
        return;
    }
    
}
function CheckTranslationFormat(sender, args)
 {
   var temp = args.Value;
    var str = [];
    
   
    
    str = temp.split('.');
    if (str.length == 2)
    {
        if(str[1].toUpperCase() == 'DOC' || str[1].toUpperCase() == 'PDF')
        {
            args.IsValid = true;
            return;
        }
        else
        {
            args.IsValid = false;
            return;
        }
    }
    else
    {
        args.IsValid = false;
        return;
    }
    
}



function CheckCommissionedImageFormat(sender, args)
 {
   var temp = args.Value;
    var str = [];
    
   
    
    str = temp.split('.');
    if (str.length == 2)
    {
        if(str[1].toUpperCase() == 'JPG' || str[1].toUpperCase() == 'GIF')
        {
            args.IsValid = true;
            return;
        }
        else
        {
            args.IsValid = false;
            return;
        }
    }
    else
    {
        args.IsValid = false;
        return;
    }
    
}
function ClearFields(this_field,type)
{
    var this_field_value= document.getElementById(this_field).value;
    if((this_field_value == "Give as many details as possible") || (this_field_value == "List anything else that we should know before writing your copy") || (this_field_value == "List anything else that we should know before writing your news release")||(this_field_value == "Please specify city (If any)"))
    {
        document.getElementById(this_field).value="";
    }
    else if(this_field_value == "")
    {   
        if(type == 1)
        {
            document.getElementById(this_field).value = "Give as many details as possible";
        }
        else if(type == 2)
        {
            document.getElementById(this_field).value = "List anything else that we should know before writing your copy";
        }
        else if(type == 3)
        {
            document.getElementById(this_field).value = "List anything else that we should know before writing your news release";
        }
        else if (type==4)
        {
            document.getElementById(this_field).value = "Please specify city (If any)";//this is used in post content page..publishing from city
        
        }
    }
}

function ClearWordCount(this_field,destination_field)
{
   if( document.getElementById(this_field).value == "")
   {
    document.getElementById(destination_field).value = "";
   }
}
function CheckIfBasePriceEntered(sender,args)
{
    var chk = document.getElementById("ctl00_ContentPlaceHolder1_chkBasePrice");
    if(chk.checked)
    {
       if (document.getElementById("ctl00_ContentPlaceHolder1_txbNewBasePrice").value!="")
        {
         args.IsValid =true;
         return;
         }
       else
          {
           args.IsValid =false;
           document.getElementById("ctl00_ContentPlaceHolder1_txbNewBasePrice").focus();
           return;
          }
    }
    else
    {
    args.IsValid=true;
    return;
    }
}

function CheckForCvEntry(sender,args)
{

 
   if (document.getElementById("ctl00_ContentPlaceHolder1_btnCV").value!="")
        {
         args.IsValid =true;
         return;
         }
   else
          {
         
              if (document.getElementById("ctl00_ContentPlaceHolder1_lblCV").innerText!="")
                { 
                args.IsValid=true;
                return;
                }
               else
               {
               args.IsValid=false;
               return;
               }
         }
}

function CheckForSampleFiles(sender,args)
{



     if (document.getElementById("ctl00_ContentPlaceHolder1_btnWriteSampl1").value!="")
        {
       
         args.IsValid =true;
         return;
         }
   else
          {
              if (document.getElementById("ctl00_ContentPlaceHolder1_lblSample1").innerText!="")
                { 
                args.IsValid=true;
                return;
                }
               else
               {
               args.IsValid=false;
               return;
               }
         }
}
function CopyWritingSelection()
    {
    
        if(document.getElementById("ctl00_ContentPlaceHolder1_rdoOnsite_0").checked)
        {
           // alert("Onsite Copywriting");              
             document.getElementById("divNo").style.display="block";
              document.getElementById("divYes").style.display="none"; 
              
              
        }
        else if(document.getElementById("ctl00_ContentPlaceHolder1_rdoOnsite_1").checked)
        {    
            //alert("Onsite Copywriting");         
            document.getElementById("divNo").style.display="none";
              document.getElementById("divYes").style.display="block";
              
        }
    }


// Function to switch the image from review.jpg to edit-save.gif
// Display or hide the div containing UserControl securitycode.
function ChangeImage()
{
    var ctrl = document.getElementById("ctl00_ContentPlaceHolder1_btnReview");
    var mydiv = document.getElementById("divsecurityCode");
    //var scode = document.getElementById("ctl00_ContentPlaceHolder1_ucSecurityImage_txbSecurityCode");
    
    //alert(scode.value);

    if(ctrl.src!=(ButtonImagePath + "images/edit-save.gif"))
    {
        ctrl.src="../images/edit-save.gif";
        mydiv.style.display="none";
    }
    else
    {
        ctrl.src="../images/review.jpg";
        mydiv.style.display="block";
    }
}
function CheckDateFormatUserList(sender,args)
{
          var frDT= isDate(document.getElementById("ctl00_ContentPlaceHolder1_txbStartDate").value);
          var toDT= isDate(document.getElementById("ctl00_ContentPlaceHolder1_txbEndDate").value);
         
          if(Trim(document.getElementById("ctl00_ContentPlaceHolder1_txbStartDate").value) =="")
            {
               frDT=true;
            }
          if(Trim(document.getElementById("ctl00_ContentPlaceHolder1_txbEndDate").value) =="")
            {
                 toDT=true;
            }  
            
         if(frDT && toDT )
            {
                return true;
            }
            else
            {
            alert("Enter valid date");
                return false
            }
}
function CheckBugRequirementFormat(sender, args)
 {
    var temp = args.Value;
    var str = [];
    
    str = temp.split('.');
    if (str.length == 2)
    {
        if(str[1].toUpperCase() == 'JPG' || str[1].toUpperCase() == 'GIF'||str[1].toUpperCase() == 'TXT'||str[1].toUpperCase() == 'DOC')
        {
            args.IsValid = true;
            return;
        }
        else
        {
            args.IsValid = false;
            return;
        }
    }
    else
    {
        args.IsValid = false;
        return;
    }
    
}


function GetSelectedItem (sender, args) 
{
    if(args.Value!="")
    { 
     args.IsValid=true;
      return;
    }
    else
    {
        if(document.getElementById("ctl00_ContentPlaceHolder1_txbOtherCategory").value!="")
         { 
          args.IsValid= true;
           return;
         }
         else
         {
          args.IsValid= false;
           return;
         }
    }
}
function CheckForPhoneNoEntry (sender, args) 
{
   if (document.getElementById("ctl00_ContentPlaceHolder1_ucComnReg_txbPhoneCountryCode").value!="" && document.getElementById("ctl00_ContentPlaceHolder1_ucComnReg_txbPhoneAreaCode").value!="" && document.getElementById("ctl00_ContentPlaceHolder1_ucComnReg_txbPhone").value!="")
   {
    args.IsValid= true;
    return;
   }
   else
   {
       if (document.getElementById("ctl00_ContentPlaceHolder1_ucComnReg_txbMobileCountryCode").value!="" && document.getElementById("ctl00_ContentPlaceHolder1_ucComnReg_txbMobileAreaCode").value!="" && document.getElementById("ctl00_ContentPlaceHolder1_ucComnReg_txbMobile").value!="")
       {
       args.IsValid= true;
       return
       }
       else
       {
       args.IsValid= false;
       return;
       }
   }
}
function CheckPublishDate(sender,args)
{
  if( document.getElementById('ctl00_ContentPlaceHolder1_chkPostImmediately').checked)
  {
  args.IsValid = true;
  return;
  }
  else
  {
    var retValue= CheckDateTimeFormat(args.Value);
    if(retValue)
    {
        args.IsValid = true;
        return;
    }
    else
    {
        args.IsValid = false;
        return;
    }
  }

}
function CheckDateTimeFormat(str)

{
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})( )(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

    var matchArray = str.match(datePat); // is the format ok?

    if (matchArray == null)
    {
    //alert(" Date is not in a valid format.")
    return false;
    }
    
    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];
   
    hour = matchArray[6];
    minute = matchArray[7];
    second = matchArray[9];
   //ampm=matchArray[11]
   
    if (month < 1 || month > 12) 
     { 
     // check month range
     //alert("Month must be between 1 and 12.");
     return false;
     }
    if (day < 1 || day > 31)
     {
     //alert("Day must be between 1 and 31.");
     return false;
     }
    if ((month==4 || month==6 || month==9 || month==11) && day==31)
     {
     //alert("Month "+month+" doesn't have 31 days!")
      return false;
     }
    if (month == 2) 
     { // check for february 29th
     var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day==29 && !isleap)) 
         {
          //alert("February " + year + " doesn't have " + day + " days!");
          return false;
         }
     }
    if (second=="")
     { second = null; }
//   if (ampm=="") { ampm = null }
    if (hour < 0  || hour > 23)
     {
     //alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
     return false;
     }
     //AM or PM 
//    if (hour <= 12 && ampm == null)
//     {
//     if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time"))
//      {
//      alert("You must specify AM or PM.");
//      return false;
//      }
//     }
//    if  (hour > 12 && ampm != null)
//     {
//   //  alert("You can't specify AM or PM for military time.");
//     return false;
//     }

    if (minute < 0 || minute > 59)
     {
     //alert ("Minute must be between 0 and 59.");
     return false;
     }
    if (second != null && (second < 0 || second > 59))
     {
     //alert ("Second must be between 0 and 59.");
     return false;
     }

    return true;
}

function checkContactMethod(sender, args)
{
 if(args.Value!="Make a choice..")
         { 
          args.IsValid= true;
           return;
         }
         else
         {
          args.IsValid= false;
           return;
         }
}



